• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Starting thread twice

 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though main thread throws exception ,the t1 thread continues to run, right?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic