Java--多线程知识(三)
一.线程安全
继续上次的线程知识:
我们知道,当线程获取了上锁了的对象之后,其他线程是无法重入锁的,这样可以保证我们的线程安全,接下来我将写一段代码来更加的展现:
public class Demo1 {public static void main(String[] args) {Object object = new Object();Thread t1 = new Thread(()->{synchronized(object){System.out.println("t1获取到锁");}System.out.println("t1出锁");});Thread t2 = new Thread(()->{synchronized(object){System.out.println("t2获取到锁");}System.out.println("t2出锁");});t1.start();t2.start();}
}
我们可以看到,t2是在t1运行结束之后才进行的,这就是锁的作用,
锁的特点:
1.锁是互斥的,当一个线程占用锁之后,其他线程是无法再次进入锁的
2.线程是无法被抢占的,前提是线程对同一个实例对象上锁。
线程安全问题:
1.线程的随即调度
2.多个线程修改同一个变量
3.修改操作不是原子
4.内存可见性
5.指令重排序
其中volitile关键字可以处理指令重排序。
当使用volatile关键字之后,每次写入volatile变量都会立即刷新到主内存。其次volatile关键字可以禁止系统的指令重排序优化。
二. notify 和 wait 以及 notifyAll
public class Demo3 {public static void main(String[] args) {Object object = new Object();Thread t1 = new Thread(()->{synchronized ( object){System.out.println("t1 wait 之前");try {object.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("t1 wait 之后");}});Thread t2 = new Thread(()->{synchronized (object){System.out.println("t2 notify 之前");object.notify();System.out.println("t2 notify 之后");}});t1.start();t2.start();
}
}
观察上述代码,object.wait() 使当前线程t1进入等待状态,并释放对object对象的锁。线程会一直等待直到其他线程调用该对象的notify()或notifyAll()方法唤醒它。
要注意,上述代码会有线程安全问题:
当 object.wait 之后,由于操作不是原子的,t1可能在等待通知之前,也就是释放锁之后,t2已经执行完 notify 代码,此时 t1就会永远收不到来自t2的通知,此时t1就会陷入无休止的等待,由于现在计算机运行速度快,基本很少出现上述问题,所以简单更改了一下线程运行的顺序:
例:
public class Demo3 {public static void main(String[] args) {Object object = new Object();Thread t2 = new Thread(()->{synchronized (object){System.out.println("t2 notify 之前");object.notify();System.out.println("t2 notify 之后");}});t2.start();try {t2.join();} catch (InterruptedException e) {throw new RuntimeException(e);}Thread t1 = new Thread(()->{synchronized ( object){System.out.println("t1 wait 之前");try {object.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("t1 wait 之后");}});t1.start();
}
}
此时就出现了线程安全问题,t1陷入了无休止的等待。
解决上述问题我们可以使用:状态标志+循环检查:
public class Demo4 {static boolean flg = false;public static void main(String[] args) {Object object = new Object();Thread t2 = new Thread(()->{synchronized (object){System.out.println("t2 notify 之前");object.notify();flg = true;System.out.println("t2 notify 之后");}});t2.start();try {t2.join();} catch (InterruptedException e) {throw new RuntimeException(e);}Thread t1 = new Thread(()->{synchronized ( object){System.out.println("t1 wait 之前");while (!flg){try {object.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("t1 wait 之后");}});t1.start();}
}
如上:我们用标志位来判断是否执行了 notify ,这样就可以避免由于 t2 执行完毕之后 t1没有收到 t2 通知的问题。
notify 跟 notifyAll 的区别就是 前者之后释放一个锁,后者则可以释放所有的锁:
public class Demo5 {public static void main(String[] args) {Object object = new Object();Thread t1 = new Thread(()->{synchronized ( object){System.out.println("t1 wait 之前");try {object.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("t1 wait 之后");}});Thread t2 = new Thread(()->{synchronized ( object){System.out.println("t2 wait 之前");try {object.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("t2 wait 之后");}});Thread t3 = new Thread(()->{synchronized ( object){System.out.println("t3 wait 之前");object.notify();System.out.println("t3 wait 之后");}});t1.start();t2.start();t3.start();}
}
我们创建了三个线程,其中让 t1 t2 线程去等待,让t3线程去释放:
我们发现运行结果出现了两种情况,t1 被释放之后 t2 并没有被释放,t2 被释放之后 t1 并没有被释放,这就是 notify 随即释放一个锁导致的,为了避免这种情况,我们可以对两把锁也实施 状态标志+循环检查,或者使用 notifyAll,
使用:notifyAll使用标志位:
使用标志位的代码:
public class Demo5 {static boolean flag = false;public static void main(String[] args) {Object object = new Object();Thread t1 = new Thread(()->{synchronized ( object){System.out.println("t1 wait 之前");while (!flag){try {object.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("t1 wait 之后");}});Thread t2 = new Thread(()->{synchronized ( object){System.out.println("t2 wait 之前");while(!flag){try {object.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("t2 wait 之后");}});Thread t3 = new Thread(()->{synchronized ( object){System.out.println("t3 wait 之前");object.notify();flag = true;System.out.println("t3 wait 之后");}});t2.start();t1.start();t3.start();}
}
wait 和 sleep 的区别:
wait 的存在就是为了被 notify 唤醒,而 sleep 是等待具体的时间,相比之前 wait 跟容易被控制,因为我们并不知道另一个线程什么时候结束,如果我们需要让 t1 线程等 t2 线程运行结束之后再运行,最好使用wait。