Monday, 15 February 2010

multithreading - java start a thread using isAlive() -


I'm new to multithreaded; Is this the right way to go about starting this thread? ({SesThread.isAlive ()} {try {sesThread.start ()}} hold (IllegalThreadStateException e) {System.out.println ("yu start")}}

Code>

Complex : Process bacteria through the caller and push it into a line. The session thread is for the deck and the further process is that the queue is empty until it is empty and hence the run of session

Problem : To get a lot of exceptions throwing me and here Until my session thread is also called twice for some reason!

i.e. (start> start> end> ​​end) not (start> end> ​​start> end)

Is there any way to synchronize or ensure this "lazy immediacy" -A system should be started only once?

PS Im creates a multithreaded UDP socket server for the speed of delivery, so it would be great to keep the minimum delay and some threads () before isAlive ()

No, you should not use this mechanism.

Qi is empty, so your consumer thread should not end. Threads are expensive to start, you should use the blocking queue and you should use your consumer thread block when the queue is empty.

  Public class Dothrides {Public Static Zero (string AG []) throws interrupted expressions {System.out.println ("toothrads: test"); New toothgrade () testing (); } // The end of the list. Private static final integer end = -1; Static class creator walkable (last block queue) & lt; Integer & gt; Queue; Public Producer (Blocking QE & lt; Integer & gt; Queue) {this.queue = queue; } @ Override Public Wide Run (try {for try (for int i = 0; i & lt; 1000; i ++) {queue.add (i); Thread.Sleep (1); } // queue.add (end); } Grip (Prerupted Pre-Probe) {// Exit Now}}} Static Class Consumer Equipment Runnabal {Last Blocking QAU & lt; Integer & gt; Queue; Public Consumers (BlockingQue  queue) {this.queue = queue; } @ Override Public Wide Run () {Boolean Finish = Incorrect; While (! Finished) {integer i = queue.take (); End = I == end; Println (i); }}} Public Zero Test () throws blocking exceptions {BlockingQueue & lt; Integer & gt; Queue = new linking linking & lt; & Gt; (); Thread pt = new thread (new creator (queue)); Thread ct = new thread (new user (queue)); // It all starts going pt.start (); Ct.start (); // Wait for it to end pt.join (); Ct.join (); }}  

No comments:

Post a Comment