• 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

Thread Synchronization- Where to put "synchronized"?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code:


I want the method comm() to be accessed by only thread at a time. So I 'synchronized' the method comm(). There is only one object 'x' . But it not working. But when I synchronize the method run(), it works. Why is that so?
Thanks in advance.
Ajit
 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajit,

The reason why it's not working right now is because right now only the comm method is synchronized. That means that within the loop inside run(), the first thread that executes comm will acquire the lock, but as soon as comm returns the lock will be released, so the next thread could start running and call comm itself. To make synchronization work in this case you can either make run itself synchronized, or enclose the for loop in a synchronized block.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Ajit,

The reason why it's not working right now is because right now only the comm method is synchronized. That means that within the loop inside run(), the first thread that executes comm will acquire the lock, but as soon as comm returns the lock will be released, so the next thread could start running and call comm itself. To make synchronization work in this case you can either make run itself synchronized, or enclose the for loop in a synchronized block.




Hi Ruben,

Making run itself synchronized works fine. But, i tried keeping the for loop in a synchronized block as follows:


But I still noticed both threads ONE and TWO entering the block at the same time. The output was as follows:


IN FOR: The current running thread is: ONE
IN FOR: The current running thread is: TWO
Current Thread name: TWO
IN FOR: The current running thread is: TWO
Current Thread name: TWO
IN FOR: The current running thread is: TWO
Current Thread name: TWO

Why does this happen when we have specified the loop to be synchronized..

Thanks
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You are getting that behavior because you are synchronizing on two different Thread objects. You would need to synchronize on a unique instance, like the unique MultiCommon object (which you can refer to by "this" in the run() method,) or the MultiUseMe instance referred to by x (do synchronized(this) or synchronized(x) and see if that works.)
 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1: The following 3 options will work.(regardless of synchronized comm() ). But which object instance is locked?
Option1:public synchronized void run(){
This will lock the object test.
Option2:public void run(){
synchronized(x){
for (int i = 0; i<10; i++){
This will lock the object instance x.
Option3: public void run(){
synchronized(this){
for (int i = 0; i<10; i++){
This will lock the object test.
-----------------
Q2:If Thread.currentThread() returns current thread should this same as option3 above (using this)?
Q3:Ruben in your response "the MultiUseMe instance referred to by x (do synchronized(this)"-How can you get lock on object instance x by using 'do synchronized(this)'?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic