• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Help: Basics of Thread

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rishi Kanth
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Daneil

Thank You. its clear now.


Rishi.
Best of Luck.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rishi Kanth
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You very much. I have complete idea of whtz going on with threads.

Thanks guys
Rishi
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not a specific SCJD issue, so please in future, ask general programming questions in the appropriate forum: Threads in this case.
Thanks.
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic