File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Is it guaranteed a started thread will run to completion? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is it guaranteed a started thread will run to completion?" Watch "Is it guaranteed a started thread will run to completion?" New topic
Author

Is it guaranteed a started thread will run to completion?

Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
Hi guys,

I would like to know that if a thread has been started, it will run to completion?

For example, in this code, will thread "Andi" always print its name?



Is it possible to get this output?

"Eyra ", followed by an exception.

In my opinion this output is not possible. The action in the main thread is sequentially, and the call t1.start() will start thread "Andi". The call t2.start() will throw an IllegalThreadStateException in the main thread because t2 "Eyra" has already started. But after this exception, thread t1 is still alive and it will get the CPU and print "Andi", so the output:

"Eyra ", followed by an exception

is not possible, because thread t1 will ALWAYS print "Andi" as the code stands.

Am I right thinking like that?

Thanks a lot everyone,

Alvaro


OCPJP 6 98%
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That output is possible:

Thread 2 is started by the method go() and prints Eyra
Thread 1 is started but the scheduler still gives the main thread priority so nothing from the Runnable gets executed.
Thread 2 gets started again by the main thread and throws an exception.
Thread 1 prints Andi

So the output can be

Eyra
Exception
Andi


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
Thanks for the answer. One thing, as you said:

Thread 2 is started by the method go() and prints Eyra
Thread 1 is started but the scheduler still gives the main thread priority so nothing from the Runnable gets executed.
Thread 2 gets started again by the main thread and throws an exception.
Thread 1 prints Andi

So the output can be

Eyra
Exception
Andi


I am agree with you 100% till this point, but I think the output: "Eyra ", followed by an exception

still needs "Andi" to be correct, or not?

If the output is: "Eyra ", followed by an exception "Andi ", I understand, but if it is: "Eyra ", followed by an exception, I don't really understand.

Other thing is if we say:

The output will contain: "Eyra ", followed by an exception. In this case the option is correct, because it is possible that the output can contain "Eyra " and the exception.

Am I right? I don't know, I really confused.





Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Alvaro San Millan wrote:I am agree with you 100% till this point, but I think the output: "Eyra ", followed by an exception

still needs "Andi" to be correct, or not?

Correct.

Alvaro San Millan wrote:
The output will contain: "Eyra ", followed by an exception. In this case the option is correct, because it is possible that the output can contain "Eyra " and the exception.

Am I right? I don't know, I really confused.

Not really. Will contain implies that it happens in that order and always happens which isn't the case. It would consider it "more correct" if it would have said: can be instead of will contain.
Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
Thanks Wouter,

Ok, forget this shade of meaning about the expression "will contain". The important thing for me to understand is:

You are agree with me in that the output: "Eyra ", followed by an exception

still needs "Andi" to be correct.

So, are you agree with me that just the output: "Eyra ", followed by an exception is not possible. It needs "Andi " somewhere in the output.

Am I right?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You're right.
Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
Thanks Wouter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it guaranteed a started thread will run to completion?