案例一
//写一个模拟汽车生产流水线的程序
//1. 要求生产 30 辆车,其有生产序号, 名称
//在生产前,检查前面生产的产品是否已被取出,如未取出,则不能生产
//在设置生产序号和名称之前休眠 20ms
//生产好的汽车放到流水线上
//2. 装运工把在流水线生产好的汽车取出,输出汽车序号和名称信息
//要求输出的生产序号和名称相一致
public class MainClass {
public static void main(String[] args) {
Car car = new Car();
CarProduction carProduction = new CarProduction(car);
CarConsumption carConsumption = new CarConsumption(car);
new Thread(carConsumption).start();
new Thread(carProduction).start();
}
}
class CarProduction implements Runnable {
private Car car;
public CarProduction() {
}
public CarProduction(Car car) {
this.car = car;
}
@Override
public synchronized void run() {
for (int i = 1; i <= 30; i++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i % 2 == 0) {
car.set("汽车" + i, i);
} else {
car.set("卡车" + i, i);
}
}
}
}
class CarConsumption implements Runnable {
private Car car;
public CarConsumption() {
}
public CarConsumption(Car car) {
this.car = car;
}
@Override
public synchronized void run() {
for (int i = 1; i <= 100; i++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
car.get();
}
}
}
class Car {
private String name;
private int id;
private boolean flag = false;
public Car() {
}
public Car(String name, int id) {
this.name = name;
this.id = id;
}
public synchronized void get() {
if (!flag) {
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.name + "->" + this.id);
flag = false;
super.notify();
}
public synchronized void set(String name, int id) {
if (flag) {
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.id = id;
flag = true;
super.notify();
}
}
案例二
三个生产商与三个消费者共用一个仓库,生产商会不断的生产商品,消费者同样也在不断的消费,但当仓库满仓时,生产商停止生产,但消费者同时还在不断的消费,当仓库有空闲仓位时,生产商继续生产。同样消费者也是,当仓库没有商品时,消费者等生产商生产出商品后,才继续消费。
public class MainClass {
public static void main(String[] args) {
Store store = new Store();
new Thread(new Consumer("张三", store)).start();
new Thread(new Consumer("李四", store)).start();
new Thread(new Consumer("王五", store)).start();
new Thread(new Producer("第一工厂", store)).start();
new Thread(new Producer("第二工厂", store)).start();
}
}
class Store {
private final int MAX = 10;
private int num = 0;
public synchronized void produce(Producer producer) throws InterruptedException {
while (num == MAX) {
System.out.println("仓库已满 ,请等待空位");
wait();
}
++num;
System.out.println(producer.name + " 生产了后,仓库现存 :" + num + "个产品");
notify();
}
public synchronized void consumer(Consumer consumer) throws InterruptedException {
while (num == 0) {
System.out.println("仓库空仓,请等待产品入库");
wait();
}
--num;
System.out.println(consumer.name + " 消费后,还剩 :" + num + "个产品");
notify();
}
}
class Producer implements Runnable {
String name;
Store store;
public Producer(String name, Store store) {
this.name = name;
this.store = store;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
store.produce(this);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
String name;
Store store;
public Consumer(String name, Store store) {
this.name = name;
this.store = store;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep((long) (Math.random() % 30 * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
store.consumer(this);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
案例3
两人从同一银行账户取钱
class UserGetMoney implements Runnable{
private static int sum = 2000;
private int money = 0;
public UserGetMoney(int money) {
this.money = money;
}
public synchronized void take(int k) {
int temp = sum;
temp -= k;
try {
Thread.sleep((int) (100 * Math.random()));
} catch (InterruptedException e) {
}
sum = temp;
System.out.println(Thread.currentThread() + "sum = " + sum);
}
public void run() {
for (int i = 1; i <= 4; i++) {
take(money);
}
}
}
public class BankTest {
public static void main(String[] args) {
UserGetMoney u = new UserGetMoney(100);
new Thread(u).start();
new Thread(u).start();
}
}
|