This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized uncertainty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized uncertainty" Watch "synchronized uncertainty" New topic
Author

synchronized uncertainty

anand-k jha
Greenhorn

Joined: Dec 02, 2008
Posts: 15
Hi All,
I just started preparing SCJP & this is my first post
Got this question from:
http://www.examulator.com/phezam/question.php



No doubt the output we cannot predict here.
I am getting 10 times zebra as output.
But when I run this program with some modification, run method as synchronized:

I am getting the same output as before but I was expecting 5 times alpha & 5 times zebra.

Anand
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
a="alpha";
Thread t = new Thread(this);
t.start();
Thread t1 = new Thread(this);
a="zebra";//executed by main thread.
t1.start();



Here main thread is changing a="zebra", before thread t start executing run() method, that why you are only getting zebra.
[ December 17, 2008: Message edited by: punit singh ]

SCJP 6
Yuan Du
Ranch Hand

Joined: Nov 20, 2008
Posts: 34
Hi punit,

main thread is changing a="zebra", before thread t start executing run() method


I cann't unterstand that. Maybe t has already executed the run() method before assigning a with "zebra".

Could you please explain it exactly?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Maybe t has already executed the run() method before assigning a with "zebra


Both case is possible Yuan, thats why we cannot predict the answer. But usually main thread will change the value of a.

Pmason(){
a="alpha";
Thread t = new Thread(this);
t.start();
Thread t1 = new Thread(this);
a="zebra";
t1.start();
}


Pmason() constructor will be executed by main thread.
t.start() will be executed by main thread. It will fork a new thread and wait for schedular to make it running, or it could happen rarely that this new thread start executing run() before main could reach a="zebra". Forking process and new thread to reach running state will usually take more time than main thread to reach a="zebra". But you cannot say confirmly, it depends on thread schedular algorigth of the system.
In most of time main thread will reach a="zebra" before new thread could reach to run() method. These all things depends on the thread schedular.


If this type of question asked in exam, you have to select "cannot predict answer".
[ December 17, 2008: Message edited by: punit singh ]
anand-k jha
Greenhorn

Joined: Dec 02, 2008
Posts: 15
My concern is:
Why not 1st thread finish using run method & then 2nd thread start, because run is synchronized here?

Anand
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Originally posted by anand-k jha:
My concern is:
Why not 1st thread finish using run method & then 2nd thread start, because run is synchronized here?

Anand


The first thread indeed completes before the second one will commence. You can check it like this



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
Consider Paul's rocket mass heater.
 
subject: synchronized uncertainty
 
Similar Threads
Thread doubt
i dont get it??
doubt in thread
Thread Code doubt
Synchronization problem