gpt4 book ai didi

java - 为什么不是所有线程同时启动?

转载 作者:行者123 更新时间:2023-11-30 06:13:43 24 4
gpt4 key购买 nike

public class B {
public static String lock = "a";
public static void main(String[] args) {
MyThread t1 = new MyThread("Thread 1");
t1.start();

lock = "b";
MyThread t2 = new MyThread("Thread 2");
t2.start();

lock = "c";
MyThread t3 = new MyThread("Thread 3");
t3.start();

lock = "d";
MyThread t4 = new MyThread("Thread 4");
t4.start();
}

}

class MyThread extends Thread{

public MyThread(String name) {
super(name);
}

@Override
public void run() {
synchronized (B.lock){
System.out.println(Thread.currentThread().getName() +" is going to sleep for 5 seconds");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " done sleeping ");
}
}
}

输出:

Thread 1 is going to sleep for 5 seconds
Thread 2 is going to sleep for 5 seconds
Thread 2 done sleeping
Thread 1 done sleeping
Thread 4 is going to sleep for 5 seconds
Thread 4 done sleeping
Thread 3 is going to sleep for 5 seconds
Thread 3 done sleeping

对于不清楚的问题,我们深表歉意。但我在这里的疑问是,由于每次线程启动后我都在更改锁定对象,为什么不是所有线程同时启动并锁定不同的字符串对象?我猜想可能是操作系统线程调度的原因。但是每次执行都只会同时启动 2 个线程(1 和 2),其余 2 个线程(3 和 4)等待获取锁。但是为什么?

最佳答案

如果您想同时启动所有线程,则需要提供某种机制,让一个线程在开始工作之前可以知道其他线程已准备好执行。

有点像 java.util.concurrent.CountDownLatch会帮忙的。基本思想是,您在线程中做的第一件事就是等待 CountDownLatch;当所有线程都创建并启动时,您只将锁存器计数为零。

例如,在您的main 方法中:

CountDownLatch latch = new CountDownLatch(4);

MyThread t1 = new MyThread("Thread 1", latch);
t1.start();
//...
MyThread t4 = new MyThread("Thread 4", latch);
t4.start();

在你的MyThread中:

class MyThread extends Thread{
private final CountDownLatch latch;
public MyThread(String name, CountDownLatch latch) {
super(name);
this.latch = latch;
}

@Override
public void run() {
latch.countDown();
latch.await();
synchronized (B.lock){
//...
}
}
}

所有线程现在都将同时尝试进入synchronized block 。然而,很明显,它们中只有一个会在任何时候执行该 block 。

关于java - 为什么不是所有线程同时启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31502954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com