wait 和notify的使用

注意:

1 使用wait和notify实现线程间的通讯。

2 wait和notify必须配合synchronize关键字一起使用。

3 wait方法--》释放锁,notify方法--》不释放锁


-----------------------------------------------------不使用wait和notify------------最古老的----------------------------------------------------------------

public class ListAdd1 {


private volatile static List list = new ArrayList();


public void add() {
list.add("bjsxt");
}


public int size() {
return list.size();
}


public static void main(String[] args) {


final ListAdd1 list1 = new ListAdd1();


Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
list1.add();
System.out.println("当前线程:" + Thread.currentThread().getName() + "添加了一个元素..");
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t1");


Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (list1.size() == 5) {
System.out.println("当前线程收到通知:" + Thread.currentThread().getName() + " list size = 5 线程停止..");
throw new RuntimeException();
}
}
}
}, "t2");


t1.start();
t2.start();
}
}


-----------------------------------------------------使用wait和notify一起使用的实例------------改进----------------------------------------------------

缺点:不能实时,因为一个锁了只能等另一个锁释放了才能进行另一个线程



/**
 * wait notfiy 方法,wait释放锁,notfiy不释放锁
 *
 */
public class ListAdd2 {
private volatile static List list = new ArrayList();


public void add() {
list.add("bjsxt");
}


public int size() {
return list.size();
}


public static void main(String[] args) throws InterruptedException {


final ListAdd2 list2 = new ListAdd2();


// 1 实例化出来一个 lock
// 当使用wait 和 notify 的时候 , 一定要配合着synchronized关键字去使用
final Object lock = new Object();


// final CountDownLatch countDownLatch = new CountDownLatch(1);


Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
synchronized (lock) {
for (int i = 0; i < 10; i++) {
list2.add();
System.out.println("当前线程:" + Thread.currentThread().getName() + "添加了一个元素..");
Thread.sleep(500);
if (list2.size() == 5) {
System.out.println("已经发出通知..");
// countDownLatch.countDown();
lock.notify();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}


}
}, "t1");


Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
if (list2.size() != 5) {
try {
System.out.println("t2进入等待中...");
lock.wait(); //释放锁 (处于等待状态)
// countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("当前线程:" + Thread.currentThread().getName() + "收到通知线程停止..");
throw new RuntimeException();
}
}
}, "t2");


t2.start(); // 必须先启动 (否则t1一旦获得锁t2就会一直阻塞 就处于等待状态永远获取不到10)
Thread.sleep(500);
t1.start(); //一开始t2是释放锁状态的,t1启动后就获取到整个锁,直到t1结束完t2才能获取到锁执行 RuntimeException()


}


}


-----------------------------------------------------使用CountDownLatch 实现wait和notify的功能------------升级版-------------------------------

优点:具有实时性,代码简洁



import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
/**
 * wait notfiy 方法,wait释放锁,notfiy不释放锁
 * @author alienware
 *
 */
public class ListAdd2 {
private volatile static List list = new ArrayList();

public void add(){
list.add("bjsxt");
}
public int size(){
return list.size();
}

public static void main(String[] args) {

final ListAdd2 list2 = new ListAdd2();

// 1 实例化出来一个 lock
// 当使用wait 和 notify 的时候 , 一定要配合着synchronized关键字去使用
//final Object lock = new Object();

final CountDownLatch countDownLatch = new CountDownLatch(1);

Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
//synchronized (lock) {
for(int i = 0; i <10; i++){
list2.add();
System.out.println("当前线程:" + Thread.currentThread().getName() + "添加了一个元素..");
Thread.sleep(500);
if(list2.size() == 5){
System.out.println("已经发出通知..");
countDownLatch.countDown(); //调用一次countDown()就减一,直到0
//lock.notify();
}
}
//}
} catch (InterruptedException e) {
e.printStackTrace();
}


}
}, "t1");

Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
//synchronized (lock) {
if(list2.size() != 5){
try {
//System.out.println("t2进入...");
//lock.wait();
countDownLatch.await(); //等于0前一直处于等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("当前线程:" + Thread.currentThread().getName() + "收到通知线程停止..");
throw new RuntimeException();
//}
}
}, "t2");

t2.start();
t1.start();

}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值