• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

synchronized threads

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

in the above code, why the output is mix-up of all the objects created i.e. why the output is not synchronized( means t1 should print all its values first, then t2 the t3 the t4).
nothing is different when is remove the synchronized keyword ...
But when make the prinTable() as static then only it is printing in a synchronized behavior ?
 
author & internet detective
Posts: 41405
854
Eclipse IDE VI Editor Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each thread has a different instance of the Table object. Therefore they are synchronizing on different objects and never wait for each other.
 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

You may want to have a look at this thread:

https://coderanch.com/t/656374/java/java/Threads-turns#3040182

The code starts 3 threads that in turn print 1,2,3 e.g. thread 1 prints 1, thread 2 prints 2, thread 3 prints 3, each in turn, for ever.

Here is the latest version of the code, see link above for more information:

Printers:




Reader:

 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Each thread has a different instance of the Table object. Therefore they are synchronizing on different objects and never wait for each other.



Thumbs up! making the printTable() method static makes all threads synchronize on the common Class monitor instead of each instance monitor.

Also, I see no guarantee that thread 1 is actually going to start printing before thread 2 etc... Threads might theoretically print out of order I think...
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello A.J. Côté
I read thread provided by you, but didn't understood , because right now I have not reached the wait() notify() topics. I read this code(my question) in the static Synchronization topic . so the thread provided by you is not helpful to me

 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.J. Côté wrote:

Jeanne Boyarsky wrote:Each thread has a different instance of the Table object. Therefore they are synchronizing on different objects and never wait for each other.



Thumbs up! making the printTable() method static makes all threads synchronize on the common Class monitor instead of each instance monitor.

Also, I see no guarantee that thread 1 is actually going to start printing before thread 2 etc... Threads might theoretically print out of order I think...



In short Jeanne was right and I explained it further:

static printTable() works because you synchronize on the unique Class monitor.

printTable() does not work because you synchronize on 3 different instance monitors.

even with static, there is still a possibility that the threads will print out of order but with static at least the printout will be grouped with regards to each thread..

To guarantee the right order, you could use thread.join() then you wouldn't need to synchronize at all.

You can also use wait() and notify to guarantee the thread order. You need to use wait() and notify() if you want to loop forever with each thread executing in order, Thread.join() wouldn't work in this case.

Note that there are other layers of abstraction on top of wait() and notify()

See this package:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html


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

A.J. Côté wrote:To guarantee the right order, you could use thread.join() then you wouldn't need to synchronize at all.



I tried this: method is:

and in main():


and also ran it without join()
but, both the outputs are same ?
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more doubt I have
what is difference b/w

and
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:one more doubt I have
what is difference b/w

and



The second one allows your class to extend any class you want as long as you implement the Runnable interface while the first one forces you to extend Thread.

I usually prefer to simply implement Runnable and I think it is recommended as best practice somewhere although I am not sure ;-)
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:

A.J. Côté wrote:To guarantee the right order, you could use thread.join() then you wouldn't need to synchronize at all.



I tried this: method is:

and in main():


and also ran it without join()
but, both the outputs are same ?



You need to use join like this (then you can remove synchronization) :

 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you A.J. It helped
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic