• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized uncertainty

 
anand-k jha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Yuan Du
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My concern is:
Why not 1st thread finish using run method & then 2nd thread start, because run is synchronized here?

Anand
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic