多線程之消費者與生產者1.5前後實例

1、 消費者與生產者(多個線程操作一個run方法時)1.5以前的版本

class Resource {

	private String name;
	private int count = 1;
	private boolean flag = false;

	public synchronized void set(String name) {

		while (flag)
			try {
				wait();
			} catch (Exception e) {
			}
		this.name = name + "--------" + count++;
		System.out.println("----生產者:" + this.name);
		flag = true;
		this.notifyAll();

	}

	public synchronized void get() {

		while (!flag)
			try {
				wait();
			} catch (Exception e) {
			}
		System.out.println("消費者" + this.name);
		flag = false;
		notifyAll();
	}
}

class Producer implements Runnable {

	private Resource r;

	Producer(Resource r) {

		this.r = r;
	}

	public void run() {

		while (true) {

			r.set("商品");
		}
	}
}

class Consumer implements Runnable {

	private Resource r;

	Consumer(Resource r) {

		this.r = r;
	}

	public void run() {

		while (true)
			r.get();
	}
}

class ThreadDemo {

	public static void main(String[] args) {

		Resource r = new Resource();

		Producer p = new Producer(r);
		Consumer c = new Consumer(r);

		Thread t1 = new Thread(p);
		Thread t2 = new Thread(p);
		Thread t3 = new Thread(p);
		Thread t4 = new Thread(c);

		t1.start();
		t2.start();
		t3.start();
		t4.start();

	}
}

2、 消費者與生產者(多個線程操作一個run方法時)1.5以後的版本

import java.util.concurrent.locks.*;

class Resource {

	private String name;
	private int count = 1;
	private boolean flag = false;

	private Lock lock = new ReentrantLock();

	private Condition condition_pro = lock.newCondition();
	private Condition condition_con = lock.newCondition();

	public void set(String name) throws InterruptedException {

		lock.lock();
		try {

			while (flag)
				condition_pro.await();
			this.name = name + "--------" + count++;

			System.out.println(Thread.currentThread().getName() + "----生產者:"
					+ this.name);
			flag = true;
			condition_con.signal();

		} finally {

			lock.unlock();
		}

	}

	public void get() throws InterruptedException {

		lock.lock();

		try {

			while (!flag)
				condition_con.await();
			System.out.println(Thread.currentThread().getName() + "消費者"
					+ this.name);
			flag = false;
			condition_pro.signal();

		} finally {

			lock.unlock();
		}
	}
}

class Producer implements Runnable {

	private Resource r;

	Producer(Resource r) {

		this.r = r;
	}

	public void run() {

		while (true) {

			try {
				r.set("商品");
			} catch (Exception e) {

			}
		}
	}
}

class Consumer implements Runnable {

	private Resource r;

	Consumer(Resource r) {

		this.r = r;
	}

	public void run() {

		while (true) {
			try {
				r.get();
			} catch (Exception e) {

			}
		}
	}
}

class ThreadDemo {

	public static void main(String[] args) {

		Resource r = new Resource();

		Producer p = new Producer(r);
		Consumer c = new Consumer(r);

		Thread t1 = new Thread(p);
		Thread t2 = new Thread(p);
		Thread t3 = new Thread(c);
		Thread t4 = new Thread(c);

		t1.start();
		t2.start();
		t3.start();
		t4.start();

	}
}


發佈了67 篇原創文章 · 獲贊 8 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章