Synchronized 关键字一般在 多线程访问同一代码块时 保证这段代码执行的原子性。
有以下几个方面需要注意下
1, synchronized 锁的有哪些?
① 对象(临界资源对象,比如 Object o = new Object() 的o 就是临界资源对象)
② this (调用当前方法的对象 )
3 类对象(也就相当于 比如Test.class 这样的 ,一般是有static修饰的)
下面是一段示例代码:
public class TestSync {
private int count = 0;
private static int staticCount = 0;
private Object object = new Object();
private static Object staticObject = new Object();
/**
* 1, testSync1和testSync2 这种形式的 也成为 锁的是代码块,而testSync3锁的是整个方法
* 2, 锁this 和 锁method 差不多
*/
public void testSync1() {
synchronized (object) { //锁的是临界资源对象 object
System.out.println(Thread.currentThread().getName()+" count "+ count++);
}
}
//锁的是this,也就是调用当前方法的对象 比如 TestSync t = new TestSync(); t.testSync2(); 这个t
public void testSync2() {
synchronized (this) {
System.out.println(Thread.currentThread().getName()+" count "+ count++);
}
}
// 锁的是方法 , 这个级别较重, 一般不建议使用
public synchronized void testSync3() {
System.out.println(Thread.currentThread().getName()+" count "+ count++);
}
//这种锁的 是 TestSync.class
public static synchronized void testSync4() {
System.out.println(Thread.currentThread().getName()+" staticCount "+ staticCount++);
}
public static void testSync5() {
synchronized (staticObject) {
System.out.println(Thread.currentThread().getName()+" staticCount "+ staticCount++);
}
}
}
2,同步方法 只保证当前方法的原子性,不保证多个业务方法之间的互相访问的原子性
看一段示例代码
public class TestSync {
private int i = 0;
public void set(int i) {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.i = i;
}
public int get() {
return this.i;
}
//运行后 先后显示 0 100
public static void main(String[] args) {
TestSync t = new TestSync();
new Thread(new Runnable() {
@Override
public void run() {
t.set(100);
}
}).start();
System.out.println(t.get()); //这里为0
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(t.get()); //这里为100
}
}
3 锁的重入
条件:
① 同一个线程 ②多次调用同步代码 3 锁定同一个对象
下面看一个段代码
public class TestSync {
synchronized void m1(){ // 锁this
System.out.println("m1 start");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
m2();
System.out.println("m1 end");
}
synchronized void m2(){ // 锁this
System.out.println("m2 start");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m2 end");
}
public static void main(String[] args) {
//先后打印如下 m1 start,m2 start,m2 end,m1 end
new TestSync().m1(); //同一个线程 多次调用同步代码(这里为m1() 和 m2() ), 锁定的都是this
}
}
当然注意一个点 : 子类同步方法 覆盖 父类同步方法,可以指定调用父类的同步方法,这种相当于锁的重入
示例代码:
public class TestSync {
synchronized void m(){
System.out.println("Super Class m start");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Super Class m end");
}
public static void main(String[] args) {
//这里依次打印Sub Class m start,Super Class m start,Super Class m end,Sub Class m end
new SubTestSync().m();
}
}
class SubTestSync extends TestSync{ //继承父类
synchronized void m(){ //重写父类同步方法
System.out.println("Sub Class m start");
super.m();
System.out.println("Sub Class m end");
}
}
4 锁与异常
当同步方法发生异常的时候,自动释放锁资源,不会影响其他线程的执行
比如当 i/0 时 发生异常了,程序会自动朝后执行
5 在定义同步代码时,不要使用常量作为锁对象
public class TestSync {
String s1 = "hello";
String s2 = "hello"; // new String("hello");
Integer i1 = 1;
Integer i2 = 2;
void m1() {
synchronized (s1) { // 可换成 i1
System.out.println("m1()");
while (true) {
}
}
}
void m2() {
synchronized (s2) { // 换成 i2
System.out.println("m2()");
while (true) {
}
}
}
public static void main(String[] args) {
final TestSync t = new TestSync();
new Thread(new Runnable() {
@Override
public void run() {
t.m1();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
t.m2();
}
}).start();
// 运行时 可以发现 只有m1() 打印出来了,m2() 一直不打印,,如果 s2 = new String("hello"); 则可以打印
// Integer 的值在 -128~127之间的数字 只打印m1()
// 常量所为锁对象会出现问题
}
}
6 锁对象变更问题
同步代码一旦加锁后,那么会有一个临时的锁引用执行锁对象,和真实的引用无直接关联。
在锁未释放之前,修改锁对象引用,不会影响同步代码的执行。
public class TestSync {
void m() {
System.out.println(Thread.currentThread().getName() + " start");
synchronized (o) {
while (true) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " - " + o);
}
}
}
public static void main(String[] args) {
final TestSync t = new TestSync();
new Thread(new Runnable() {
@Override
public void run() {
t.m();
}
}, "thread1").start();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
t.m();
}
}, "thread2");
t.o = new Object(); // 这个 改变了
thread2.start();
}
}
网友评论