zoukankan      html  css  js  c++  java
  • 叫号系统的模拟---采用java synchronized

    package com.com.pipi

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

    /**
    * @description: 这样容易吃大锅饭
    * @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(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    change = false;
    }
    change = true;

    }
    }

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

    for(int i = 0; i < 10; i++){

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


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

    }




    }
    }
  • 相关阅读:
    Android改app名称
    DNSLog注入笔记
    mac burp suite https证书安装
    python-requests-proxies判断学习
    mac java jdk 安装删除
    php简单一句话分析
    mysql盲注学习-1
    Python实现访问者模式
    Python operator模块和functools模块
    SQL 日期函数转换
  • 原文地址:https://www.cnblogs.com/herosoft/p/9473159.html
Copyright © 2011-2022 走看看