• 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

synchronized uncertainty

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic