wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Help: Basics of Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Help: Basics of Thread" Watch "Help: Basics of Thread" New topic
Author

Help: Basics of Thread

Rishi Kanth
Ranch Hand

Joined: May 23, 2006
Posts: 43
Hi , this maybe a silly doubt but i want to make myself clear.

We use the keyword Synchronized to avoid the race condition. when a thread wants to execute a Synchronized method, firt it needs to grab the lock to the object. If any other thread comes up it needs to wait.
My doubt is wht is this lock ??? actually. Example in the class shown:

public class BusyFlag {
protected Thread busyflag = null;
public Synchronized void getBusyFlag () {
while (busyflag != Thread.currentThread()) {
if (busyflag == null)
busyflag = Thread.currentThread();
try {
Thread.sleep(100);
} catch (Exception e) {}
}
}
public Synchronized void freeBusyFlag () {
if (busyflag == Thread.currentThread()) {
busyflag = null;
}
}
}

Grabing a lock to the object means obtaining the lock of the class object or...??
I hope your understanding me guys.

Thank You
Rishi
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
Hi Rishi,

The lock obtained by the synchronized keyword is actually the lock of the object. When we refer to the current object in our code, we use the "this" keyword (i.e. "this.variable = new variable;"). If you understand this concept, then the following example might help you understand what is being locked when you synchronize a method.

Example: These two excerpts of code are identical for synchronization purposes



and



The lock obtained when you synchronize on an object (using synchronize(this) or synchronizing the method) is essentially a mutual-exclusion lock (mutex), which can be used to enforce that at any one time only one thread can own this lock. You can also synchronize on class variable, a common use of this is to synchronize access to Collection classes (i.e. when multiple threads access a Hashtable) instead of the whole object, and you can even synchronize on the class itself (ClassName.class). This is a different lock than synchronizing on an oject instance, and may take a bit more explaining. I would recommend Andrew Monkhouse's "SCJD Exam with J2SE" book for a better explanation of these concepts (and a whole lot more useful information about the exam too!)

Best wishes,

Daniel
[ July 27, 2006: Message edited by: Daniel Bryant ]

SCJP 1.4,<br />SCJD (URLyBird 1.2.1, JDK 5.0)
Rishi Kanth
Ranch Hand

Joined: May 23, 2006
Posts: 43
Hey Daniel

Thank You Very much . I could understand what ur saying. But there is one more doubt if u dont mind. if they are two sync. methods in a class
class XYZ{
public synchronized void abc(){}
public synchronized void def(){}
}
if a thread1 aquires lock on the object while accessing abc, and if another thread2 wants the access def. does it needs to wait for the lock to be released by thread1?? and in my further reading i came across the Class Lock for sycn of Static methods.
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
Hi Rishi,

In answer to your question - yes, thread 2 would have to wait until thread 1 had finished with the abc method. This is the core purpose of a "mutex" lock - a process accessing any synchronized method within an object (and remember also that using the synchronized keyword in a method signature is equivalent to synchronizing on the object itself) must obtain the object lock before it proceed.

I hope this helps

Best wishes,

Daniel
[ July 27, 2006: Message edited by: Daniel Bryant ]
Rishi Kanth
Ranch Hand

Joined: May 23, 2006
Posts: 43
Hey Daneil

Thank You. its clear now.


Rishi.
Best of Luck.
Sri Vishnu Mandaleeka
Greenhorn

Joined: Jul 07, 2006
Posts: 2
hey

in the example of the class XYZ. when thread1 tries to access the sync methods it needs to get the lock on the oject. ie synchronized (this) right?

my doubt is does "this" represent instance of the class from which the thread is called. or is it something else. Can someone tell me aout Class Locks for staic methods synchroniztion.

one more thing, in that example if thread1 gets the lock and is ale to access the synch method abc(), does the thread need to reaquire the lock for get access to the synch method def() or it can access the method directly as it holds the lock.

Thanks in advance
[ July 28, 2006: Message edited by: Sri Vishnu Mandaleeka ]
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
When you declare a method to be synchronized, the method call itself is the scope of the lock, so it's acquired on entry and released on exit. If you wanted to invoke 2 methods, synchronized or not, in the context of a single lock, you would have to wrap the 2 method calls in a single synchronized block.


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
Rishi Kanth
Ranch Hand

Joined: May 23, 2006
Posts: 43
Hey Ken

According to HFJ ..." Locks are not per methods they are per Objects". but what you say is they are according to the methods. Can you give me more explanation about your thought.

Rishi
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
Locks are on objects. Declaring a method as synchronized means that a lock will be obtained on the object whose method is being called before the method's code block is entered and the lock will be released after the method exits. The lock is still on the object but the scope of the lock is the duration of the method call.
Rishi Kanth
Ranch Hand

Joined: May 23, 2006
Posts: 43
Hey Ken .. I am able to get you. Thank You for explaining. Can you take a example and explain about it. like creating 2 objects of a class and threads are trying to access a sync method.

Thanks in Advance
Rishi
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
Hey everyone,

How about an example involving static method synchronization and class locks? The example can be easily modified to demonstrate synchronization with two different objects, but I'll leave that as an example...



And when I run this example I get



Here we can clearly see that initially both threads are trying to access the synchronized method, but only one manages to get the class lock (thread 1 in my example) When this thread has finished in the xyz() method (which consists of a simple pause using the sleep() method, but imagine this method contains very complex code that takes time to execute) it releases the lock, allowing the other thread (who has been patiently waiting) to gain the lock.

I hope this helps.

Daniel
Rishi Kanth
Ranch Hand

Joined: May 23, 2006
Posts: 43
Thank You very much. I have complete idea of whtz going on with threads.

Thanks guys
Rishi
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is not a specific SCJD issue, so please in future, ask general programming questions in the appropriate forum: Threads in this case.
Thanks.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
Consider Paul's rocket mass heater.
 
subject: Help: Basics of Thread