【发布时间】:2020-11-10 11:08:11
【问题描述】:
我正在尝试向自己介绍并行编程,但我不确定我的想法是否正确。我有同一个类的多个实例,我想同时执行同一个任务。在此之后,线程等待 x 时间并再次执行操作。
我目前正在考虑这样的事情
public class RunnableT implements Runnable {
@Override
public void run() {
while(!Main.stop) {
//Perform task
//Wait to mask next action
}
}
}
所以我会有一个这样的主线程:
public class Main {
public static boolean stop;
public static void main(String[] args) {
stop = false;
Thread t1 = new Thread(new RunnableT());
t1.start();
Thread t2 = new Thread(new RunnableT());
t2.start();
for(int i = 0; i < 10; i++) {
//Call t1 & t2 to perform the action
//Wait a second
//Loop
}
stop = true;
}
}
我知道我做错了,但我不知道该怎么做。
【问题讨论】:
标签: java multithreading parallel-processing