aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How does this work?.Please explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How does this work?.Please explain" Watch "How does this work?.Please explain" New topic
Author

How does this work?.Please explain

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Chandra,
Output is

A
C
D
E
F

But i am not understanding how
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 26, 2007
Posts: 652
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 26, 2007
Posts: 652
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 26, 2007
Posts: 652
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 26, 2007
Posts: 652
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 26, 2007
Posts: 652
Done
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How does this work?.Please explain