Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange result from Thread Synchronization

 
Mahbub Rahman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic