• 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

Strange result from Thread Synchronization

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have written the following code and found the result which is not clear to me? Can someone help me find the reason?

public class Main implements Runnable{

static Integer id = 0;

public static void main(String[] args)

{

new Thread(new Main()).start();

new Thread(new Main()).start();

}

public void run()

{

press(id);

}

synchronized void press(Integer idx)

{

System.out.println(Thread.currentThread().getName()+ " " + id.intValue());

System.out.println(Thread.currentThread().getName()+ " " + (++id).intValue());

}

}


The result is:
---------------
Thread-0 0

Thread-1 0

Thread-1 2

Thread-0 1
 
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is unclear ,mention that.

please use code tag.

BTW synchronized method usage this reference ,because you are passing two different objects so they can access your method at a time,they both will use their own lock.
 
Mahbub Rahman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Meeta.
Yes this is obvious that two objects uses their own lock.
The point I did not understand is how the below output sequence is produced.

Thread-0 0

Thread-1 0

Thread-1 2

Thread-0 1

I tried a lot but could not figure out. How can Thread-1 produce 0 and than 2? The variable is static and both thread is increasing them. If Thread-0 increased it to 1 just before Thread-1 accessed it, than Thread-0 should also print 2 as well.
Please correct me if I am wrong.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahbub Rahman wrote:
Yes this is obvious that two objects uses their own lock.
The point I did not understand is how the below output sequence is produced.

Thread-0 0

Thread-1 0

Thread-1 2

Thread-0 1

I tried a lot but could not figure out. How can Thread-1 produce 0 and than 2? The variable is static and both thread is increasing them. If Thread-0 increased it to 1 just before Thread-1 accessed it, than Thread-0 should also print 2 as well.
Please correct me if I am wrong.




One possibility....

1. thread-0 prints zero
2. thread-1 prints zero
3. thread-0 get the i variable -- increments it to 1, but before it can print...
4. thread-1 get the i variable -- increments it to 2, and prints two
5. thread-0 prints one (that it had calculated earlier)

Henry
 
Mahbub Rahman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Henry,
Thanks for the nice idea. But if the variable is static, than Thread-0 should also print 2. How do you think?
I also thought just like you did, but the confusion is the variable is static.

Am I missing something?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahbub Rahman wrote:@Henry,
Thanks for the nice idea. But if the variable is static, than Thread-0 should also print 2. How do you think?
I also thought just like you did, but the confusion is the variable is static.

Am I missing something?




Just because the variable is static doesn't mean the everything is in sync with it. Take this expression...



Yes, The id variable will be increment, but then it has to do the rest of this expression...


And do it before, it is sent for printing. It only reads and increment the id variable once, and then while it needs to calculate all the components of the expression, it has to do it with temporary variables.

Henry
 
Mahbub Rahman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Henry
Now I got the point. Thanks a lot for this excellent concept.

Ahhh...that means JVM can switch to another thread in the middle of the execution of an statement.
I mean, executing a single statement is not atomic operation. Context switching can occur at the intermediate level.

Thanks a lot Henry.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahbub Rahman wrote:@Henry
Now I got the point. Thanks a lot for this excellent concept.

Ahhh...that means JVM can switch to another thread in the middle of the execution of an statement.
I mean, executing a single statement is not atomic operation. Context switching can occur at the intermediate level.

Thanks a lot Henry.




Yeah, very few statements are atomic. Heck, even "++id" is not atomic -- as that is comprised of a load, increment, and store.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic