java流水线工程设计_java多线程计算机流水线模型

/*设计一个生产电脑和搬运电脑类,要求每生产出一台电脑就搬走一台电脑。

*没有生产出新的电脑,则搬运工要等待新电脑产出;

*如果生产出的电脑没有搬走,则要等待电脑搬走之后再生产,并统计出生产的电脑数量。

*/

package Java多线程_01;

class Resource_01{

private Computer computer;

public synchronized void make() throws Exception{

if(this.computer != null) {

super.wait();

}

Thread.sleep(100);

this.computer = new Computer("惠普v1000", 11000);

System.out.println("【生产电脑】" + this.computer);

super.notifyAll();

}

public synchronized void get() throws Exception{

if(this.computer == null) {

super.wait();

}

Thread.sleep(10);

System.out.println("【取走电脑】"+this.computer);

this.computer = null;

super.notifyAll();

}

}

class Computer{

private static int count = 0;

private String name;

private double price;

public Computer(String name, double price) {

this.name = name;

this.price = price;

count++;

}

public String toString() {

return "【第"+count+"台电脑】"+"电脑品牌:"+this.name+"、价值:"+this.price;

}

}

class Producer_01 implements Runnable{

private Resource_01 resource;

public Producer_01(Resource_01 resource) {

this.resource = resource;

}

public void run() {

for(int x=0;x<50;x++) {

try {

this.resource.make();

}catch(Exception e) {

e.printStackTrace();

}

}

}

}

class Consumer_01 implements Runnable{

private Resource_01 resource;

public Consumer_01(Resource_01 resource) {

this.resource = resource;

}

public void run() {

for(int x=0;x<50;x++) {

try {

this.resource.get();

}catch (Exception e) {

e.printStackTrace();

}

}

}

}

public class homework_02 {

public static void main(String[] args) {

Resource_01 resource = new Resource_01();

new Thread(new Producer_01(resource)).start();

new Thread(new Consumer_01(resource)).start();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值