62,621
社区成员
发帖
与我相关
我的任务
分享
package PExClass;
import java.lang.Thread;
public class CPar {
public static void main(String[] args) {
Producer producer = new Producer();
producer.start();
Concumer concumer = new Concumer();
concumer.start();
}
}
class Product {
static int num=0;
static boolean bWait=false;
final static int MAX = 10;
}
class Producer extends Thread {
public void run() {
while (true) {
produce();
}
}
public synchronized void produce() {
try {
if (Product.num == Product.MAX) {
Product.bWait=true;
wait();
} else {
int num=++Product.num;
System.out.println(Thread.currentThread().getName()
+ ":produce:" + num);
if(Product.bWait){
Product.bWait=false;
notify();
}
}
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
class Concumer extends Thread {
public void run() {
while (true) {
consume();
}
}
public synchronized void consume() {
try {
if (Product.num == 0) {
Product.bWait=true;
wait();
} else {
int num=--Product.num;
System.out.println(Thread.currentThread().getName()
+ ":consume:" + num);
if(Product.bWait){
Product.bWait=false;
notify();
}
}
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
package cn.xiaozejun.xiancheng.ch005;
public class Product {
private int num = 0;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
//这个方法用来生产产品
public synchronized void produce()
{
num++;
System.out.println("生产了一个产品...");
this.notifyAll();
}
//这个方法用来销售产品
public synchronized void sale()
{
num--;
System.out.println("销售了一个产品...");
this.notifyAll();
}
//这个方法用来根据当前的产品的数量判断是否应该生产产品
public synchronized void waitForProduce() throws Exception
{
while(num<1)
{
System.out.println("等待生产...");
this.wait();
}
}
//这个方法用来根据当前的产品的数量判断是否应该销售产品
public synchronized void waitForSale()throws Exception
{
while(num>10)
{
System.out.println("等待销售...");
this.wait();
}
}
}
package cn.xiaozejun.xiancheng.ch005;
public class Procduce implements Runnable{
Product product = null;
public Procduce(Product product)
{
this.product = product;
}
public void run(){
try {
while(true)
{
Thread.sleep(100);
product.waitForSale();
System.out.println("正在准备生产...");
product.produce();
System.out.println("现在商品的数量:"+product.getNum());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package cn.xiaozejun.xiancheng.ch005;
public class Sale implements Runnable{
Product product = null;
public Sale(Product product)
{
this.product = product;
}
public void run() {
try {
while(true)
{
Thread.sleep(500);
product.waitForProduce();
System.out.println("正在准备销售...");
product.sale();
System.out.println("现在产品的数量:"+product.getNum());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package cn.xiaozejun.xiancheng.ch005;
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Product product = new Product();
Procduce produce = new Procduce(product);
Sale sale = new Sale(product);
new Thread(produce).start();
new Thread(sale).start();
Thread.sleep(5000);
System.exit(0);//安全退出
}
}