0

The Class X have two methods: test and test1.

I've created two threads: t1 and t2. Thread t1 is accessing test method and t2 is accessing test1 method of same object. When t1 is accessing test method which synchronized it acquires lock on object.

Will t2 be able to access test1 method on same object? Why it is able to access this method if t1 has a lock on it?

If I'm executing the following code

            X x = new X();
           new MyThread(x).start(); // It execute test() method
       new MyThread1(x).start();// It execute test1() method





class X 
{
    String a  = "varsha";
    public synchronized void test ()
    {
        try 
        {
            Thread.sleep (6000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace ();
        }
    } 
    public void test1 ()
    {
        synchronized (a)
        {
        }
    } 
}
1
  • 1
    Please, format your code. This is horrible!!! Commented Mar 14, 2013 at 7:17

5 Answers 5

3

You have two different locks:

  • test() locks this;
  • test1() locks this.a.

The two locks are completely independent and thus the two methods can be called at the same time.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code is equivalent to the following:

class X 
{
    String a  = "varsha";
    public void test ()
    {
        synchronized (this)
        {
            try
            {
                Thread.sleep (6000);
            }
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
        }
    } 

    public void test1 ()
    {
        synchronized(a)
        {
        }
    } 
}

So these methods are synchronizing at different objects (this versus a) and thus can be executed concurrently without locking each other.

Note, that I replaced Thread.currentThread ().sleep (6000) with Thread.sleep (6000) because method sleep is static and thus you don't need any instance of Thread in order to use it.

Comments

0
class X {

  String a  = "varsha";

   public synchronized void test(){

   try {             
                  //if you are modifying the instance variable here
                  // then the test1() synchronized block will 
                 //not be given lock permission to t2 thread
                 // synchronization is for thread safety.

                 // In your example you are not modifying the instance variable.

   } catch (InterruptedException e) {
    e.printStackTrace();
   }
 } 
 public  void test1(){
   synchronized(a){
   }
 } 
}

Comments

0

Here is whats actually happening.
Every object in Java has a "monitor lock", In this case object "x".

There are two threads (MyThread and MyThread1) trying to acquire this lock in the following sequence -

Imagine there is a queue – MyThread is in front of MyThread1 in this queue because you have started MyThread first followed by MyThread1.

MyThread acquires the lock first and it starts executing, you have invoked the sleep() method on it. This will change the state of MyThread from "execution state" to "waiting state" and then to "ready state", it will release the lock at this moment since it is not in execution state. At this point MyThread1 is ahead in the queue and it acquires the lock and starts executing.

This is in a way similar to the concept of "Context Switch". Refer book - Operating System Internals and Design.

Comments

-1

When you mark a method as synchronized, it locks the object for that method; meaning no other thread can access that PARTICULAR method for that object. In your case no other thread can access the test method; but of course test1 method can be accessed.

1 Comment

I suggest writing a simple test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.