Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how does Thread.join works with synchronized/non-synchronized methods  RSS feed

 
Nitin Rao
Greenhorn
Posts: 8
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I followed a thread question on Niko's Java Blog . Here is the code:



Just before the main method exits, the account’s number field is guaranteed to have value 2000.
A)true
B)false

here, I am confused on following 3 things:
1. in above exact code, both s1 and s2 are showing their values as TERMINATED. yet the account's number value is not guaranteed as 2000. Why is it so?.
2. also when I add synchronized keyword in account's method increase(), then the number value is guaranteed to come as 2000. How?
3. in above case 2 if I comment the join() statements, and let the increase() method be synchronized then again number's value is NOT guaranteed to be 2000. How & Why?

forgive me for quering too many things 1 question, but I am totally confused. I know that's the single object's resource that two objects are using. I also know that join method holds the thread on wait until the thread on which it is called has been completed. So there are 3 threads : one, two and main. But still confused to see output.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in above exact code, both s1 and s2 are showing their values as TERMINATED. yet the account's number value is not guaranteed as 2000. Why is it so?.

Both threads access increase method randomly but not in serial way when you don't synchronized increase method hence not atomic. That's why you can't guarantee for 2000 value.

also when I add synchronized keyword in account's method increase(), then the number value is guaranteed to come as 2000. How?

After making it synchronized you are making the increase method atomic which guarantees only 1 thread can access at any time. Hence value is 2000.

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in above case 2 if I comment the join() statements, and let the increase() method be synchronized then again number's value is NOT guaranteed to be 2000. How & Why?

I am not sure about this. Lets wait for the masters speak about it.
 
Nitin Rao
Greenhorn
Posts: 8
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:
in above exact code, both s1 and s2 are showing their values as TERMINATED. yet the account's number value is not guaranteed as 2000. Why is it so?.

Both threads access increase method randomly but not in serial way when you don't synchronized increase method hence not atomic. That's why you can't guarantee for 2000 value.

also when I add synchronized keyword in account's method increase(), then the number value is guaranteed to come as 2000. How?

After making it synchronized you are making the increase method atomic which guarantees only 1 thread can access at any time. Hence value is 2000.



Tushar, bhai, while digging for the solution I also come across the fact that increment (i.e. number++) process is NOT an atomic. Is it true.
 
Henry Wong
author
Sheriff
Posts: 22832
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitin Rao wrote:
Tushar, bhai, while digging for the solution I also come across the fact that increment (i.e. number++) process is NOT an atomic. Is it true.


Yes. The increment operation is *not* atomic.


Also, the JVM may do register caching... so, if you don't use synchronize or volatile, then sharing variables, even with atomic operations may not work correctly.

Henry
 
Henry Wong
author
Sheriff
Posts: 22832
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:
in above case 2 if I comment the join() statements, and let the increase() method be synchronized then again number's value is NOT guaranteed to be 2000. How & Why?

I am not sure about this. Lets wait for the masters speak about it.


If the main thread doesn't wait for the other threads to finish (ie. with the join() method call), then it may look at the "result" before the threads finish. Hence, it may not be at the value of 2000 yet.

Henry
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks Henry..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!