• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronized methods

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K & B suggests not to rely on synchronizedList() because just the individual methods get synchronized. So if Thread-1 is working on one synchronized method of list, other thread may work on other synchrnoized method, that might make it inconsistent or nonatomic.

I tried this code and executed lot of times.


Here Thread-1 calls the synchrnized display1() and it leaves(need clarification) and then I am making it work for a longer time in display2().
Now am expecting Thread-2 to invoke and execute display1() method before display2() is finished,but thats not happening. Its always Thread-1
executing both the methods and then Thread-2 getting its chance.

Actual output
Thread-1
Thread-1
Thread-2
Thread-2

I was expecting
Thread-1
Thread-2
Thread-1
Thread-2

From the above I understand, If multiple threads are invoking synchronized methods on same object, until one thread is done executing all the synchronized methods
no other thread can invoke any of the synchronized methods.(Probably to keep the data consistent).

If I am right till here, then why does not it happen the same way in case of synchronizedList()

synchronizedList() makes all the individual methods synchronized, but still other threads are able invoke those synchronized methods one at a time parallely(i.e two threads invoking two different synchronized methods on the same List instance).

Thanks for reading..Phewww
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Himalay,

When a thread sleeps it doesn't release any locks. Try to put the delay between display1() and display2() in the run() method.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruben

I am not expecting Thread.sleep to release lock, it was just to make sure it works for more time on display2()..so that in the meantime Thread-2 executes display1().

Isnt it the case that Thread-1 should release the lock from display1() after executing.
Here Thread-1 is executing both the synchronized methods display1() and display2() and then only allowing Thread-2 to start executing

What I am thinking is ..when Thread-1 is done executing display1(), Thread-2 should be given a chance immediately. So that..Thread-2 works on display1() and Thread-1 works on display2() simultaneously.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote:Ruben

I am not expecting Thread.sleep to release lock, it was just to make sure it works for more time on display2()..so that in the meantime Thread-2 executes display1().

Isnt it the case that Thread-1 should release the lock from display1() after executing.
Here Thread-1 is executing both the synchronized methods display1() and display2() and then only allowing Thread-2 to start executing

What I am thinking is ..when Thread-1 is done executing display1(), Thread-2 should be given a chance immediately. So that..Thread-2 works on display1() and Thread-1 works on display2() simultaneously.


But if Thread-1 starts executing display2(), it will hold the lock, and sleeping will not release that lock. There is only one lock being used in your code, which Thread-1 and Thread-2 are competing for. That's why you should sleep in the run() method between display1() and display2() to make your code work the way you expect it to.

Yes, Thread-1 releases the lock when it finishes executing display1(), but then it is executing display2() right away, which doesn't give Thread-2 the chance to acquire the lock and start executing display1().

Notice that it is possible also that Thread-2 might start executing display1() before Thread-1 starts executing display2(), but it is not too likely. You are trying to use sleep to allow that to happen, but the problem is that you are making the thread sleep at the wrong point in the code.

Does that make sense? If not, think about what you are trying to prove with your code.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it what you mean Ruben.

Lets assume two threads are working on the same object, and there are three synchronized methods A , B ,C
If thread-1 is executing A, then thread-2 cannot invoke A, B or C. Its only when thread-1 SWITCHES from one method to another, thread-2 may possibly take control.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it Himalay.

In your original code it is possible that Thread-2 might be selected by the scheduler to run as soon as Thread-1 finishes executing display1() and before it starts executing display2(). It's not very likely though.
 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why it is not possible for another thread to enter display1(); after first thread has been completed. and the first thread before entering display2() second thread can enter display1();
I executed this on my machine and I am getting output like thgis :

1) Thread-0
Thread-1
Thread-0
Thread-1


2) Thread-0
Thread-0
Thread-1
Thread-1


3 ) Thread-0
Thread-1
Thread-1
Thread-
0

And after nearly 30 excetions I got this output :
Thread-0
Thread-0
Thread-1
Thread-1

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Banu Chowdary wrote: Why it is not possible for another thread to enter display1(); after first thread has been completed. and the first thread before entering display2() second thread can enter display1();



Its not that it is impossible.... Actually it is possible and you result show that ...
Ruben was telling the same thing...

Ruben wrote: In your original code it is possible that Thread-2 might be selected by the scheduler to run as soon as Thread-1 finishes executing display1() and before it starts executing display2(). It's not very likely though.



And just to reming you again.... "when it comes to thread very little is gauranteed." Its the JVM, who RULES.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take it like this Banu..if multiple threads are working on the same object. At any point of time only one synchronized method can be executed.

1)
Thread-0
Thread-1
Thread-0
Thread-1

Thread-0 executes display1() but before it tries to execute display2()..Thread-1 starts executing display1(). And then before Thread-1 tries to execute display2(), Thread-0 executes it.

Usually a Thread should execute all the synchronized methods one after the other and then other Threads get a chance. Its not happening here, after executing display1() ..Thread-0 tries to SWITCH from display1() to display2()..but right at that switching point, the Thread Scheduler decides to allocate Thread-1(). This behavior of the Thread Scheduler is not guaranteed, and so it produces different result.

Ruben meant ..the following is the most possible output..which I agree too. Switching between methods is very fast process and interfering during that time is difficult, but NOT for your Thread Scheduler(or may be JVM).

Thread-0
Thread-0
Thread-1
Thread-1

To add to this...One should not rely on synchronizedList(), that synchronizes individual methods in a List. But (as in the above example) one thread might interfere while the other Thread is switching. That can lead to inconsistent data in the list. Check out Pg 743 K&B for your reference, shoot me back if you have any questions.

Thanks to Ruben, for clearing me on this.

Cheers
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Himalay. Also, you could have code that performs some operation between the invocation of the synchronized methods, and in that case it is much more likely for another thread to acquire the lock. Especially if you have many threads accessing the same object. But even in the case that you used, with no other unsynchronized code in between the two synchronized method invocations, it is possible for another thread to acquire the lock at the exact time when the first thread has finished executing the first synchronized method, and before it has time to enter execution for the second synchronized method. The bottom line is that if you want real synchronization you need to enclose your code in a synchronized block, or to put it in another synchronized method.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
reply
    Bookmark Topic Watch Topic
  • New Topic