多线程,奇数偶数,生产者,消费者

一、奇数偶数
1.Date

public class Date {

	private int num;

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public Date(int num) {
		super();
		this.num = num;
	}
	
	public synchronized void Even() {
		this.notifyAll();
		if(num%2==0) {
			System.out.println("Even"+Thread.currentThread().getName()+"-->"+(num++));
			
			try {
				Thread.sleep(1000);
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public synchronized void Odd() {
		this.notifyAll();
		if(num%2!=0) {
			System.out.println("Odd"+Thread.currentThread().getName()+"-->"+(num++));
			try {
				Thread.sleep(1000);
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

2.AThread

public class AThread implements Runnable {

	private Date date;
	
	public AThread(Date date) {
		super();
		this.date = date;
	}

	@Override
	public void run() {
		while(true) {
		date.Even();
	}
	}
}

3.BThread

public class BThread implements Runnable {

	private Date date;
	
	public BThread(Date date) {
		super();
		this.date = date;
	}

	@Override
	public void run() {
		while(true) {
		date.Odd();
		}
	}

}

4.Test

public class Test {

	public static void main(String[] args) {
		Date date=new Date(0);
		
		AThread a=new AThread(date);
		BThread b=new BThread(date);
		
		Thread t1=new Thread(a);
		Thread t2=new Thread(b);
		
		t1.start();
		t2.start();
	}
}

二、生产者,消费者
1.Food

package com.pro.food;

public class Food {

	private String name;
	private String type;
	private int j;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getJ() {
		return j;
	}
	public void setJ(int j) {
		this.j = j;
	}
	public Food(int j) {
		super();
		this.j = j;
	}
	
}

2.Product

package com.pro.food;

public class Product implements Runnable {

	private Food food;
	private int i=0;
	
	public Product(Food food) {
		super();
		this.food = food;
	}

	@Override
	public void run() {
		while(true) {
			synchronized(food) {
				food.notifyAll();
				if(food.getJ()==0) {
					if(i%2==0) {
						food.setType("炒饭");
						food.setName("蛋炒饭");
						food.setJ(1);
					}else if(i%2!=0) {
						food.setType("包子");
						food.setName("肉包");
						food.setJ(1);
					}
					i++;
					System.out.println("生产了"+food.getType()+"-"+food.getName());
				}else if(food.getJ()==1) {
					try {
						Thread.sleep(1000);
						food.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}

	}

}

3.Consumer

package com.pro.food;

public class Consumer implements Runnable {

	private Food food;
	
	public Consumer(Food food) {
		super();
		this.food = food;
	}

	@Override
	public void run() {
		while(true) {
			synchronized (food) {
				food.notifyAll();
				if(food.getJ()==1) {
					System.out.println("拿到了"+food.getType()+"-"+food.getName());
					food.setJ(0);
				}else if(food.getJ()==0) {
					try {
						Thread.sleep(1000);
						food.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}

	}

}

4.Test

package com.pro.food;

public class Test {

	public static void main(String[] args) {
		Food food=new Food(0);
		Product pro=new Product(food);
		Consumer con=new Consumer(food);
		
		Thread t1=new Thread(pro);
		Thread t2=new Thread(con);
		
		t1.start();
		t2.start();
	}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值