wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need examples on these Thread related topics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need examples on these Thread related topics" Watch "need examples on these Thread related topics" New topic
Author

need examples on these Thread related topics

Rashid Mian
Ranch Hand

Joined: Feb 14, 2007
Posts: 31
Can any one describe these lines taken from K&R book about Threads(give example in each case ):
"
What Happens If a Thread can't Get the Lock?
If a thread tries to enter a synchronized method and the lock is already taken, the thread is said to be blocked on the object's lock. Essentially, the thread goes into a kind of pool for that particular object and has to sit there until the lock is released and the thread can again become runnable/running. Just because a lock is released doesn't mean any particular thread will get it. There might be three threads waiting for a single lock, for example, and there's no guarantee that the thread that has waited the longest will get the lock first.

When thinking about blocking, it's important to pay attention to which objects are being used for locking.

Threads calling non-static synchronized methods in the same class will only block each other if they're invoked using the same instance. That's because they each lock on this instance, and if they're called using two different instances, they get two locks, which do not interfere with each other.

Threads calling static synchronized methods in the same class will always block each other�they all lock on the same Class instance.

A static synchronized method and a non-static synchronized method will not block each other, ever. The static method locks on a Class instance while the non-static method locks on the this instance�these actions do not interfere with each other at all.

For synchronized blocks, you have to look at exactly what object has been used for locking. (What's inside the parentheses after the word synchronized?) Threads that synchronize on the same object will block each other. Threads that synchronize on different objects will not.
"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rashid Mian:
Can any one describe these lines taken from K&R book about Threads(give example in each case)...

It would be excellent practice for you to try to write code examples yourself. Try it, and if you get stuck, then post your code here so we can point you in the right direction.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Priya Venkat
Greenhorn

Joined: Mar 20, 2007
Posts: 2
Just try answering and read the explanation for the answers in the self test question on thread1 and thread2 calling x.a(), x.b(), y.a(), y.b() in SCJP 5 book K and B. That will help you understanding this part.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need examples on these Thread related topics
 
Similar Threads
Doubt in threads
Threads using two different instances Problem
thread doubt
What Happens If a thread Can't Get the Lock?
synchronized and Thread