1.实现银行叫号机程序(假设最大叫号到100,一共三个叫号机)
- 无序 方式一
package work;
import java.util.concurrent.TimeUnit;
public class BankCallNumThread {
public static void main(String[] args) {
/**
*设置三个线程
*/
BankTickerTa bankTickerTa = new BankTickerTa();
new Thread(bankTickerTa, "窗口A").start();
new Thread(bankTickerTa, "窗口B").start();
new Thread(bankTickerTa, "窗口C").start();
}
}
class BankTickerTa extends Thread {
int ticker = 1;
@Override
public void run() {
while (true) {
synchronized (this) {
if (ticker > 100) {
System.out.println("今天号已经叫完,请明天早点来哦");
break;
} else {
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("请到" + Thread.currentThread().getName() + "等待 " + "票号为" + ticker);
ticker++;
}
}
}
}
}
- 无序 方式二
package work;
public class BankCallNumRunnable {
public static void main(String[] args) {
//使用方式一和方式二实现银行叫号机程序(假设最大叫号到100,一共三个叫号机)
//定义三个线程 模拟叫号机
BankTickerTask task = new BankTickerTask();
//窗口A
Thread t1 = new Thread(task);
t1.setName("窗口A");
//窗口B
Thread t2 = new Thread(task);
t2.setName("窗口B");
//窗口C
Thread t3 = new Thread(task);
t3.setName("窗口C");
t1.start();
t2.start();
t3.start();
}}
class BankTickerTask implements Runnable {
int ticker=1;
@Override
public void run() {
while (true) {
synchronized (this) {
if (ticker >100 ) {
System.out.println("今天"+Thread.currentThread().getName()+"已经叫完,请明天早点来哦");
break;
} else {
System.out.println("请到" + Thread.currentThread().getName() + "等待 " + "票号为" + ticker);
ticker ++;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
- 有序
package work;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadLock {
public static void main(String[] args) {
//三个线程间的通讯
MyTask1 task = new MyTask1();
new Thread() {
@Override
public void run() {
while (true) {
try {
task.task1();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
;
}.start();
new Thread() {
@Override
public void run() {
while (true) {
try {
task.task2();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
;
}.start();
new Thread() {
@Override
public void run() {
while (true) {
try {
task.task3();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
;
}.start();
}
}
class MyTask1 {
int ticker = 1;
//创建互斥锁对象
ReentrantLock r = new ReentrantLock();
//创建三个对象
Condition c1 = r.newCondition();
Condition c2 = r.newCondition();
Condition c3 = r.newCondition();
//标识 1:可以执行任务1,2:可以执行任务2, 3:可以执行任务3
int flag = 1;
public void task1() throws InterruptedException {
while (true) {
r.lock();
if (flag != 1) {
c1.await();//当前线程等待
}
if (ticker <= 100){
System.out.println("窗口A "+" 票号" + ticker++);
}else{
System.out.println("明天早点来");
System.exit(0);
}
flag = 2;
//唤醒线程
c2.signal();
r.unlock();
}
}
public void task2() throws InterruptedException {
while (true) {
r.lock();
if (flag != 2) {
c2.await();//线程等待
}
if (ticker <= 100) {
System.out.println("窗口B:"+" 票号" + ticker++);
} else {
System.out.println("明天早点来");
System.exit(0);
}
flag = 3;
c3.signal();
r.unlock();
}
}
public void task3() throws InterruptedException {
while (true) {
r.lock();
if (flag != 3) {
c3.await();//线程等待
}
if (ticker <= 100){
System.out.println("窗口C:" +" 票号"+ ticker++);
}else {
System.out.println("明天早点来");
System.exit(0);
}
flag = 1;
c1.signal();
r.unlock();
}
}
}