About multithreading

How to kill a stream? ..... How to restart them again with multithreading?

+5
source share
7 answers

I port my workflows to my own class and use the finished property to kill the thread flow loop.

Sorry, I don’t have a java version right now, but you should get an 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();
            }

        }

    }
}
+2
source

Since your post is marked as “Java,” I have a good idea of ​​what you are saying. Let's say you start a topic by doing:

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

, destroy , . , "". runnable , . interrupt.

foo.interrupt();

Runnable , , .

+4

Thread.stop() , (. API ). Thread.interrupt() , .

Java B. Goetz, Java Concurrency , Addison-Wesley Professional.

+3

Thread - run :

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

    // Thread will end gracefully here.
  }
}

, Thread . ( Thread.start , , IllegalThreadStateException.)

start.

, : Concurrency Java.

+3

- , . , , true. .

+1

, , , , Java 5 concurrency . , , , , .

+1

:

SE:

Java?

? stop(),

// Java?

, java.

public void start()

; Java .

: ( start) ( ).

. , .

API . ExecutorService:

Java Executor?

0
source

All Articles