Java: Why not to start a thread in the constructor? How to terminate?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

To your first question: Starting a thread in a constructor passing in this escapes this That means that you are actually giving out a reference to your object before it is fully constructed. The thread will start before your constructor finishes. This can result in all kinds of weird behaviors To your second question: There is no acceptable way to force another thread to stop in Java, so you would use a variable which the thread would check to know whether or not it should stop.

The other thread would set it to indicate that the first thread would stop. The variable has to be volatile or all accesses synchronized to ensure proper publication. Here is some code which would be something 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 } } } Whatever wants to create and start the thread would do: MyNewThread thread = new MyNewThread(); thread.start() Whatever wants to stop the thread would do: thread.stop().

To your first question: Starting a thread in a constructor passing in this escapes this. That means that you are actually giving out a reference to your object before it is fully constructed. The thread will start before your constructor finishes.

This can result in all kinds of weird behaviors. To your second question: There is no acceptable way to force another thread to stop in Java, so you would use a variable which the thread would check to know whether or not it should stop. The other thread would set it to indicate that the first thread would stop.

The variable has to be volatile or all accesses synchronized to ensure proper publication. Here is some code which would be something 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 } } } Whatever wants to create and start the thread would do: MyNewThread thread = new MyNewThread(); thread.start(); Whatever wants to stop the thread would do: thread.stop().

Starting a thread in a constructor only publishes this if you actually publish this (as in the code above, and even then, it's the constructor call that does the publishing). Foo = new SomeSpecialThread(); foo.start(); doesn't publish this, or does it? Now, starting a thread on its own constructor does indeed expose an incomplete object.

– R. Martinho Fernandes Apr 11 at 15:08 I expect you meant to make the field shouldStop volatile. – seh Apr 11 at 15:14 1 @seh: no need for that as all accesses to it are synchronized.

@Justin: wouldn't it be better to just inherit from Thread? – R. Martinho Fernandes Apr 11 at 15:16 1 @Justin, "To use a volatile without synchronization you need to ensure that only a single thread will ever be modifying it, which would not be the case here." ... this is wrong.

Changes to volatile variables are visible to other threads without further synchronization. Writing a volatile variable and subsequent reading same variable create "happens-before" relationship which is guaranteed to be visible in Java Memory Model. See download.oracle.

Com/javase/6/docs/api/java/util/concurrent/… for details (or JMM specs). – Peter Å tibraný Apr 11 at 15:33 1 @Peter You are certainly right, I misread the statement in JCIP.It is only the case when writes to the variable do depend on its current value that you have to ensure only a single thread is updating the value – Justin Waugh Apr 11 at 15:35.

The second part to this question is: how if I have a loop running in one thread (the serial port listen thread) and I type an exit command in my second thread. How do I get the first thread to terminate? Have it keep looping until a condition has been reached.

For example: public void run() { while (!inputConsole.getCommand(). Equals("exit") ) { //Do Something Thread. Sleep(1000); //put thread to sleep for 1 second } }.

Lets take a look at a basic example: class MyClass implements Runnable{ int a = 0; String be = null; public MyClass(){ new Thread(this).start(); be = "Foo"; } public void run(){ a = b.length(); //can throw NullPointerException } } In this instance the MyClass. This is said to escape the constructor. That means that the object is available to reference but all of its fields that are being built in the constructor may not be created.To take this to another level what if be was final You would expect it to be available but it is not ensured.

This is known as a partially constructed objects and is perfectly legal in java.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions