0

I am reading the Oracle tutorials about multithreading programming in Java. I don't understand why should I create a new object to sync the some part of code? For what reason does the creation of new dummy object serve?

I do understand that creating these two objects will prevent compiler from reordering the code segment guarded by the construction syncronized(lock1){}

However, I would like to know can I use any other objects (except MsLunch) in the construction syncronized(lock1){} ?

What is the motivation behind introducing such construction syncronized(lock1){} ?

Here is the piece of code, I am concerned with:

public class MsLunch {
       private long c1 = 0;
       private long c2 = 0;
       // what is the purpose of these two objects? how do they serve as locks?
       private Object lock1 = new Object();
       private Object lock2 = new Object();

       public void inc1() {
              synchronized(lock1) {
                   c1++;
              }
       }

       public void inc2() {
              synchronized(lock2) {
                   c2++;
              } 
       }
 }
mr.M
  • 851
  • 6
  • 23
  • 41
  • 3
    You could see this question http://stackoverflow.com/questions/1085709/what-does-synchronized-mean?rq=1 – Jose Luis Jan 30 '16 at 12:00

4 Answers4

1

First some basics:

The synchronization object is used as a key to open a door to a restricted area (the synchronization block).

As long as a thread entered this restricted area it holds the monitor (lock) so that no other thread can enter. When a thread exits the restricted area it releases the monitor and another thread can take it.

This means that each synchronization block that uses the same synchronization object will prevent other thread to enter until the monitor is available (unlocked).

For what reason does the creation of new dummy object serve?

The reason is that you should use objects that are not accessible by others objects than the ones that use them. That's why the synchronization objects are private. If they would be accessible by others, other might use them in their synchronization blocks somewhere in the application. If the synchronizcation object is e.g. public then every other object can use it. This might lead to unexpected usage and might result in deadlocks.

So you should make sure who will get a reference to the synchronization object.

René Link
  • 48,224
  • 13
  • 108
  • 140
0

The lock is accessed by the threads to check if the code is currently "in use" or if they can execute it after locking the object themselves. You may think it could be automatic, but it has a use compilers couldn't infer : you can use the same lock to synchronize multiple code blocks, restraining the access to any of them at the same time.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • I would like to know can I use any other objects (except MsLunch) in the construction syncronized(lock1){} ? What is the motivation behind introducing such construction syncronized(lock1){} ? – mr.M Jan 30 '16 at 12:09
0

In my opinion, dummy Object just represents the point of synchronization.

For synchronize different threads, you must be able to declare point of synchronization and(if it is needed) give access to this point from different parts of the code. For example:

   private Object lock = new Object();

   public void inc1() {
          synchronized(lock) {
               c1++;
          }
   }

   public void inc2() {
          synchronized(lock) {
               c2++;
          } 
   }

In this example shown the usage of same point of synchronization from different part of code.

And because Java is Object oriented language i.e. unit of language in Java is Object, we have exactly such a construction.

By the way, you can use any object in as point of synchronization. You can do even this:

public void inc1() {
       synchronized(this) {
            c1++;
       }
}
Ken Bekov
  • 13,696
  • 3
  • 36
  • 44
0

what is the purpose of these two objects? how do they serve as locks?

As shown in your code, inc1() and inc2() can only be invoked by one thread each. This means that c1 and c2 can be increased just once per thread - but simultaneuosly by two different threads (because both threads synchronize on different objects).

If both blocks were synchronized like this synchronized(lock1), c1 and c2 could not be increased simultaneuosly.

Würgspaß
  • 4,660
  • 2
  • 25
  • 41