• 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

Synchronization of run method

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have this code which has a synchronized run method.




I want to know that the run method is synchronized on which object?
And if it is synchronized on 'u',then why does the output is not continuous,i.e why does it comes like this-"Thread-0 Thread-0 Thread-0 Thread-1 Thread-2..."?
Please help me out .
Thanks...
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example the run method will synchronize of the UseThreads object i.e. u object. The output is correct IMO. You get the name of the same thread 3 times. The order cannot be guaranteed as one thread completes, which thread will get lock on the object is decided by the JVM not by us (unless you use wait-notify to notify a particular thread)...
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ankit,I do agree by what you say,'
But isn't it correct that as the run method is synchronized,the threads will have a lock on 'u' object,and hence other thread cannot execute the same method run,when the lock on the object is held by some other thread?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that is true. And this is why you see the same thread 3 times in the output instead of thread names mixed with each other. Try to remove the synchronize keyword and then see the output...
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes that is true. And this is why you see the same thread 3 times in the output instead of thread names mixed with each other. Try to remove the synchronize keyword and then see the output...



Even if you remove the synchronized keyword from the method, the output is the same. i.e. same thread name 3 times and then another..like--

run:
Thread-0
Thread-0
Thread-0
Thread-2
Thread-2
Thread-2
Thread-1
Thread-1
Thread-1
Thread-3
Thread-3
Thread-3
BUILD SUCCESSFUL (total time: 1 second)

There is no difference if synchronized keyword is removed(except of the order of the Threads executing).
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ananya Raval wrote:

Yes that is true. And this is why you see the same thread 3 times in the output instead of thread names mixed with each other. Try to remove the synchronize keyword and then see the output...



Even if you remove the synchronized keyword from the method, the output is the same. i.e. same thread name 3 times and then another..like--

run:
Thread-0
Thread-0
Thread-0
Thread-2
Thread-2
Thread-2
Thread-1
Thread-1
Thread-1
Thread-3
Thread-3
Thread-3
BUILD SUCCESSFUL (total time: 1 second)

There is no difference if synchronized keyword is removed(except of the order of the Threads executing).




This is just by chance, try increasing for loop iteration to 100 and see the mixed result.
 
Ranch Hand
Posts: 33
1
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you start multiple threads simultaneously you should keep one thing in mind that, output will not be same every time you run the code.

If your run method is synchronized then any of the 4 thread name can be printed anytime, but it will be printed thrice consecutively as when one thread have lock on run method other can not enter it(which thread will gain the lock is unpredictable as it is not necessary that if a thread is started first mean it will gain the lock early), i.e -

Thread-0
Thread-0
Thread-0
Thread-2
Thread-2
Thread-2
Thread-1
Thread-1
Thread-1
Thread-3
Thread-3
Thread-3

but if the run method is not synchronized then also every names will be printed thrice but same name may not appear thrice consecutively
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic