This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization  RSS feed

 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please explain these lines in a simple manner:

When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired.

But, when you synchronize a block of code, you specify which object's lock you want to use as the lock. That gives you the ability to have more than one lock for code synchronization within a single object.

Scjp6, page (736-737)
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manpreet Kamboj wrote:Can anybody please explain these lines in a simple manner:


Can you elaborate what is your confusion? What do you think they mean?

Henry
 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Myrunnable r1 = new Myrunnable();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r1);
t1.start();
t2.start();

I know r1 is the job that workers (t1 & t2) are supposed to perform.Also, if t1 will enter synchronized method, t2 will have to wait and vice versa.

What I don't know is which object we are talking about? Are we talking about getting a lock on object r1?

 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we acquire multiple locks on single object by using synchronized blocks?


Also,if:

Myrunnable r1= new Myrunnable();
Myrunnable r2 = new Myrunnable();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();

Here both t1 and t2 can acquire lock on synchronized method at the same time? Because they will not interfere with one another. Is it so? Here can we say that threads t1 & t2 have acquired locks on different objects?
 
Paweł Baczyński
Bartender
Posts: 2047
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any locks being acquired here.
Can you show us what Myrunnable os doing?
 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I am just taking an example. Myrunnable is a class that implements runnable interface.
 
Paweł Baczyński
Bartender
Posts: 2047
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manpreet Kamboj wrote:Well, I am just taking an example. Myrunnable is a class that implements runnable interface.

I have guessed that.
What I meant that it all depends on what Myrunnable.run() contains.
There is no locking on your example. It might be some in Myrunnable.run().
 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the run method contains a call to any synchronized method or let say run itself contains synchronized block of code, that accesses let say common resource int bankbalance, then it means bank balance is the object?
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manpreet Kamboj wrote:
I know r1 is the job that workers (t1 & t2) are supposed to perform.Also, if t1 will enter synchronized method, t2 will have to wait and vice versa.

What I don't know is which object we are talking about? Are we talking about getting a lock on object r1?


The object being used as a lock, for a synchronized (non-static) method, is the object referenced by the "this" variable. This is why we are asking you to show us more code. Without knowing which method (and hence, instance) that you are referring to, we don't know which instance the "this" variable is referring to.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!