• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Question about Java Synchronization

Hi there,

I have a question about "lock" in synchronization. Basically, I am not sure why I need to make a static lock object.

To illustrate, pls refer to the following two programs. The program is suppose to print "3" (the value of "final"). The first one will print out "1". I know it is because the 1st one uses a lock object which is not static and the 2nd uses a static lock object. But why?

I would appreciate it very much if you could explain in details (just a litttle bit) about Synchronization and lock (why/when static)

Thanks.

public class WrongCriticalSection  extends Thread {
      
      private static final int SLEEP_UNITS = 300;

      private static int shared = 0;
      
      private Object lock = null;
      
      public WrongCriticalSection(){
            lock = new Integer(0);
      }
      

      public void run() {
            int local;
            
            synchronized ( lock ) {      
                  local = shared;
                  try {
                        sleep(SLEEP_UNITS);   // perform lenghty computations
                  }
                  catch (InterruptedException e){}
            
                  local = local + 1;
                  shared = local;
            }
      }
      

      public static void main(String[] args) {

            Thread th1 = new WrongCriticalSection();
            Thread th2 = new WrongCriticalSection();
            Thread th3 = new WrongCriticalSection();
            
            th1.start();
            th2.start();
            th3.start();
            
            try {
                  th1.join();
                  th2.join();
                  th3.join();
            }
            catch (InterruptedException e){}
            
            System.out.println("Shared is: " + shared);
      }
}

and

public class CriticalSection extends Thread {
      
      private static final int SLEEP_UNITS = 300;

      private static int shared = 0;
      
      private static Object lock = new Integer(0);  // any object is fine
      
      public void run() {
            int local;
            
            synchronized ( lock ) {      
                  local = shared;
                  try {
                        sleep(SLEEP_UNITS);   // perform lenghty computations
                  }
                  catch (InterruptedException e){}
            
                  local = local + 1;
                  shared = local;
            }
      }
      

      public static void main(String[] args) {

            Thread th1 = new CriticalSection();
            Thread th2 = new CriticalSection();
            Thread th3 = new CriticalSection();
            
            th1.start();
            th2.start();
            th3.start();
            
            try {
                  th1.join();
                  th2.join();
                  th3.join();
            }
            catch (InterruptedException e){}
            
            System.out.println("Shared is: " + shared);
      }
}
0
tiger0516
Asked:
tiger0516
3 Solutions
 
objectsCommented:
In the first class each thread is locking on different objects because each instance has its own set of member variables.

In the second class each thread locks on the *same* object because class (static) variables are shared by all instances of that class.
0
 
tiger0516Author Commented:
What is I say

private int shared = 0;

instead of

private static int shared = 0;

What shall I do to get the same result?

Thanks.

Feel confused by thread :(
0
 
tiger0516Author Commented:
>What is I say

Shall be What if I say
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Tommy BraasCommented:
The class WrongCriticalSection has a lock per instance of that class. That means that there is NO synchronization between threads.

The  class CriticalSection has one lock for the class. That means that all instances of this class shares the same lock, i.e. you achieve synchronization between threads in the critical section.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Basically, I am not sure why I need to make a static lock object.

I assume it is a continuation of your old question: >> http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21815874.html

You should've asked for clarification there instead of opening a new one :) Anyway, the reason for having it static is that all objects should share it. So if one object locks it, the other objects will not be able to enter that block of code which has acquired the lock. They will wait for the current object to finish execution, then they will acquire a lock and enter it.
0
 
Tommy BraasCommented:
;-D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now