when we use synchronized keyword,what gets locked? [duplicate]

有些话、适合烂在心里 提交于 2019-12-10 17:49:00

问题


Question comes to my mind while reading about the concurrency issues in threads and dealing with it through synchronized keyword is that,when we use the term lock it is used for the object which contains the run method (or the job of the thread).But why we cannot use the term lock for the method only whose definition contains the synchronized keyword,as this keyword means that once a thread enters the method,then this thread can only be disturbed by the JVM scheduler after the completion of the method?

i am studying from head first java and there is line written over there that the "object is locked" and the reason given is again in a questioning manner that is,"what happen if we have two synchronized method".So i am confuse here that what is the surprising thing that can happen if only the method is locked?

Please forgive me if i asked a vague question and thanks in advance.


回答1:


When synchronizing, an object monitor is acquired by the thread. Since only one thread can acquire the monitor at a time, another thread trying to acquire it will be block. So in a sense nothing is really locked, except maybe a certain piece of code.




回答2:


Using synchronized keyword is an implicit equivalent to using a ReentrantLock, as the javadoc shows it in an example :

class X {
   private final ReentrantLock lock = new ReentrantLock();
   // ...

   public void m() {
     lock.lock();  // block until condition holds
     try {
       // ... method body
     } finally {
       lock.unlock()
     }
   }
 }

I don't know if it answers to you question maybe I should have juste made a comment.




回答3:


Addressing your question : what happens if we have two synchronized method?
Consider the following code snippet :-

public class Worker {

private Object lock1 = new Object();
private Object lock2 = new Object();

public void step1() {
    synchronized (lock1) {          
        //do something
    }
}

public void step2() {
    synchronized (lock2) {          
        //do something
    }
}

public void process() {
        step1();
        step2();        
}

public void main() {
    long start = System.currentTimeMillis();
    Thread t1 = new Thread(new Runnable() {

        public void run() {
            process();

        }
    });
    t1.start();
    Thread t2 = new Thread(new Runnable() {

        public void run() {
            process();

        }
    });
    t2.start();

        t1.join();
        t2.join();

    long end = System.currentTimeMillis();
    System.out.println("time taken " + (end - start));

}

}

In this code snippet we are using lock1 and lock2 as object locks,but what happens if we use this keyword instead of these locks(lock1 and lock2)?

  • time taken without using this keyword(using lock1,lock2 as in above) = T
  • time taken using this keyword in both synchronized blocks = 2T,

This time difference arises due to the fact that same object lock is used in both the synchronized blocks. If thread-1 would be accessing step1 method, step2 method will be blocked for thread-2. If method are of independent nature, different locks should be used.



来源:https://stackoverflow.com/questions/25232564/when-we-use-synchronized-keyword-what-gets-locked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!