Java的锁分为两种:
-
对象锁(又称实例锁,
synchronized
):该锁针对的是该实例对象(当前对象)。synchronized
是对类的当前实例(当前对象)进行加锁,防止其他线程同时访问该类的该实例的所有synchronized块,注意这里是“类的当前实例”, 类的两个不同实例就没有这种约束了。
每个对象都有一个锁,且是唯一的。 -
类锁(又称全局锁,
static synchronized
):该锁针对的是类,无论实例出多少个对象,那么线程依然共享该锁。static synchronized
是限制多线程中该类的所有实例同时访问该类所对应的代码块。(实例.fun
实际上相当于class.fun
)
下面来进行几种情况的测试:一下都是用来测试锁的范围
1、两个静态方法都加锁
package test2;
public class Test2 {
public synchronized void add(){
System.out.println("1 :) ");
t();
}
public void add1(){
System.out.println("2 :( ");
t();
}
public void t(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、两个非静态方法都加锁
package test2;
public class Test1 extends Thread{
private int flag;
private Test2 t;
public Test1(int f , Test2 t2){
flag = f;
t = t2;
}
public void run(){
if(flag == 1)
try {
// Test2.add();
t.add();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(flag == 2)
try {
t.add1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Test2 t = new Test2();
Test1 t1 = new Test1(1,t);
Test1 t2 = new Test1(2,t);
t1.start();
t2.start();
}
}
class Test2 {
public synchronized void add(){
System.out.println("1 :) "+System.currentTimeMillis());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void add1(){
System.out.println("2 :( "+System.currentTimeMillis());
}
}
结果:此时会锁住加锁的另外一个对象