Stop () in the stream?

In the following example, how is the stop() method implemented?

What should be done instead of using the stop() method?

From my point of view, when the desired state is paused, the thread expects to use Object.wait . When the thread resumes, the target thread is notified using Object.notify . but it is doubtful in the case of the implementation of stop() in the example below.

 Class NewThread implements Runnable { String name; // name of thread Thread t; boolean suspendFlag; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); suspendFlag = false; t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for(int i = 15; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(200); synchronized(this) { while(suspendFlag) { wait(); } } } } catch (InterruptedException e) { System.out.println(name + " interrupted."); } System.out.println(name + " exiting."); } void mysuspend() { suspendFlag = true; } synchronized void myresume() { suspendFlag = false; notify(); } } 

 class SuspendResume { public static void main(String args[]) { NewThread ob1 = new NewThread("One"); NewThread ob2 = new NewThread("Two"); try { Thread.sleep(1000); ob1.mysuspend(); System.out.println("Suspending thread One"); Thread.sleep(1000); ob1.myresume(); System.out.println("Resuming thread One"); ob2.mysuspend(); System.out.println("Suspending thread Two"); Thread.sleep(1000); ob2.myresume(); System.out.println("Resuming thread Two"); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } // wait for threads to finish try { System.out.println("Waiting for threads to finish."); ob1.t.join(); ob2.t.join(); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting."); } } 
+2
source share
6 answers

The thread automatically stops if the run () function returns. There is no need to use the stop () function because the stop method is not recommended by java and it is unsafe to use

+6
source

Calling the stop method will destroy the thread on which it is called. A thread should be killed only when there is no need to continue what the thread does. When you call the stop method, Thread will stop its execution and die.

It is preferable to let the thread complete its launch method and kill it, rather than force it to kill.

+1
source

The stop () call throws an exception / error that must be thrown at the stream at a random point. If you have access to all the code for a stream, you can safely use it, but in this case you are much better off supporting interrupts.

Instead of Object.wait / notify, you'll probably be better off using a library with a high level of concurrency support, i.e. using a lock that will simplify your code.

More on stop (); Does Thread.stop () really stop Thread?

+1
source

It depends on your threads and what they should actually do.

If they are workers that, for example, listen to the tcp / ip socket, then you better have a volatile boolean inside the class, which says that the loop inside the run () method should continue. Then add your class, which extends the stream, implements the pleaseStop () function, which puts the boolean value in false, which then leads to the end of your execution method (you can even clear your resources then).

On the other hand, if these are workers who have a final job, you just have to wait until they are ready using the join () function.

0
source

The stop method in the Thread class is obsolete and unsafe to use, because it forces it to unlock all the blocks that it has blocked.

Some suggestions on how to stop the flow can be found here .

0
source
 private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if (jToggleButton1.isSelected()) { jToggleButton1.setBackground(Color.green); jToggleButton1.setText("ON"); //MainClass main = new MainClass(); new Thread(new Runnable() { @Override public void run() { try { server = new ServerSocket(4400, 500); do { socket = server.accept(); ClientHandler cliendHandler = new ClientHandler(socket); cliendHandler.start(); } while (true); } catch (IOException ex) { } } }).start(); } else { try { server.close(); jToggleButton1.setText("START SERVER"); jToggleButton1.setBackground(Color.red); } catch (IOException ex) { Logger.getLogger(Server_Prog.class.getName()).log(Level.SEVERE, null, ex); } } } 
0
source

Source: https://habr.com/ru/post/1386870/


All Articles