aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange result from Thread Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strange result from Thread Synchronization" Watch "Strange result from Thread Synchronization" New topic
Author

Strange result from Thread Synchronization

Mahbub Rahman
Greenhorn

Joined: Dec 12, 2012
Posts: 8
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

Joined: Dec 05, 2012
Posts: 305

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.


My one regret in life is that I am not someone else.
- Woody Allen
Mahbub Rahman
Greenhorn

Joined: Dec 12, 2012
Posts: 8
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
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mahbub Rahman
Greenhorn

Joined: Dec 12, 2012
Posts: 8
@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
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

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

Joined: Dec 12, 2012
Posts: 8
@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
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange result from Thread Synchronization