生产搬运电脑
class Resource1 {
private int number = 0;
private Computer computer;
public Resource1() {
Thread numberDaemonThread = new Thread(() -> {
while (true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("【守护线程】电脑数量:" + this.number);
}
});
numberDaemonThread.setDaemon(true);
numberDaemonThread.start();
}
public synchronized void create(String brand, double price) {
if (this.computer != null) {
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.computer = new Computer(brand, price);
this.number++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("【" + Thread.currentThread().getName() + "】电脑生产完成" + this.number);
super.notify();
}
public synchronized void carry(String brand, double price) {
if (this.computer == null) {
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("【" + Thread.currentThread().getName() + "】搬运电脑" + this.number);
this.computer = null;
super.notify();
}
}
class Computer {
private String brand;
private double price;
public Computer(String brand, double price) {
this.brand = brand;
this.price = price;
}
@Override
public String toString() {
return "Computer{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
public class Test02 {
public static void main(String[] args) {
Resource1 resource1 = new Resource1();
new Thread(()->{
for (int i = 0; i < 10; i++) {
resource1.create("外星人",2999.01);
}
},"电脑生产线程").start();
new Thread(()->{
for (int j = 0; j < 10; j++) {
resource1.carry("外星人",2999.01);
}
},"电脑搬运线程").start();
}
}
多线程进行加减法
class Resource {
private int number = 0;
private boolean flag;
public synchronized void add(){
while (flag == false){
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.number++;
System.out.println("[" + Thread.currentThread().getName() + "]执行加法操作结果为:" + this.number);
this.flag = false;
super.notifyAll();
}
public synchronized void sub(){
while (this.flag == true){
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.number--;
System.out.println("[" + Thread.currentThread().getName() + "]执行减法操作结果为:" + this.number);
this.flag = true;
super.notifyAll();
}
}
public class Test01 {
public static void main(String[] args) throws Exception {
Resource resource = new Resource();
for (int i = 0; i < 2; i++) {
new Thread(()->{
for (int j = 0; j < 10; j++) {
synchronized (resource){
resource.add();
}
}
},"{加法线程} i=" + i).start();
}
for (int i = 0; i < 2; i++) {
new Thread(()->{
for (int j = 0; j < 10; j++) {
synchronized (resource){
resource.add();
}
}
},"{减法线程} i=" + i).start();
}
}
}
多线程——竞拍抢答
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
class AnswerThread implements Callable<String> {
private boolean flag;
@Override
public String call() throws Exception {
if (this.flag == false) {
this.flag = true;
return "【" + Thread.currentThread().getName() + "】你抢答成功";
}else {
return "【" + Thread.currentThread().getName() + "】抢答失败";
}
}
}
public class Test01 {
public static void main(String[] args) throws Exception {
AnswerThread answerThread = new AnswerThread();
FutureTask<String> futureTask1 = new FutureTask<>(answerThread);
FutureTask<String> futureTask2 = new FutureTask<>(answerThread);
FutureTask<String> futureTask3 = new FutureTask<>(answerThread);
new Thread(futureTask1).start();
new Thread(futureTask2).start();
new Thread(futureTask3).start();
System.out.println(futureTask1.get());
System.out.println(futureTask2.get());
System.out.println(futureTask3.get());
}
}
----------------------
output:
【Thread-0】你抢答成功
【Thread-1】抢答失败
【Thread-2】抢答失败