开发者

About multi threading

How to开发者_如何学Go kill the thread? ..... How to restart them again in multi threading?


Since your post is tagged "Java," I have a good idea of what you are saying. Let's say you start a thread by doing:

Thread foo = new Thread(someRunnable);
foo.start();

Now that destroy and friends are deprecated, you need a way to kill the thread. Luckily for you, there has always been the concept of "interrupts." Simply change your runnable so that, on interrupt, it exits. Then call the thread's interrupt method.

foo.interrupt();

If you wrote your Runnable to handle this correctly, it will stop whatever it is doing and terminate.


Thread.stop() kills a thread, but you definitely don't want to do this (see the API documentation for an explanation why). Thread.interrupt() sends an asynchronous notification to a thread, so that it can shut itself gracefully.

For a comprehensive text on Java multithreading, I recommend B. Goetz, Java Concurrency in Practice, Addison-Wesley Professional.


The preferred way for a Thread to die is for the execution of the run method to go to completion:

Thread t = new Thread(new Runnable() {
  public void run() {
    // Do something...

    // Thread will end gracefully here.
  }
}

Once a thread gracefully dies in the example above, the Thread cannot be restarted. (Trying to call Thread.start on a thread that has already been started will cause an IllegalThreadStateException.)

In that case, one can make another instance of the thread and call start on that.

Probably a good place to get more information on threading would be Lesson: Concurrency from The Java Tutorials.


i wrap my worker threads up in their own class and use a terminated property to kill the thread proc loop.

sorry i dont have a java version to hand right now but you should get the idea from this http://pastie.org/880516

using System.Threading; 

namespace LoaderDemo
{
    class ParserThread
    {
        private bool m_Terminated;
        private AutoResetEvent m_Signal;
        private string m_FilePath;
        ...

        public ParserThread(AutoResetEvent signal, string filePath)
        {
            m_Signal = signal; 
            m_FilePath = filePath;

            Thread thrd = new Thread(this.ThreadProc);
            thrd.Start(); 
        }

        public bool Terminated { 
            set { m_Terminated = value; } 
        }

        private Guid Parse(ref string s)
        {
            //parse the string s and return a populated Guid object
            Guid g = new Guid();

            // do stuff...

            return g;
        }

        private void ThreadProc()
        {
            TextReader tr = null;
            string line = null;
            int lines = 0;

            try
            {
                tr = new StreamReader(m_FilePath);
                while ((line = tr.ReadLine()) != null)
                {
                    if (m_Terminated) break;

                    Guid g = Parse(ref line);
                    m_GuidList.Add(g);
                    lines++;
                }

                m_Signal.Set(); //signal done

            }
            finally
            {
                tr.Close();
            }

        }

    }
}


The best way to kill a thread is to set up a flag for the thread to watch. Program the thread to exit when it sees the flag is set to true. There's no way to restart a killed thread.


If you want to start, stop, restart threads at will, maybe using the Java 5 concurrency package would be a good idea. You can have an Executor that will do a bit of work, and when you need that bit of work to be done again, you can just re-schedule it to be done in the executor.


Regarding your first query on killing thread:

You can find more details about topic in below SE questions:

How to properly stop the Thread in Java?

How can I kill a thread? without using stop();

How to start/stop/restart a thread in Java?

Regarding your second query of re-starting thread, it's not possible in java.

You can find below details in documentation page

public void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Instead of plain Threads, you can use advanced concurrent API for thread life cycle management. Have a look at this post for ExecutorService details :

How to properly use Java Executor?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜