How to kill a stream? ..... How to restart them again with multithreading?
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(); } } } }
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.
destroy
interrupt
foo.interrupt();
Runnable , , .
Thread.stop() , (. API ). Thread.interrupt() , .
Thread.stop()
Thread.interrupt()
Java B. Goetz, Java Concurrency , Addison-Wesley Professional.
Thread - run :
Thread
run
Thread t = new Thread(new Runnable() { public void run() { // Do something... // Thread will end gracefully here. } }
, Thread . ( Thread.start , , IllegalThreadStateException.)
Thread.start
IllegalThreadStateException
start.
start
, : Concurrency Java.
- , . , , true. .
, , , , Java 5 concurrency . , , , , .
:
SE:
Java?
? stop(),
// Java?
, java.
public void start(); Java .: ( start) ( ).
public void start()
; Java .
: ( start) ( ).
. , .
API . ExecutorService:
Java Executor?