当前位置:网站首页>多线程顺序输出
多线程顺序输出
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();
}
}
}
边栏推荐
- Image-Adaptive YOLO for Object Detection in Adverse Weather Conditions
- tensorRT5.15 使用中的注意点
- MFC读取点云,只能正常显示第一个,显示后面时报错
- MNIST Handwritten Digit Recognition - Lenet-5's First Commercial Grade Convolutional Neural Network
- counting cycle
- MNIST手写数字识别 —— 从二分类到十分类
- 集合--LinkedList
- 软著撰写注意事项
- latex-写论文时一些常用设置
- 【五一专属】阿里云ECS大测评#五一专属|向所有热爱分享的“技术劳动者”致敬#
猜你喜欢

arm-2-基础阶段

LeetCode_Dec_3rd_Week

Amazon Cloud Technology Build On-Amazon Neptune's Knowledge Graph-Based Recommendation Model Building Experience
![[CV-Learning] Linear Classifier (SVM Basics)](/img/94/b48e34b2c215ca47f8ca25ce97547e.png)
[CV-Learning] Linear Classifier (SVM Basics)

target has libraries with conflicting names: libcrypto.a and libssl.a.

PCL1.12 解决memory.h中EIGEN处中断问题

【论文阅读】Further Non-local and Channel Attention Networks for Vehicle Re-identification

Copy Siege Lion 5-minute online experience MindIR format model generation

MOOSE平台使用入门攻略——如何运行官方教程的例子

MNIST手写数字识别 —— ResNet-经典卷积神经网络
随机推荐
彻底删除MySQL教程
【论文阅读】Exploring Spatial Significance via Hybrid Pyramidal Graph Network for Vehicle Re-identificatio
周志华机器学习
yoloV5 使用——训练速度慢,加速训练
【论文阅读】SPANET: SPATIAL PYRAMID ATTENTION NETWORK FOR ENHANCED IMAGE RECOGNITION
Copy攻城狮5分钟在线体验 MindIR 格式模型生成
MOOSE平台官方第二个例子分析——关于创建Kernel,求解对流扩散方程
详解近端策略优化
审稿意见回复
Pytest常用插件
latex-写论文时一些常用设置
基于BiGRU和GAN的数据生成方法
LeetCode_22_Apr_2nd_Week
【论文阅读】Further Non-local and Channel Attention Networks for Vehicle Re-identification
中国联通、欧莱雅和钉钉都在争相打造的秘密武器?虚拟IP未来还有怎样的可能
Brief description of database and common operation guide
Qt日常学习
【论文阅读】Multi-View Spectral Clustering with Optimal Neighborhood Laplacian Matrix
[Copy Siege Lion Log] Flying Pulp Academy Intensive Learning 7-Day Punch Camp-Study Notes
MNIST手写数字识别 —— ResNet-经典卷积神经网络