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();
}
}
}