wait和notify简单应用1

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
//一个线程去读取List的长度,如果List为空的时候,线程就wait,如果List中有值了,就notify去获取它的长度
public class ListAwit {
	private static List<Integer> list = new ArrayList<Integer>();
	final static Random rand = new Random();
	public static void main(String[] args) {
		
		final ListAwit la = new ListAwit();
		
		new Thread(new Runnable(){
			public void run() {
				while(true){
					if(list.size() > 0){
						synchronized(la){
							la.notify();
						}
					}
					sleep();
				}
			}})
		.start();
		while(true){
			try {
				System.out.println(Thread.currentThread().getName()+"等待.........");
				synchronized(la){
					la.wait();
				}
				sleep();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"##长度:"+list.size());
			System.out.println(list);
		}
	}
	private static void sleep(){
		try {
			int randint = rand.nextInt(2000);
			Thread.sleep(randint);
			list.add(randint);
			if(rand.nextInt(10)==5){
				list.clear();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 运行结果:
	    main等待.........
		main##长度:4
		[289, 887, 41, 1192]
		main等待.........
		main##长度:8
		[289, 887, 41, 1192, 1322, 238, 641, 1608]
		main等待.........
	 */
}


实现一种类似信号控制的程序

public class SynchronizedMutex {
	private Thread curOwner = null;
	public synchronized void acquire() throws InterruptedException{
		System.out.println("当前线程:"+Thread.currentThread().getName());
		while(curOwner != null){
			this.wait();
		}
		this.curOwner = Thread.currentThread();
	}
	public synchronized void release() throws InterruptedException{
		System.out.println("当前线程:"+Thread.currentThread().getName());
		while(curOwner == Thread.currentThread()){
			this.curOwner = null;
			this.notify();
		}
	}
}

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class MutexTest{
//	private Semaphore mutex = new Semaphore(1);
	private SynchronizedMutex mutex = new SynchronizedMutex();
	public static void main(String[] args) {
		final MutexTest test = new MutexTest();
		
		Runnable task = new Runnable(){
			@Override
			public void run() {
				while(true){
					int i = new Random().nextInt(3);
					switch(i){
						case 0:test.add();
						case 1:test.add1();
						case 2:test.del();
						default:test.add();
					}
				}
			}
		};
		ExecutorService exec = Executors.newCachedThreadPool();
		for(int i=0; i<5; i++){
			exec.execute(task);
		}
	}
	private static int value;
	public void add(){
		try {
			mutex.acquire();
			TimeUnit.SECONDS.sleep(1);
			System.out.println(Thread.currentThread().getName()+"增一:"+ (++value));
			mutex.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public void add1(){
		try {
			mutex.acquire();
			TimeUnit.SECONDS.sleep(1);
			value = value + 2;
			System.out.println(Thread.currentThread().getName()+"增二:"+ value);
			mutex.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public void del(){
		try {
			mutex.acquire();
			TimeUnit.SECONDS.sleep(1);
			System.out.println(Thread.currentThread().getName()+"减一:"+ (--value));
			mutex.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 运行结果:
	 *  当前线程:pool-1-thread-2
		当前线程:pool-1-thread-4
		当前线程:pool-1-thread-5
		当前线程:pool-1-thread-3
		当前线程:pool-1-thread-1
		pool-1-thread-2减一:-1
		当前线程:pool-1-thread-2
		当前线程:pool-1-thread-2
		pool-1-thread-4增一:0
		当前线程:pool-1-thread-4
		当前线程:pool-1-thread-4
		pool-1-thread-4增二:2
		当前线程:pool-1-thread-4
		当前线程:pool-1-thread-4
		pool-1-thread-4减一:1
		当前线程:pool-1-thread-4
		当前线程:pool-1-thread-4
		pool-1-thread-4增一:2
		当前线程:pool-1-thread-4
		当前线程:pool-1-thread-4
		pool-1-thread-4减一:1
		当前线程:pool-1-thread-4
		当前线程:pool-1-thread-4
	 */
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值