• java多线程(5)模拟排队叫号程序,不能出现交替执行的结果


    package com.javaconcurrencyprogramming.chapter1;

    /**
    * @description: 排队叫号程序,还是有问题
    * @author:
    * @create:
    **/

    public class TicketWindowRunnable implements Runnable{

    private int index = 1; //不做static修饰

    private final static int MAX = 50;


    @Override
    public void run() {

    while (index <= MAX){

    System.out.println(Thread.currentThread() + " 的号码是: " + (index++));

    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }


    public static void main(String[] args) {

    final TicketWindowRunnable task = new TicketWindowRunnable();

    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();
    }
    }
  • 相关阅读:
    第十三周学习进度条
    冲刺第四天
    第二天冲刺
    第三天冲刺
    第一天冲刺
    课下作业
    第十二周学习进度条
    课下作业
    webp与png、jpg相互转换
    Node疑难解决
  • 原文地址:https://www.cnblogs.com/herosoft/p/10738321.html
Copyright © 2020-2023  润新知