1、商品类
package com.wyq.syn;
public class Product3 {
private String name;
private String brand;
//fals代表没有
public boolean flag = false;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Product3(String name, String brand) {
super();
this.name = name;
this.brand = brand;
}
public Product3() {
super();
}
public synchronized void pro(String name, String brand){
if(flag){
try {
super.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
this.setBrand(brand);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setName(name);
System.out.println(Thread.currentThread().getName()+"生产了"+this.brand+"牌子的"+this.name);
flag = true;
super.notify();
}
public synchronized void con(){
if(!flag){
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"消费了"+this.brand+"品牌的"+this.name);
flag = false;
super.notify();
}
}
2、生产者
package com.wyq.syn;
public class Product3 {
private String name;
private String brand;
//fals代表没有
public boolean flag = false;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Product3(String name, String brand) {
super();
this.name = name;
this.brand = brand;
}
public Product3() {
super();
}
public synchronized void pro(String name, String brand){
if(flag){
try {
super.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
this.setBrand(brand);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setName(name);
System.out.println(Thread.currentThread().getName()+"生产了"+this.brand+"牌子的"+this.name);
flag = true;
super.notify();
}
public synchronized void con(){
if(!flag){
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"消费了"+this.brand+"品牌的"+this.name);
flag = false;
super.notify();
}
}
3、消费者
package com.wyq.syn;
public class Consumer3 implements Runnable{
private Product3 pro;
public Product3 getPro() {
return pro;
}
public void setPro(Product3 pro) {
this.pro = pro;
}
public Consumer3(Product3 pro) {
super();
this.pro = pro;
}
public Consumer3() {
super();
}
@Override
public void run() {
for(int i =0;i<10;i++){
pro.con();
}
}
}
4、测试类
package com.wyq.syn;
public class Test3 {
public static void main(String[] args) {
Product3 pro = new Product3();
Producer3 p = new Producer3(pro);
Consumer3 c = new Consumer3(pro);
Thread t1 = new Thread(p, "生产者");
Thread t2 = new Thread(c, "消费者");
t1.start();
t2.start();
}
}
5、总结