File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Illustration about locking (Threads) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Illustration about locking (Threads)" Watch "Illustration about locking (Threads)" New topic
Author

Illustration about locking (Threads)

pedro abs
Ranch Hand

Joined: Mar 25, 2010
Posts: 33
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

Joined: Aug 03, 2010
Posts: 37
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.


Cheers,
Divyesh.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Do post the entire code. It is not possible to visualize the threads until you do so.

PS: I like the thread running man


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
pedro abs
Ranch Hand

Joined: Mar 25, 2010
Posts: 33
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

Joined: Mar 25, 2010
Posts: 33
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

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Illustration about locking (Threads)
 
Similar Threads
Concurency in a Spring Web application
Concept of synchronized methods & code blocks
Request to cancel the topic
Data class -- synchronzed methods of it -legal or not
PE file