• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Illustration about locking (Threads)

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, would you give me an opinion if this image illustrates correctly the concept about thread locking :

I'm studying Java Threads and drafted this image.

Thank you very much !





 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is impossible to say whether this is correct or not without looking at the full code, but assuming the code is as usual for threads(e.g. o is a Runnable and object1 is same as o, etc), you are right, the picture correctly depicts the concept.
Also, instead of writing synchronized(object1), it would even be okay if you declare the method as synchronized in this case. When run method of one thread finishes, notifyAll() method is automatically called and another random thread waiting for this monitor will be picked up for running.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do post the entire code. It is not possible to visualize the threads until you do so.

PS: I like the thread running man
 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys:

I thing the better code to illustrate that image is:


The possible output is:
Thread-0 = 0
Thread-0 = 1
Thread-0 = 2
Thread-0 = 3
Thread-0 = 4
Thread-2 = 0
Thread-2 = 1
Thread-2 = 2
Thread-2 = 3
Thread-2 = 4
Thread-1 = 0
Thread-1 = 1
Thread-1 = 2
Thread-1 = 3
Thread-1 = 4


or I could replace the use of 'object1' for the keyword 'this'. I think the idea would be the same.

The output is the same.





What I wanted to illustrate with this picture is the idea of the threads 'fighting' for the lock of the object at which they will execute a method that contains an excerpt synchronized.


Thanks a lot !!!
 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another example using wait and notifyAll:


Possible output:

Thread-0 = 0
Thread-0 = 1
Thread-0 waiting....
Thread-2 = 0
Thread-2 = 1
Thread-2 waiting....
Thread-1 = 0
Thread-1 = 1
Thread-1 waiting....
after 10 seconds: wake up all threads !!!
Thread-1 = 2
Thread-1 = 3
Thread-1 = 4
Thread-2 = 2
Thread-2 = 3
Thread-2 = 4
Thread-0 = 2
Thread-0 = 3
Thread-0 = 4
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pedro abs wrote:

or I could replace the use of 'object1' for the keyword 'this'. I think the idea would be the same.


Output only will be the same!
 
I AM MIGHTY! Especially when I hold this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic