gpt4 book ai didi

java - 一个线程上有多个启动

转载 作者:行者123 更新时间:2023-12-01 07:50:13 25 4
gpt4 key购买 nike

我试图多次启动一个线程。但我读到这是不可能的,因为我得到了 IllegalThreadStateException 。我正在尝试做以下事情。我有n部智能手机。我想对应用程序进行自动测试。测试是并行的,每台设备一个应用程序。有什么想法可以解决吗?这是我的代码?

Semaphore smartphones = new Semaphore(androidComponents.size());
Thread evaluationProcess = new Thread() {
public void run() {
try {
smartphones.acquire();
[CODE FOR TESTING]
smartphones.release();
}
};

while (!evaluationCompleted(apps)) {
evaluationProcess.start();
while (smartphones.availablePermits() == 0) ;
}

最佳答案

因为你不能在一个线程上调用两次start。

Thread API:

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

您必须在 run() 中放入一个工作单元 - 需要由多个线程执行的代码。然后根据需要实例化尽可能多的线程(对每个线程调用 start,但仅调用一次)。

关于java - 一个线程上有多个启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174839/

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