zoukankan      html  css  js  c++  java
  • 三个线程,ABC 10次(volatile+synchronized(2 synchronized可以保证内存可见性,所以去掉status 的volatile修饰符)

    package ThreadABC;

    public class MyThread extends Thread {
    public static int status = 0;
    @Override
    public void run() {
    while (true) {
    synchronized (MyThread.class) {
    if ("A".equals(Thread.currentThread().getName()) && status < 30 && status % 3 == 0) {
    System.out.print("A");
    status++;
    } else if ("B".equals(Thread.currentThread().getName()) && status < 30 && status % 3 == 1) {
    System.out.print("B");
    status++;

    } else if ("C".equals(Thread.currentThread().getName()) && status < 30 && status % 3 == 2) {
    System.out.println("C");
    status++;
    }
    if (status == 30) {
    break;
    }
    }
    }
    }

    public MyThread(String name) {
    super(name);
    }
    }
  • 相关阅读:
    chess「dp」
    e[树上主席树]
    d[贪心]
    神盐皇
    LA 8043. ACM-ICPC World Finals 2017 E. Need for Speed
    八数码问题
    UVa 679. Dropping Balls
    关于时间复杂度
    欧拉序列 (Euler Tour)
    莫队算法
  • 原文地址:https://www.cnblogs.com/mlz-2019/p/9539784.html
Copyright © 2011-2022 走看看