• 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 problem

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


My problem is Line1.I do not understant acutally two threads execute this fun() method at the same time!When i remove synchronized keyword from run() method although the fun method is synchronized the program does not produce desire result.Please help me to solve this problem.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prasun Howlader:
...although the fun method is synchronized the program does not produce desire result...


The first thread that enters the synchronized run() method will keep the RunnableThread object's lock until it finishes looping and leaves the method.

But synchronizing the fun() method doesn't have an effect, because the fun() method just prints a single line. So each time fun() is called, the Thread1 object's lock is acquired, a line is printed, and then the lock is immediately released again.
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear marc weber What happen when run() method like this-



and fun() method is-

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronizing run() is the key. If run() is synchronized, then the first thread that enters the method will keep the lock until it finishes looping through all 5 iterations. But if run() is not synchronized, then the thread scheduler can alternate between the threads while they iterate through their loops. (The brief call to sleep just makes it more likely that the thread scheduler will switch between the two.)

I don't think that adding a sleep call to fun() will do anything other than slow it down. The fun() method is called with each iteration in the run() loop. And each time fun() executes, the lock is acquired and then released. (Remember, this is a different lock than what's being used in the run() method.)

What behavior (output) are you trying to get?
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc for your good explanation. Actually i want to see the result that the thread1 completes its execution then thread2 completes its job but do not use synchronized word in run method but using synchronized in the fun method.I think my problem is understanding synchronization.
 
reply
    Bookmark Topic Watch Topic
  • New Topic