• 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
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Synchronization and Locks

 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm still a little bit confused about the concept of synchronization and locks.
For the below code, is there only one object holding a lock to the run method thus when
thread 1 runs, thread 2 cannot access the run method.
What's the difference if I replace the first implementation of the run method with the commented one?
I am not getting something here....Can someone pls help?

Thanks,
Clement
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this example, there is no difference. Acquiring a lock via a synchronized block like this:

has the exact same effect as writing
synchronized void foo(){
//code
}
In both cases, you are specifying that only one thread can run the method/block at a time, because you are synchronizing on the object itself. In the case of the synchronized method, the "this" is implicit. It's explicit in the synchronized block. The benifit of using a block then, is that you can synchronize on some different object than "this", if your particular program needs to do that for a variety of reasons.
But like I said, in your program, they are functionally equivalent.
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, in this case, are there two different objects holding a lock on to itself each time they
enter the run method ?
Thanks,
Clement
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there are two (or more) threads each trying to get the same lock, and since only one thread can get the lock at a time, only one thread will be able to run your run() method in your class.
[ May 15, 2002: Message edited by: Rob Ross ]
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
Thanks for your fast response.
Pls have a look at the following code which displays a random value of i.
My question is, as you said only one thread can obtain a lock on the run method at a time,
why is it that the value of i is not printed in order, ie either Thread1 (or Thread2)
finish printing the value of i first, only then the next thread gets the turn.

Thanks,
Clement
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the key is here:
for synchronized method:
only the datas that accessed by the method will be locked,not means that the second thread cant invoke the same method.Situation can be thus:the two thread both can get chance to run,when thread1 get it,it prints something but not all,then thread2 get the chance to run,because the method didnt lock any datas,so thread2 can easily continue,so it also print.
may be :
Thread One is running... at i=0
Thread One is running... at i=1
Thread One is running... at i=2
Thread One is running... at i=3
Thread One is running... at i=4
Thread One is running... at i=5
Thread One is running... at i=6
Thread One is running... at i=7
Thread Two is running... at i=0
Thread Two is running... at i=1
Thread Two is running... at i=2
Thread Two is running... at i=3
Thread Two is running... at i=4
Thread Two is running... at i=5
Thread Two is running... at i=6
Thread Two is running... at i=7
Thread Two is running... at i=8
Thread Two is running... at i=9
Thread One is running... at i=8
Thread One is running... at i=9
i think the synchronized object also the same reason.
if i am wrong,correct me
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there r two threads running?
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1. I think it's because there are two instances of thread being created, c1 and c2
and the synchronization is being done on each of them ('this').
But I'm a bit unsure how many objects were created actually ?
for eg.
Thread t1 = new Thread();
Thread t2 = new Thread();
Are there two objects being created for the above? Hmm...going back to the basics...
2. Check out the below code, where the synchronization is done on the object o. It will print out
the value of i in order.



Thanks,
Clement
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Did not get a reply from anyone just yet.
Rob, Corey, bartenders, etc.?? Anyone to help out, pls?

Clement
 
Steven Sun
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because Object o is locked by synchronized,so the second thread will wait for the object lock to be set free by the first one,so the output will be in order.
is that clear??
Object o is "public" for both threads,that is the key.
 
Sheriff
Posts: 15986
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clement,
The key point to remember is that the locking is done on a specific object. The same code can still be executed concurrently by different threads if they are using two different instances.
In your first example where you synchronize the run method, the synchronization is pretty useless and just adds overhead.
// two objects created
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
// these two threads still execute the code
// in run() concurrently despite the synchronize
t1.start(); // executes run() with lock on t1
t2.start(); // executes run() with lock on t2
In your second example where you synchronize on a common object, the two threads have to wait to obtain the lock on o, which is why you are getting the results you expected.
See this JavaWorld article for a good discussion of threads.
HTH,
Junilu
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! Thanks for the help, guys.
Clement
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, taking advantage of Clement´┐Żs doubt, if the class implemented Runnable instead of extending Thread, would the syncronization make a difference?
Thanks,
Francisco
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Francisco A Guimaraes:
...if the class implemented Runnable instead of extending Thread, would the syncronization make a difference?


No. Locking and synchronization is done at the Object level (i.e. every object and class has a lock). Therefore, how synchronization is performed is not based upon what type of object you have (Thread vs. Runnable).
Corey
 
Steven Sun
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Steven Sun:
I think the key is here:
for synchronized method:
only the datas that accessed by the method will be locked,not means that the second thread cant invoke the same method.Situation can be thus:the two thread both can get chance to run,when thread1 get it,it prints something but not all,then thread2 get the chance to run,because the method didnt lock any datas,so thread2 can easily continue,so it also print.
may be :
Thread One is running... at i=0
Thread One is running... at i=1
Thread One is running... at i=2
Thread One is running... at i=3
Thread One is running... at i=4
Thread One is running... at i=5
Thread One is running... at i=6
Thread One is running... at i=7
Thread Two is running... at i=0
Thread Two is running... at i=1
Thread Two is running... at i=2
Thread Two is running... at i=3
Thread Two is running... at i=4
Thread Two is running... at i=5
Thread Two is running... at i=6
Thread Two is running... at i=7
Thread Two is running... at i=8
Thread Two is running... at i=9
Thread One is running... at i=8
Thread One is running... at i=9
i think the synchronized object also the same reason.
if i am wrong,correct me


Sorry for my misunderstanding.
the key should be that u have start two threads ,that is, you have two cn28 objects,so synchronized is meaningless.Remember,synchronized is for one object,only u want to use multi-threads to change a single object or its members,synchronized is helpful.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic