This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illustration about locking (Threads)

 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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 !





 
Divyeshh Patel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic