wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Starting thread twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Starting thread twice" Watch "Starting thread twice" New topic
Author

Starting thread twice

Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

Hi,
In the below question from K&B mock tests:Practice Exam 2 ,I think " Andi " has to be printed as it runs in separate thread,Also after which we are trying to start t2 for second, the point where we get IllegalMonitorSateException.As per my thinking the answer should only be F,( which contains both "Andi" and Exception) but confused how E is also correct , can you please help Thanks.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Remember the three threads (including the main one) can execute in any order, switching control at any time. What happens if you attempt to restart t1 before it's had a chance to print anything?
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

Matthew Brown wrote:Remember the three threads (including the main one) can execute in any order, switching control at any time. What happens if you attempt to restart t1 before it's had a chance to print anything?


here we are restarting t2 and not t1, that to after staring t1,

so t1 should have no problem in printing "Andi"( at any time), and option E is not having "Andi" in output that is what my question is!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Doesn't matter. You have no idea what order things that are in different threads are going to happen in. So you've no idea at what stage the exception will be thrown. If the exception happens before t1 prints something out, you can get E.
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

Even though main thread throws exception ,the t1 thread continues to run, right?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

It should do, yes. But it's still '"Eyra", followed by an exception.'. The answer doesn't say what happens after that - though you could argue it's a bit misleading.
Grigoriy Lorenzi
Greenhorn

Joined: Sep 09, 2013
Posts: 2
Well, I think that Don is right! There are three Threads and all of them generate some kind of output.

Main: Exception
t1: "Andi"
t2: "Eyra"

They can be written out in any order, but for sure, result will contain all of them! So anser 'E' is incorrect, because it doesn't contain "Eyra".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Starting thread twice
 
Similar Threads
Will a started thread run to completion in this example?
K&B - Practice Exams book - Full Exam II - Question 48
Is it guaranteed a started thread will run to completion?
Thread Problem from K&B practice exam
Doubt on a thread question from K&B OCP Practice Exams