• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

thread post answer

 
Ranch Hand
Posts: 637
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was a post and after i submitted the answer it got deleted. Since i have put in the effort am still posting it



1) hardy.sleep(1000); -> is same as Thread.sleep(1000): This will simply cause the currently executing thread to sleep for 1 second. This shows static methods must always be invoked using class name to avoid confusion.

2)
Once both the threads are started, who gets to go first is upto scheduler hence "A" and "D" can get displayed in any order.
So the result could include
A
D

or

D
A

3)"B" will never get displayed in this code since no one is interrupting laurel thread.
a) In case hardy thread had a reference to laurel thread
b) laurel goes to sleep.
c) Before laurel wakes up , hardy interrupts laurel by invoking
laurel.interrupt() then "B" would have displayed.

4) laurel.wait(10000); This causes the laurel thread to
a) Wait on "laurel" object: Unless some one notifies on laurel object by invoking laurel.notify() [ This again might not cause the above thread to wake up , if there are more than one threads waiting on laurel"] or notifyAll().
OR
b) Wait for 10seconds and wakes up.

Since in this code noone is notifying , the only possiblity for laurel thread to comeout is after waiting for 10 seconds.

Further since again no one is interrupting laurel thread during its waiting period "E" would never be displayed.

5) Now these leaves us with C and F.
Once laurel & hardy threads come out of sleeping and waiting respectively they have one statement each System.out.println("C"); and System.out.println("F"); respectively to execute, before calling it a day.

The order in which these statements are executed is again depends on Thread schedular.
hence the result can include either

C
F

or

F
C


Hence the final result could be
D A C F
OR
A D C F
OR
A D C F
OR
D A F C

Hope this clears your doubt.
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak, nice to see that you have provided the solution. But, it would be great if others can have a look at the original problem too.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There was a post and after i submitted the answer it got deleted. Since i have put in the effort am still posting it



I don't know why, but certain people delete their questions (and topic), as soon as they get an answer. This obviously, isn't very nice to other users who may have similar questions -- but... oh well.

Henry
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, Henry and Deepak. I deleted the question, because after running the program several times, I understood its working and thought it was worth deleting as it was a small problem( a trivial one).But I was unaware that Deepak had already posted an answer, when i was deleting.Sorry guys.
Thanks Deepak, for the effort.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic