Why not start the thread in the constructor? How to stop?

I am learning how to use threads in Java. And I wrote a class that implements Runnable to simultaneously start another thread. The main thread handles listening on the serial port, where, when the second thread handles sending data to the same port.

public class MyNewThread implements Runnable { Thread t; MyNewThread() { t = new Thread (this, "Data Thread"); t.start(); } public void run() { // New Thread code here } 

In the first thread, the second begins:

 public class Main { public static void main(String[] args) throws Exception{ new MyNewThread(); // First thread code there } } 

This works, but my compiler notes a warning: it is dangerous to start a new thread in the constructor. Why is this?

The second part of this question is: how if I have a loop running in one thread (serial port listening thread) and I type the exit command in the second thread. How can I complete the first thread? Thank.

+42
java multithreading terminate
Apr 11 2018-11-11T00:
source share
4 answers

To your first question: Starting a thread in a constructor passing in this escapes this . This means that you actually issue a link to your object before it is fully built. The flow will begin before your constructor completes. This can lead to any kind of strange behavior.

To your second question: there is no acceptable way to force another thread to stop in Java, so you should use a variable that the thread would check whether to stop or not. Another thread would set it to indicate that the first thread would stop. The variable must be unstable or all calls are synchronized to ensure proper publication. Here is some code that will look like what you want.

 public class MyNewThread implements Runnable { private final Thread t; private volatile boolean shouldStop = false; MyNewThread() { t = new Thread (this, "Data Thread"); } public void start() { t.start(); } public void stop() { shouldStop = true; } public void run() { while(!shouldStop) { // do stuff } } } 

Regardless of what you want to create and start the stream, do:

 MyNewThread thread = new MyNewThread(); thread.start(); 

Whatever wants to stop the thread, do:

 thread.stop(); 
+42
Apr 11 '11 at 15:05
source share
— -

Let's look at a basic example:

 class MyClass implements Runnable{ int a = 0; String b = null; public MyClass(){ new Thread(this).start(); b = "Foo"; } public void run(){ a = b.length(); //can throw NullPointerException } } 

In this case, MyClass.this is said to avoid the constructor. This means that the object is available for reference, but all of its fields that are built in the constructor may not be created. To do this on another level, that if b was final you would expect it to be available, but it would not be provided. This is known as partially constructed objects and is completely legal in java.

+9
Apr 11 2018-11-11T00:
source share

about the second question, you can check if the second thread was completed or not using the isAlive method, and if so, use the break keyword to disable the loop for the first thread, then it will stop if nothing needs to be done

 public class MyNewThread implements Runnable { Thread t; MyNewThread() { t = new Thread (this, "Data Thread"); t.start(); } public void run() { reading code ................ // New Thread code here } 



 public class Main { public static void main(String[] args) throws Exception{ MyNewThread thread = new MyNewThread(); while(true) { listening code ................... if(!thread.t.isAlive()) break; } } } 
0
Feb 10 '13 at
source share

The second part of this question is: how if I have a loop running in one thread (serial port listening thread) and I type the exit command in my second thread. How can I get the first thread to end?

Continue the cycle until the condition is reached. For example:

 public void run() { while ( !inputConsole.getCommand().equals("exit") ) { //Do Something Thread.sleep(1000); //put thread to sleep for 1 second } } 
-2
Apr 11 2018-11-11T00:
source share



All Articles