1、对象锁(synchronized修饰方法或代码块),分两种情况,一种是synchronized method和synchronized(this)代码块,锁定的都是当前对象,两者是同一把锁,一种是synchronized(任意Object对象),代码如下:
public class ObjectLock {
public void method1(){
synchronized (this) { //对象锁
try {
System.out.println("start method1..");
Thread.sleep(2000);
System.out.println("end method1..");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void method2(){
try {
System.out.println("start method2 ..");
Thread.sleep(2000);
System.out.println("end method2 ..");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private Object lock = new Object();
public void method3(){ //任何对象锁
synchronized (lock) {
try {
System.out.println("start method3..");
Thread.sleep(2000);
System.out.println("end method3..");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
final ObjectLock objLock = new ObjectLock();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
objLock.method1();
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
objLock.method3();
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
objLock.method2();
}
});
t1.start();
t2.start();
t3.start();
}
}
t1和t2线程争抢的是同一把锁,t1访问method1之后释放当前锁,t2才可以访问method2,而t3访问的method3使用的是另外一把自定义的对象锁,所以,t1和t2同步,和t3异步。
输出结果 为:
start method3..
start method1..
end method3..
end method1..
start method2 ..
end method2 ..
2、类锁(synchronized 修饰静态的方法或代码块)
于一个class不论被实例化多少次,其中的静态方法和静态变量在内存中都只有一份。所以,一旦一个静态的方法被申明为synchronized。此类所有的实例化对象在调用此方法,共用同一把锁,称之为类锁。
java类可能会有多个对象,但是只有1个Class对象,也就是说类的不同实例之间共享该类的Class对象。Class对象其实也仅仅是1个java对象,只不过有点特殊而已。由于每个java对象都有1个互斥锁,而类的静态方法是需要Class对象。所以所谓的类锁,不过是Class对象的锁而已。获取类的Class对象有好几种,synchronized(类名.class)的方式
public class Test
{
// 类锁:形式1
public static synchronized void Method1()
{
System.out.println("我是类锁一号");
try
{
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
// 类锁:形式2
public void Method2()
{
synchronized (Test.class)
{
System.out.println("我是类锁二号");
try
{
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}