假设一个Class如下
public Class A
{
...
public synchronized void method1 {
...
} public synchronized void method2 {
...
}}A obj = new A();如果有1个线程1正在调用 obj 的method1, 是不是另一个线程2就连obj的method2都不能调用?
谢谢
public Class A
{
...
public synchronized void method1 {
...
} public synchronized void method2 {
...
}}A obj = new A();如果有1个线程1正在调用 obj 的method1, 是不是另一个线程2就连obj的method2都不能调用?
谢谢
A obj = new A();
obj.method1();
因为obj访问了method1,那么此时obj这个对象持有“锁”,所以,如果obj想去访问method2,只能等待method1方法执行完毕。
但是如果是这样的话
A obj1 = new A();
A obj2 = new A();
那么此时是两个对象,如果obj1访问了method1,obj2既可以访问method1,也可以访问method2,因为现在是两个对象,如果,obj1调用method1那么obj1被上锁,但是和obj2没有任何关系,所以上锁的仅仅是访问synchronized方法的对象,一旦该对象持有锁(被上锁),那么这个对象就不能再访问A类中的任何Synchronized方法。注意是:Synchronized方法,非Synchronized方法还是可以访问的。
public Class A
{
...
public synchronized void method1 {
...
} public synchronized void method2 {
...
}}
相当于
public Class A
{
...
public void method1 {
synchronized (this) {
...
}
} public synchronized void method2 {
synchronized (this) {
...
}
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;/**
* synchronized
*
* @Author luochengor
* @Date Nov 25, 2011
* @Email [email protected]
*/
public class Synchronized { /**
* @param args
*/
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
D d = new D();
exec.execute(new ThreadA(d));
exec.execute(new ThreadB(d));
}}class D {
public synchronized void method1() {
try {
System.out.println("D.method1()");
System.out.println("method-1 start to sleep.");
TimeUnit.SECONDS.sleep(1);
System.out.println("method-1 wake up.");
} catch (InterruptedException e) {
System.out.println("method-1 is interrupted.");
}
} public synchronized void method2() {
try {
System.out.println("D.method2()");
System.out.println("method-2 start to sleep.");
TimeUnit.SECONDS.sleep(3);
System.out.println("method-2 wake up.");
} catch (InterruptedException e) {
System.out.println("method-2 is interrupted.");
}
}
public void unSynchronized() {
System.out.println("This is unSynchronized method.");
}
}class ThreadA implements Runnable {
private D d; public ThreadA(D d) {
this.d = d;
} @Override
public void run() {
while (!Thread.interrupted()) {
try {
System.out.println("ThreadA.run start doing method1().");
d.unSynchronized();
d.method1();
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("ThreadA.run is interrupted.");
}
}
}
}class ThreadB implements Runnable {
private D d; public ThreadB(D d) {
this.d = d;
} @Override
public void run() {
while (!Thread.interrupted()) {
try {
System.out.println("ThreadB.run start doing method2().");
d.unSynchronized();
d.method2();
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("ThreadB.run is interrupted.");
}
}
}
}运行结果:
ThreadA.run start doing method1().
This is unSynchronized method.
D.method1()
method-1 start to sleep.
ThreadB.run start doing method2().
This is unSynchronized method.
method-1 wake up.
D.method2()
method-2 start to sleep.
ThreadA.run start doing method1().
This is unSynchronized method.
method-2 wake up.
D.method1()
method-1 start to sleep.
method-1 wake up.
ThreadB.run start doing method2().
This is unSynchronized method.
D.method2()
method-2 start to sleep.
ThreadA.run start doing method1().
This is unSynchronized method.
method-2 wake up.
D.method1()
method-1 start to sleep.
method-1 wake up.
ThreadB.run start doing method2().
This is unSynchronized method.
D.method2()
method-2 start to sleep.
ThreadA.run start doing method1().
This is unSynchronized method.
method-2 wake up.
D.method1()
method-1 start to sleep.