当前位置:网站首页>多线程顺序输出
多线程顺序输出
2022-08-04 05:31:00 【Louzen】
之前面试被坑过,填坑
public class PrintSorted {
private static final List<MyLock> myLocks = new ArrayList<>();
public static class MyLock {
public String awakeThreadName = "";
public MyLock(String awakeThreadName) {
this.awakeThreadName = awakeThreadName;
}
}
public static void main(String[] args) {
print(10);
synchronized (myLocks.get(0)) {
myLocks.get(0).notifyAll();
}
}
public static void print(int threadNum) {
List<Thread> threads = new ArrayList<>(threadNum);
for (int i = 0; i < threadNum; ++i) {
MyLock myLock = new MyLock("thread-" + i);
myLocks.add(myLock);
}
for (int i = 0; i < threadNum; ++i) {
final int x = i;
threads.add(new Thread() {
@Override
public void run() {
while (true) {
synchronized (myLocks.get(x)) {
try {
myLocks.get(x).wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread - " + x);
synchronized (myLocks.get((x + 1) % threadNum)) {
myLocks.get((x + 1) % threadNum).notifyAll();
}
}
}
});
}
for (int i = 0; i < threadNum; ++i) {
threads.get(i).start();
}
}
}
边栏推荐
- Introduction to Convolutional Neural Networks
- MNIST手写数字识别 —— Lenet-5首个商用级别卷积神经网络
- [开发杂项][调试]debug into kernel
- [日常办公][ssh]cheatsheet
- 【五一专属】阿里云ECS大测评#五一专属|向所有热爱分享的“技术劳动者”致敬#
- AWS uses EC2 to reduce the training cost of DeepRacer: DeepRacer-for-cloud practical operation
- Amazon Cloud Technology Build On 2022 - AIot Season 2 IoT Special Experiment Experience
- 空洞卷积
- 深度学习理论 —— 初始化、参数调节
- 深度学习,“粮草”先行--浅谈数据集获取之道
猜你喜欢

彻底删除MySQL教程

度量学习(Metric learning)—— 基于分类损失函数(softmax、交叉熵、cosface、arcface)

MOOSE平台官方第二个例子分析——关于创建Kernel,求解对流扩散方程

数据库的简述与常用操作指南

MNIST handwritten digit recognition, sorted by from two to ten

Copy攻城狮5分钟在线体验 MindIR 格式模型生成

Deep Learning Theory - Overfitting, Underfitting, Regularization, Optimizers

安装pyspider后运行pyspider all后遇到的问题

【深度学习日记】第一天:Hello world,Hello CNN MNIST

LeetCode_Dec_3rd_Week
随机推荐
Qt日常学习
arm-2-基础阶段
度量学习(Metric learning、损失函数、triplet、三元组损失、fastreid)
DRA821 环境搭建
MNIST Handwritten Digit Recognition - Building a Perceptron from Zero for Two-Classification
ConnectionRefusedError: [Errno 111] Connection refused问题解决
[日常办公][杂项][vscode]tab space
Copy攻城狮的年度之“战”|回顾2020
第二章 STA相关概念
Halcon缺陷检测
[Deep Learning Diary] Day 1: Hello world, Hello CNN MNIST
[日常办公][shell]常用代码段
How to get started with MOOSE platform - an example of how to run the official tutorial
arm交叉编译
如何用Pygame制作简单的贪吃蛇游戏
Endnote编辑参考文献
Rules.make-适合在编辑模式下看
SQL注入详解
Code to celebrate the Dragon Boat Festival - Zongzi, your heart
Amazon Cloud Technology Build On-Amazon Neptune's Knowledge Graph-Based Recommendation Model Building Experience