62,628
社区成员
发帖
与我相关
我的任务
分享public class ThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fruits fruits = new Fruits();
Putfruit p1 = new Putfruit(fruits);
Takefruit tf1 = new Takefruit(fruits);
Thread td1 = new Thread(p1, "Father");
Thread td2 = new Thread(p1, "Mother");
Thread td3 = new Thread(tf1, "Son");
Thread td4 = new Thread(tf1, "Sister");
td1.start();
td2.start();
td3.start();
td4.start();
}
}
class Fruits {
private String fruit;
private static boolean isFull = false;
private int count = 0;
public String getFruit() {
return fruit;
}
public void setFruit(String fruit) {
this.fruit = fruit;
}
public static boolean isFull() {
return isFull;
}
public static void setFull(boolean isFull) {
Fruits.isFull = isFull;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public synchronized void add() {
try {
while (isFull()) {
wait();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (getCount() == 0) {
if (Thread.currentThread().getName().equals("Father")) {
setFruit("Apple");
setCount(1);
setFull(true);
System.out.println(Thread.currentThread().getName()+"放了一个"+getFruit());
notifyAll();
} else {
setFruit("Orange");
setCount(1);
setFull(true);
System.out.println(Thread.currentThread().getName()+"放了一个"+getFruit());
notifyAll();
}
}
}
public synchronized void eat() {
while (!isFull()) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (getCount() == 1) {
if (Thread.currentThread().getName().equals("Son") && getFruit().equals("Apple")) {
System.out.println(Thread.currentThread().getName()+"吃了一个"+getFruit());
setFruit(null);
setFull(false);
setCount(0);
notifyAll();
}else if (Thread.currentThread().getName().equals("Sister") && getFruit().equals("Orange")) {
System.out.println(Thread.currentThread().getName()+"吃了一个"+getFruit());
setFruit(null);
setFull(false);
setCount(0);
notifyAll();
}
}
}
}
class Putfruit implements Runnable {
private Fruits fruits;
public Putfruit(Fruits fruits) {
super();
this.fruits = fruits;
}
public void run() {
while(true) {
fruits.add();
}
}
}
class Takefruit implements Runnable {
private Fruits fruits;
public Takefruit(Fruits fruits) {
super();
this.fruits = fruits;
}
public void run() {
while(true){
fruits.eat();
}
}
}
@org.junit.Test
public void test() throws InterruptedException {
BlockingDeque apple = new LinkedBlockingDeque();
BlockingDeque orange = new LinkedBlockingDeque();
new Thread(() -> {
int i = 0;
while (true) {
String data = "爸爸放苹果" + (++i);
apple.push(data);
System.out.println(data + " 已经完成,开始随机休息");
try {
TimeUnit.MICROSECONDS.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
int i = 0;
while (true) {
String data = "妈妈放橘子" + (++i);
orange.push(data);
System.out.println(data + " 已经完成,开始随机休息");
try {
TimeUnit.MICROSECONDS.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
while (true) {
try {
System.out.println("女儿吃苹果" + apple.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
while (true) {
try {
System.out.println("儿子吃橘子" + orange.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
TimeUnit.MINUTES.sleep(5);
}
public class ThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fruits fruits = new Fruits();
Putfruit p = new Putfruit(fruits);
Takefruit t = new Takefruit(fruits);
Thread td1 = new Thread(p, "Father");
Thread td2 = new Thread(p, "Mother");
Thread td3 = new Thread(t, "Son");
Thread td4 = new Thread(t, "Sister");
td1.start();
td2.start();
td3.start();
td4.start();
}
}
class Fruits {
private String fruit;
private static boolean isFull = false;
private int count = 0;
private int turn = 10;
public String getFruit() {
return fruit;
}
public void setFruit(String fruit) {
this.fruit = fruit;
}
public static boolean isFull() {
return isFull;
}
public static void setFull(boolean isFull) {
Fruits.isFull = isFull;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTurn() {
return turn;
}
public void setTurn() {
turn--;
}
public synchronized void add() {
try {
while (isFull()&& getTurn() > 0) {
wait();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (getCount() == 0 && getTurn() > 0) {
if (Thread.currentThread().getName().equals("Father")) {
setFruit("Apple");
setCount(1);
setFull(true);
System.out.println(Thread.currentThread().getName() + "放了一个" + getFruit());
notifyAll();
} else {
setFruit("Orange");
setCount(1);
setFull(true);
System.out.println(Thread.currentThread().getName() + "放了一个" + getFruit());
notifyAll();
}
}
}
public synchronized void eat() {
while (!isFull()&& getTurn() > 0) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (getCount() == 1&& getTurn() > 0 ) {
if (Thread.currentThread().getName().equals("Son") && getFruit().equals("Apple")) {
System.out.println(Thread.currentThread().getName() + "吃了一个" + getFruit());
setFruit(null);
setFull(false);
setCount(0);
setTurn();
notifyAll();
} else if (Thread.currentThread().getName().equals("Sister") && getFruit().equals("Orange")) {
System.out.println(Thread.currentThread().getName() + "吃了一个" + getFruit());
setFruit(null);
setFull(false);
setCount(0);
setTurn();
notifyAll();
}
}
}
}
class Putfruit implements Runnable {
private Fruits fruits;
public Putfruit(Fruits fruits) {
super();
this.fruits = fruits;
}
public void run() {
while(fruits.getTurn()!=0) {
fruits.add();
}
System.out.println("进程" + Thread.currentThread().getName() + "结束了");
}
}
class Takefruit implements Runnable {
private Fruits fruits;
public Takefruit(Fruits fruits) {
super();
this.fruits = fruits;
}
public void run() {
while(fruits.getTurn()!=0){
fruits.eat();
}
System.out.println("进程" + Thread.currentThread().getName() + "结束了");
}
}
运行结果
Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
进程Sister结束了
进程Mother结束了
进程Son结束了
进程Father结束了

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) {
People father = new People(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (Table.dish) {//获得盘子的控制权
if (Table.dish.getFood() == null) {
Table.dish.setFood(new Apple());
System.out.println("爸爸放了一个苹果...");
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
People mother = new People(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (Table.dish) {//获得盘子的控制权
if (Table.dish.getFood() == null) {
Table.dish.setFood(new Orange());
System.out.println("妈妈放了一个橘子...");
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
People daughter = new People(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (Table.dish) {//获得盘子的控制权
if (Table.dish.getFood() instanceof Apple) {
Table.dish.getFood().eat("daughter");
Table.dish.clear();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
People son = new People(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (Table.dish) {//获得盘子的控制权
if (Table.dish.getFood() instanceof Orange) {
Table.dish.getFood().eat("son");
Table.dish.clear();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(father);
exec.execute(son);
exec.execute(daughter);
exec.execute(mother);
}
}
class People implements Runnable {
private Runnable runnable;
public People(Runnable runnable) {
this.runnable = runnable;
}
@Override
public void run() {
runnable.run();
}
}
class Table {
public final static Dish dish = new Dish();
private Table() {}
}
class Dish {
private Edible food;
public Edible getFood() {
return food;
}
public void setFood(Edible food) {
this.food = food;
}
public void clear() {
this.food = null;
}
}
class Orange implements Edible {
@Override
public void eat(String who) {
System.out.println(who + " eat orange");
}
}
class Apple implements Edible {
@Override
public void eat(String who) {
System.out.println(who + " eat apple");
}
}
interface Edible {
void eat(String who);
}
无聊写写的,随便看看吧