方式1 缺点线程t2一直在死循环,浪费CPU
public class T1 {
volatile List list = new ArrayList();
public void add (int i){
list.add(i);
}
public int getSize(){
return list.size();
}
public static void main(String[] args) {
T1 t1 = new T1();
new Thread(() -> {
for (int i =0;i<9;i++ ){
t1.add(i);
System.out.println("add"+i);
}
},"t1").start();
new Thread(() -> {
while (true){
if(t1.getSize() == 5){
System.out.println("t2 结束");
}
}
},"t2").start();
}
}
运行结果:
add0
add1
add2
add3
add4
t2 结束
add5
add6
add7
add8
方式2,使用wait和notify实现
package com.zheng.nie.t01;
import java.util.ArrayList;
import java.util.List;
/**
* @author: niezheng1
* @Date: 2019/1/9 17:24
*
* 必须先让t2先进行启动 使用wait 和 notify 进行相互通讯,wait会释放锁,notify不会释放锁
*/
public class T2 {
volatile List list = new ArrayList();
public void add (int i){
list.add(i);
}
public int getSize(){
return list.size();
}
public static void main(String[] args) {
T2 t2 = new T2();
Object lock = new Object();
new Thread(() -> {
synchronized(lock){
System.out.println("t2 启动");
if(t2.getSize() != 5){
try {
/**会释放锁*/
lock.wait();
System.out.println("t2 结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.notify();
}
},"t2").start();
new Thread(() -> {
synchronized (lock){
System.out.println("t1 启动");
for (int i=0;i<9;i++){
t2.add(i);
System.out.println("add"+i);
if(t2.getSize() == 5){
/**不会释放锁*/
lock.notify();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
}
运行结果:
t2 启动
t1 启动
add0
add1
add2
add3
add4
t2 结束
add5
add6
add7
add8
方式3
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* @author: niezheng1
* @Date: 2019/1/9 18:20
* 使用CountDownLatch 代替wait notify 好处是通讯方式简单,不涉及锁定 Count 值为0时当前线程继续执行,
*/
public class T3 {
volatile List list = new ArrayList();
public void add(int i){
list.add(i);
}
public int getSize(){
return list.size();
}
public static void main(String[] args) {
T3 t = new T3();
CountDownLatch countDownLatch = new CountDownLatch(1);
new Thread(() -> {
System.out.println("t2 start");
if(t.getSize() != 5){
try {
countDownLatch.await();
System.out.println("t2 end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"t2").start();
new Thread(()->{
System.out.println("t1 start");
for (int i = 0;i<9;i++){
t.add(i);
System.out.println("add"+ i);
if(t.getSize() == 5){
System.out.println("countdown is open");
countDownLatch.countDown();
}
}
System.out.println("t1 end");
},"t1").start();
}
}
网友评论