多线程实现红绿灯 |
package HXZY.moth5.day5_11.linghtpract;
/**
* およそ神
* 抽象的灯类
*/
public abstract class Linght {
abstract void gree() throws InterruptedException;
abstract void red() throws InterruptedException;
abstract void yellow() throws InterruptedException;
}
接下来分别是3种对应颜色的灯实现Runnable接口:
package HXZY.moth5.day5_11.linghtpract;
/**
* およそ神
*/
public class Green implements Runnable {
private Linght linght;
public Green(Linght linght) {
this.linght = linght;
}
@Override
public void run() {
while (true) {
try {
linght.gree();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package HXZY.moth5.day5_11.linghtpract;
/**
* およそ神
*/
public class Red implements Runnable {
private Linght linght;
public Red(Linght linght) {
this.linght = linght;
}
@Override
public void run() {
while (true) {
try {
linght.red();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package HXZY.moth5.day5_11.linghtpract;
/**
* およそ神
*/
public class Yellow implements Runnable {
private Linght linght;
public Yellow(Linght linght) {
this.linght = linght;
}
@Override
public void run() {
while (true) {
try {
linght.yellow();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
接下来是我们的控制台管理类:
package HXZY.moth5.day5_11.linghtpract;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* およそ神
*/
public class Aqs extends Linght {
private final ReentrantLock lock = new ReentrantLock();
private final Condition notRed = lock.newCondition();
private final Condition notGreen = lock.newCondition();
private final Condition notYellow = lock.newCondition();
private int swich = 1;
/**
*调用绿灯的方法,绿灯亮,通知黄灯
*调用红灯的方法,红灯亮,通知绿灯
* 调用黄灯的方法,黄灯亮,通知红灯
*/
@Override
void gree() throws InterruptedException {
try {
lock.lock();//需要同步的代码首先加锁
while (swich!=1)notGreen.await();//不是绿色阻塞
for (int i = 0; i < 5; i++) {
System.out.println("绿灯亮" + (i + 1) + "次");
Thread.sleep(500);
}
this.swich = 2;
this.notYellow.signal();//给绿灯发信息
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();//用完一定要解锁
}
}
@Override
void red() throws InterruptedException {
try {
lock.lock();//需要同步的代码首先加锁
while (swich!=2)notRed.await();//不是绿色阻塞
for (int i = 0; i < 5; i++) {
System.out.println("红灯亮" + (i + 1) + "次");
Thread.sleep(500);
}
this.swich = 3;
this.notGreen.signal();//给黄灯发信息
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
@Override
void yellow() throws InterruptedException {
try {
lock.lock();//需要同步的代码首先加锁
while (swich!=3)notYellow.await();
for (int i = 0; i < 5; i++) {
System.out.println("黄灯亮" + (i + 1) + "次");
Thread.sleep(500);
}
this.swich = 1;
this.notRed.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
最后是我们的测试类:
package HXZY.moth5.day5_11.linghtpract;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* およそ神
*/
public class Test {
public static void main(String[] args) {
Aqs aqs = new Aqs();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(new Yellow(aqs));
executorService.submit(new Green(aqs));
executorService.submit(new Red(aqs));
executorService.shutdown();
}
}