This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Starting thread twice

 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though main thread throws exception ,the t1 thread continues to run, right?
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic