題目:
![]()
方法1:(有問題)
package com.gao.Project.Pro9;
public class Product {//商品類
//品牌
private String brand;
//名字
private String name;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.gao.Project.Pro9;
public class PuoducerThread extends Thread{ //生產者線程
//共享商品
private Product p ;
//構造器
public PuoducerThread(Product p) {
this.p = p;
}
@Override
public void run() {
for (int i = 1; i <= 10 ; i++) {//生產10個商品 i--->生產次數
synchronized (p){
if(i%2 == 0){
//生成費列羅巧克力
p.setBrand("費列羅");
p.setName("巧克力");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}else {
//生產哈爾濱啤酒
p.setBrand("哈爾濱");
p.setName("啤酒");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
//將生產的產品打印出來
System.out.println("生產者生產了"+p.getBrand()+"---"+p.getName());
}
}
}
package com.gao.Project.Pro9;
public class CustomerThread extends Thread{//消費者線程
//共享商品
private Product p;
public CustomerThread(Product p) {
this.p = p;
}
@Override
public void run() {
for (int i = 1; i <=10 ; i++) {// 消費次數
synchronized (p){//用同一個鎖鎖住,解決同步問題-->防止數據錯亂--->費列羅啤酒,哈爾濱巧克力
System.out.println("消費者消費了"+p.getBrand()+"---"+p.getName());
}
}
}
}
package com.gao.Project.Pro9;
public class Test {
public static void main(String[] args) {
//共享的商品
Product p = new Product();
//創建生產者和消費者線程
PuoducerThread pt = new PuoducerThread(p);
CustomerThread ct = new CustomerThread(p);
pt.start();
ct.start();
}
}
方法2:(同步)
package com.gao.Project.Pro10;
public class PuoducerThread extends Thread{ //生產者線程
//共享商品
private Product p ;
//構造器
public PuoducerThread(Product p) {
this.p = p;
}
@Override
public void run() {
for (int i = 1; i <= 10 ; i++) {//生產10個商品 i--->生產次數
if(i % 2 == 0){
p.setProduct("費列羅","巧克力");
}else {
p.setProduct("哈爾濱","啤酒");
}
}
}
}
package com.gao.Project.Pro10;
public class CustomerThread extends Thread{//消費者線程
//共享商品
private Product p;
public CustomerThread(Product p) {
this.p = p;
}
@Override
public void run() {
for (int i = 1; i <=10 ; i++) {// 消費次數
p.getProduct();
}
}
}
package com.gao.Project.Pro10;
public class Product {//商品類
//品牌
private String brand;
//名字
private String name;
//引入一個燈 ture:紅燈 false:綠燈
boolean flag = false; //默認情況沒有商品,生產者得生產了之后才能消費
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//生產商品
public synchronized void setProduct(String brand,String name){
if(flag == true){//燈是紅的,證明有商品,生產者不生產,等待消費者消費
try {
wait(); //出現異常,用try catch 捕獲
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//燈是綠的,證明沒有商品,就生產
this.setBrand(brand);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
this.setName(name);
//將生產的產品打印出來
System.out.println("生產者生產了"+this.getBrand()+"---"+this.getName());
//生產完以后,燈變色
flag = true;
//告訴消費者來消費
notify();
}
//消費商品
public synchronized void getProduct (){
if(!flag){//(flag == false) 燈是綠的,證明沒有商品,消費者需要等待
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//燈是紅的,證明有商品,可以消費
System.out.println("消費者消費了"+this.getBrand()+"---"+this.getName());
//消費完,燈變色
flag = false;
//告訴生產者趕緊生產
notify();
}
}
package com.gao.Project.Pro10;
public class Test {
public static void main(String[] args) {
//共享的商品
Product p = new Product();
//創建生產者和消費者線程
PuoducerThread pt = new PuoducerThread(p);
CustomerThread ct = new CustomerThread(p);
pt.start();
ct.start();
}
}