生产者消费者简单模型-java

package cn.mldn.demo;

public class DeadLock {
	public static void main(String[] args) {
		Message msg = new Message();
		new Thread(new Producer(msg)).start();
		new Thread(new Consumer(msg)).start();
	}
}
class Message{
	private String title;
	private String content;
	boolean flag = true;  // 同步問題中,貢獻資源不僅互斥,而且有先后顺序;//生产者先生产
							//控制从buff中只能有一个进程消费或者生产;
	public synchronized void prod(String title, String content) {//生產 synchronized 互斥
		if(!flag) { //如果flag == false 则
			try {
				super.wait(); //生产进程等待
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.title = title;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.content = content;
		super.notify(); //将进程唤醒;
		flag = false;//标记 可以 此时可以进行消费了;
	}
	public synchronized String cons() {  //同上
		if(flag) {
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.notify();
		flag = true;
		return this.title + "  -  " + this.content;
		
	}
}
class Consumer implements Runnable{
	private Message msg ;
	public Consumer(Message msg) {
		this.msg = msg;
	}
	public void run() {
		for(int i = 0; i < 100; i++) {
			System.out.println(this.msg.cons());
		}
	}
}
class Producer implements Runnable{
	private Message msg ;
	public Producer(Message msg) {
		this.msg = msg;
	}
	public void run() {
		for(int i = 0 ; i < 100; i++) {
			if(i % 2 == 0) {
				this.msg.prod("生產1", "小東西聽別緻");
				//System.out.println("已生產");
			}
			else {
				this.msg.prod("生產2", "小東西dadada別緻");
			}
		}
	}
}

多线程数字加减问题:

public class MyThredTest {
	public static void main(String[] args)  throws Exception{
		Resource re = new Resource();
		SubThread st = new SubThread(re);
		AddThread at = new AddThread(re);
		new Thread(at,"加线程 - A").start();
		new Thread(at,"加线程 - B").start();
		
		new Thread(st,"减线程 - x").start();
		new Thread(st,"减线程 - y").start();
	}
}
class Resource{
	
	private volatile int num = 0;
	private boolean flag = true;
	public synchronized void addThread() throws Exception{
		while(this.flag == false) {
			super.wait();
		}
		Thread.sleep(10);
		this.num++;
		System.out.println("加法操作" + Thread.currentThread().getName() + "num = " + this.num);
		this.flag = false;
		super.notifyAll();
	}
	public synchronized void subThread() throws Exception{
		while(this.flag == true) {
			super.wait();
		}
		Thread.sleep(20);
		this.num--;
		System.out.println("减法操作" + Thread.currentThread().getName() + "num = " + this.num);
		this.flag = true;
		super.notifyAll();
	}
}

class AddThread implements Runnable{
	private Resource re;
	public AddThread(Resource re) {
		this.re = re;
	}
	public void run() {
		for(int i = 0; i < 100; i++) {
			try {
				this.re.addThread();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
class SubThread implements Runnable{
	private Resource re;
	public SubThread(Resource re) {
		this.re = re;
	}
	public void run() {
		for(int i = 0; i < 100; i++) {
			try {
				this.re.subThread();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

此代码中有一个问题,数字num会出现一直减一直加的问题,是因为在进行线程同步时,多个线程都通过wait()方法进入阻塞状态后,当一个线程通过notifyAll唤醒其余的线程后,阻塞状态的线程就会直接继续往下执行,所以无法实现线程同步。解决思路是线程从阻塞状态被唤醒后必须再次检验flag的值。
 

发布了97 篇原创文章 · 获赞 3 · 访问量 9420

猜你喜欢

转载自blog.csdn.net/foolishpichao/article/details/103442778
今日推荐