• 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

Threads and synchronized run()

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code:
public class Test extends Thread
{
static int x, y;
public synchronized void run(){ for(; ;){ x++; y++; System.out.println(x+" "+y);} }
public static void main(String[] args)
{
new Test().start();
new Test().start();
}
}
What will the above code print?
The answer given is " You cannot say anything about the values"
and the explanation is:
You may be tempted by the synchronized keyword on the run method. But note that, there are two diferent thread objects and both the threads are accquiring locks for their own thread object. So, in this case synchronized doesn't make any sense.
I thought it will keep on printing the values for x and y incrementing by 1 on each line. To clarify this, when i run it and saw, it does so, it keeps printing x and y incremented by 1 each time.
Is the answer given above is wrong or is my understanding wrong?
please clarify.
Thanks,
Priyha.


[This message has been edited by Marilyn deQueiroz (edited October 16, 2001).]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the answer you state is the one provided, then clearly it's just not carefully phrased.
I imagine what the author means is, "there's no pre-determined output for this code," meaning you can't predict the exact, run-time result of this code.
You can certainly describe characteristics, though. For example, for every println(), x and y will have the same value per line.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have slightly modify your code...
so you can run this code... and then you can see the O/P. It will make more clear which object's thread is running. On windowNT, do increase the buffer size of dos shell.
and Yes "you can not predict the output" as it will depend on underlying OS.
and Yes "synchronized works on same object's method." If there are two object then synchronized won't work. JVM Locks the object, I think.
If I am wrong then do reply.
Regards
Ravish
public class Test extends Thread
{
int i;
static int x, y;
public Test(int i){
this.i = i;
}
public synchronized void run(){
for(; ;) {
x++;
y++;
System.out.println("Test" + i + " x=" + x + ":y=" + y );
}
}
public static void main(String[] args)
{
new Test(1).start();
new Test(2).start();
}
}

[This message has been edited by ravish kumar (edited October 16, 2001).]
 
Priyha Jootu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravish,
I ran the code given by you... yes, both the threads are printed in a random order but the values x and y are incremented by 1 each time , right?
rgds,
priyha
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Ernest:
for every println(), x and y will have the same value per line.


Not correct.
Consider this:
Initialy x and y are 0.
Thread 1 runs and increments x and y to 1. Now it creates the string for println. It takes the value of x (i.e 1) and before it could take the value of y, Thread 2 runs and increments the values seting x and y to 2. Now, thread 1 runs again and completes the string creation by taking y's current value ie. 2.
So, Thread 1 prints 1 2.
You simply cannot determine what kind of values it'll print.

------------------
SCJP2, SCWCD Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
JQPlus - For SCJP2
JWebPlus - For SCWCD
JDevPlus - For SCJD
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh! I ignored the static declaration of x and y. My response above of course is in error.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Priyha Jootu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou very much Paul, I got your point.
rgds,
priyha.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic