Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

synchrinized code doubt

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this code:

I have created 3 threads.All the three acessing the get() method.My understanding is once a thread enters a sychronized method any other thread cannot enter the method until it is completed.Here i expect the result to be
12
12
12

but the result is
1112
22

I understand from this that there is no synchronization in this code and my basic understanding is wrong.Could anyone explain me or alter this code so that i shall be able to understand about synchronize.
Thanks in advance.

( tags added)
[ November 11, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The wait method is releasing the lock.
 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jeff

I hve removed the wait() method.The output is ok.
But even if i remove the synchronized keyword from the method get()the output is the same.So is this code not a correct example for a synchronized behaviour.
Thanks
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, it's not, because wait shouldn't release the lock. Sleep would, but wait shouldn't. Probably the threads aren't exclusive because there's no object to acquire the lock on, so the JVM just runs the threads as though there's no synchronization at all.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe when you call wait method, it will release the lock on the object, so that other threads in queue can get a lock(assuming the threads are accessing a synchronized method). In this case as mentioned in the code..printing the values as 111...222..I believe is no strange..since the thread accessing the wait method will release it locks and wont print the value '2' at that point, where as it gives access to thread t1 or t2 depending on the JVM priority. The same thing happens with them too and hence they print only 1 at that point. When thread t1 wakes up then according to the JVM scheduling mechanism it will print '2'..so do the other threads....

Wait method releases the lock obtained by synchronized method.

Madhu
-----------
SCJP, SCWCD(In Progress)
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just checked the API for wait, and you're right, it does release the lock. I stand, er, sit corrected...
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A hint: on which object's lock are these threads synchronizing? For this to work all threads must be synchronizing on a lock belonging to one unique shared object.
 
Madhu Kumar Vatts
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except wait(), other methods such as notify(), join(), sleep(), yield()keep locks with them when they go to Non-Runnable(also referred to as Waiting/blocked/sleeping state). If these statements are true than the below program should print 121212

but its printing 111222. Anybody can explain why?

----------------------------------------------------------
public class Synch implements Runnable {
synchronized void get() {
System.out.println(1);
try {
Thread.sleep(600);//This should keep lock on the object
} catch (InterruptedException ie) {
}
System.out.println(2);
}

public void run() {
this.get();

}

public static void main(String as[]) {
Synch s = new Synch();
Synch s1 = new Synch();
Synch s2 = new Synch();
Thread t = new Thread(s);
Thread t1 = new Thread(s1);
Thread t2 = new Thread(s2);
t.start();
t1.start();
t2.start();

}
}
 
author
Posts: 23893
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the policy for this board is to only hint, to let people figure it out themselves... so here is a hint.

Change part of your program as follows: (the beginning of main)



Run it. Does it work now? And if it does, why?

Henry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Madhu-

take Barry's hint. Thanks Barry.

S.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry's hint would have been my second hint
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey barry & henry,
i concluded from ur hints that all three threads have different objects so all three threads can acess synchronised method simultaneously .Is my conclusion correct?u guys made me to think a lot.
thanks.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it! Each of the threads is using a lock not used by the other threads.
[ November 14, 2004: Message edited by: Barry Gaunt ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic