目录
- 一、四个线程两个进行加,两个进行减,互相抢
- 步骤
- 代码
- 二、四个线程,线程加减法,循环输出加一和减一
- 步骤
- 代码
- 出现的问题
四个线程两个进行加,两个进行减,互相抢,满足条件则胜利
步骤:
- 现将加和减的方法写出来,在定义四个线程。
- 利用while(true)可以一直进行抢占。
代码:
class OneThread implements Runnable {
private Data data;
public OneThread(Data data) {
this.data = data;
}
@Override
public void run() {
while (true) {
try {
this.data.add();
System.out.println(Thread.currentThread().getName() + "--加--:" + this.data.getTicket());
} catch (InterruptedException e) {
e.printStackTrace();
}
if (this.data.getTicket() >= 600) {
System.out.println("add()胜利");
break;
} else if (this.data.getTicket() <= 400) {
System.out.println("add()失败");
break;
}
}
}
}
class TwoThread implements Runnable {
private Data data;
public TwoThread(Data data) {
this.data = data;
}
@Override
public void run() {
while (true) {
try {
this.data.add();
System.out.println(Thread.currentThread().getName() + "--加--:" + this.data.getTicket());
} catch (InterruptedException e) {
e.printStackTrace();
}
if (this.data.getTicket() >= 600) {
System.out.println("add()胜利");
break;
} else if (this.data.getTicket() <= 400) {
System.out.println("add()失败");
break;
}
}
}
}
class ThreeThread implements Runnable {
private Data data;
public ThreeThread(Data data) {
this.data = data;
}
@Override
public void run() {
while (true) {
try {
this.data.less();
System.out.println(Thread.currentThread().getName() + "==减==:" + this.data.getTicket());
} catch (InterruptedException e) {
e.printStackTrace();
}
if (this.data.getTicket() >= 600) {
System.out.println("less()失败");
break;
} else if (this.data.getTicket() <= 400) {
System.out.println("less()胜利");
break;
}
}
}
}
class FourThread implements Runnable {
private Data data;
public FourThread(Data data) {
this.data = data;
}
@Override
public void run() {
while (true) {
try {
this.data.less();
System.out.println(Thread.currentThread().getName() + "==减==:" + this.data.getTicket());
} catch (InterruptedException e) {
e.printStackTrace();
}
if (this.data.getTicket() >=600) {
System.out.println("less()失败");
break;
} else if (this.data.getTicket() <= 400) {
System.out.println("less()胜利");
break;
}
}
}
}
class Data {
private int ticket = 500;
public void setTicket(int ticket) {
this.ticket = ticket;
}
public int getTicket() {
return this.ticket;
}
public synchronized void add() throws InterruptedException {
this.setTicket(++this.ticket);
Thread.sleep(10);
}
public synchronized void less() throws InterruptedException {
this.setTicket(--this.ticket);
Thread.sleep(10);
}
}
public class Demo01 {
public static void main(String[] args) {
Data data = new Data();
OneThread one = new OneThread(data);
TwoThread two = new TwoThread(data);
ThreeThread three = new ThreeThread(data);
FourThread four = new FourThread(data);
new Thread(one, "线程一").start();
new Thread(two, "线程二").start();
new Thread(three, "线程三").start();
new Thread(four, "线程四").start();
}
}
结果:
线程三==减==:403
线程四==减==:402
线程一--加--:403
线程四==减==:402
线程三==减==:401
线程二--加--:402
线程三==减==:401
less()胜利
线程四==减==:400
线程一--加--:401
add()失败
线程四==减==:400
线程二--加--:401
add()失败
线程四==减==:400
less()胜利
四个线程,线程加减法,循环输出加一和减一
步骤:
- 定义一个类,写出加法和减法的方法
- 在main()内些Lamdba表达式定义四个线程进行加法和减法的循环输出
代码:
class MessageA {
private int count = 0;
private boolean isFlag = false;
//true表示加法
//false表示减法
public synchronized void sub() {
synchronized (this) {
if (isFlag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.count--;
System.out.println("【" + Thread.currentThread().getName() + "】减" + this.count);
isFlag = true;
this.notify();
}
}
public synchronized void add() {
synchronized (this) {
if (!isFlag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.count++;
System.out.println("【" + Thread.currentThread().getName() + "】加" + this.count);
isFlag = false;
this.notify();
}
}
}
public class LamdbaA {
public static void main(String[] args) {
MessageA message = new MessageA();
for (int i = 1; i <= 4; i++) {
if (i % 2 == 0) {
new Thread(() -> {
for (int j = 0; j < 50; j++) {
message.sub();
}
}, "线程" + i).start();
} else {
new Thread(() -> {
for (int j = 0; j < 50; j++) {
synchronized (message) {
message.add();
}
}
}, "线程" + i).start();
}
}
}
}
结果:
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
出现的问题:
不先进行加法,让线程等待,就会出现错误,输出的数据乱套,不知道是哪出现了问题。
【线程0】减-1
【线程2】减-2
【线程3】加-1
【线程1】加0
【线程0】减-1
【线程2】减-2
【线程3】加-1
【线程1】加0
【线程2】减-1
【线程1】加0