• 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

How does this work?.Please explain

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TwoThreads{
static Thread laurel,hardy;
public static void main(String[] args){
laurel=new Thread(){
public void run() {
System.out.println("A");
try{
hardy.sleep(1000);
} catch(Exception e){
System.out.println("B");
}
System.out.println("C");
}
};
hardy=new Thread() {
public void run(){
System.out.println("D");
try{
laurel.wait();
}catch(Exception e){
System.out.println("E");
}
System.out.println("F");
}
};
laurel.start();
hardy.start();
}
}

Thanks
Nik
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arora,

Your code tries to make us fool two places.

1- hardy.sleep(1000);
sleep(...) is a static method of the Thread class and it should be called in static context. hardy.sleep(...) never means that it will cause thread hardy to sleep. Current thread will sleep.

2- laurel.wait();
wait() and notify() methods must be called in the synchronized context.
Otherwise, IllegalMonitorStateException is thrown, that is true with your code. Because you have handled that exception, S.O.P statement will get executed there, and control will continue to the next line after the try catch block.

Output sequence is not certain: but one of the possible outputs may be

A
D
E
F
C


Regards,
cmbhatt
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,
Output is

A
C
D
E
F

But i am not understanding how
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmmmmmmmmmmmmmm!

I never imagined (1000 milliseconds) 1 second time would be so short.

How did "C" come so early!!!

Did laurel wake up early?



Regards,
cmbhatt
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey chandra how to determine the output of threads because it all depends on cpu scheduling and how to answer these type of questions in scjp 5.0 exam?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arora,

1- Which OS are you using!
2- Did you compile this program?
3- Was at each execution the output sequence same?
4- What idea did you get from the output Vs code?


Regards,
cmbhatt
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
I am asking generally how to determine threads output because it all depends on cpu scheduling and i am really not comfortable regarding these type of questions. I get confused. So, how to determine the output exactly?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I agree that thread behavior is not deterministic. It varies from plateform to plateform. What I can answer to your question regarding the behavior of the threads are:

1- Thread starting sequence does not guarantee that they will run in that sequence only.

2- What time you give to the sleep(...) method is the minimum time the thread will be inactive. It is not like exactly after the specified time it will start running.

3- We can't notify thread, we notify the object on which other threads may be blocking. We don't know which thread will grasp the thread; one of the thread is chosen by the scheduler to give the lock to.

4- notifyAll() means, a chaos (I say so), all threads waiting for certain object will jump to acquire that lock, but only one will win this.

5- You can't restart a dead thread, IllegalThreadStateException is thrown.

6- If certain method is synchronized, you can guarantee that what is happening inside that method, will complete without any disorder with another thread that may be willing to acquire the lock of the object the current thread has acquired to run the sync method.

7- We must see whether wait() and notify are not being used inside the sync context. Otherwise gives runtimeexception(IllegalMonitiorStateException).

8- One more case we can look in questions like wait() or notify() methods are being called from withing sync code but the lock is of different object.
One may overlook this case; That is also like case 7 (just previous point).

9- One special case is, regarding the sequence of the call to wait() and notify on particular object, where object is notified before another thread
calls wait upon. In such a case, waiting thread go on waiting... waiting for
something that has been notified much before. Refer to last pages of Thread chapter in K&B book.


I may have missed some other important points, right not I don't remember exactly.


Regards,
cmbhatt
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya chandra but in scjp 5.0 exam how to determine the output when there are 4 choice of different sequence and how i should know that the particular thread will run.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exam creators were not boozed while creating exam to ask to choose one from the all possible answers. There are questions which are scenario based and the correct answer screams before you to be chosen. Did you go through the points I prepared attentively. If you go through them, you will find a lot relieve in that regard.

Go through book and couple of examples to see what can and what can't be.
Post doubts here!



Regards,
cmbhatt
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok one more thing. I have completed diploma. I stay in bangalore(India). I am preparing for scjp 5.0 and i want to write web component developer exam also so can you tell me how much salary i may get after completing these two exams and apply for a job.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

By the way, what diploma have you done? From Bangalore only you have done?
I am also from Bang.

Send me an e-mail!



Regards,
cmbhatt
[ April 27, 2007: Message edited by: Chandra Bhatt ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic