package com.com.wangwenjun.concurrent.chapter04;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
* @description: for循环主要解决的是线程的生命周期完成后又进行一轮
* @author:
* @create:
**/

public class TicketWindowRunnable implements Runnable{

private int index = 1;
private boolean change = true;

private final static int MAX = 50;

private final static Object MUTEX = new Object();

@Override
public void run() {
synchronized(MUTEX){
while(index <= MAX && change ){
System.out.println(Thread.currentThread().getName() + " 的号码是: " +(index++));

try {
// TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}

change = false;
}
change = true;

}
}

public static void main(String[] args){
final TicketWindowRunnable task = new TicketWindowRunnable();

//4为线程个数据,也就是要循环至少13次
for(int i = 0; i < MAX/4 + 1; i++){

Thread windowThread1 = new Thread(task, "一号窗口");
Thread windowThread2 = new Thread(task, "二号窗口");
Thread windowThread3 = new Thread(task, "三号窗口");
Thread windowThread4 = new Thread(task, "四号窗口");

windowThread1.start();
windowThread2.start();
windowThread3.start();
windowThread4.start();

}

}
}

相关文章:

  • 2021-12-07
  • 2021-11-26
  • 2022-01-11
  • 2021-07-10
  • 2021-07-29
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-13
  • 2021-09-27
相关资源
相似解决方案