我有以下测试项目:
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class myThreadTest {
private static final Logger log = LoggerFactory.getLogger(myThreadTest.class);
private ScheduledExecutorService executorService1 = Executors.newSingleThreadScheduledExecutor();
private ScheduledExecutorService executorService2 = Executors.newSingleThreadScheduledExecutor();
private Future<?> task1;
private Future<?> task2;
private class Task1 implements Runnable {
@Override
public synchronized void run() {
log.debug("-----------------------");
for (int i = 0; i < 100; i++) {
log.debug("{} Hello from Task 1",i);
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.debug("-----------------------");
}
}
private class Task2 implements Runnable {
@Override
public synchronized void run() {
log.debug("********************");
for (int i = 0; i < 100; i++) {
log.debug("{} Hello from Task 2",i);
}
log.debug("********************");
}
}
private void start() {
task1 = executorService1.scheduleAtFixedRate(new Task1(), 1, 500, TimeUnit.MILLISECONDS);
task2 = executorService2.scheduleAtFixedRate(new Task2(), 1, 505, TimeUnit.MILLISECONDS);
}
public void stop() throws InterruptedException {
if (task1 != null) {
task1.cancel(false);
}
if (task2 != null) {
task2.cancel(false);
}
}
public static void main(String[] args) {
myThreadTest mtt = new myThreadTest();
mtt.start();
}
}
我有2个任务在不同的线程中运行。然而,run方法是同步的,我可以在调试日志中看到线程在执行方法时正在发生变化 - 为什么会这样?
...
17:56:09,593 pool-2-thread-1 DEBUG myThreadTest:39 - ********************
17:56:09,593 pool-1-thread-1 DEBUG myThreadTest:21 - -----------------------
17:56:09,600 pool-2-thread-1 DEBUG myThreadTest:41 - 0 Hello from Task 2
17:56:09,600 pool-2-thread-1 DEBUG myThreadTest:41 - 1 Hello from Task 2
17:56:09,600 pool-1-thread-1 DEBUG myThreadTest:23 - 0 Hello from Task 1
17:56:09,600 pool-2-thread-1 DEBUG myThreadTest:41 - 2 Hello from Task 2
17:56:09,600 pool-2-thread-1 DEBUG myThreadTest:41 - 3 Hello from Task 2
17:56:09,601 pool-2-thread-1 DEBUG myThreadTest:41 - 4 Hello from Task 2
17:56:09,601 pool-2-thread-1 DEBUG myThreadTest:41 - 5 Hello from Task 2
17:56:09,601 pool-2-thread-1 DEBUG myThreadTest:41 - 6 Hello from Task 2
17:56:09,601 pool-2-thread-1 DEBUG myThreadTest:41 - 7 Hello from Task 2
17:56:09,602 pool-2-thread-1 DEBUG myThreadTest:41 - 8 Hello from Task 2
17:56:09,602 pool-2-thread-1 DEBUG myThreadTest:41 - 9 Hello from Task 2
17:56:09,602 pool-2-thread-1 DEBUG myThreadTest:41 - 10 Hello from Task 2
17:56:09,603 pool-2-thread-1 DEBUG myThreadTest:41 - 11 Hello from Task 2
17:56:09,603 pool-1-thread-1 DEBUG myThreadTest:23 - 1 Hello from Task 1
17:56:09,603 pool-2-thread-1 DEBUG myThreadTest:41 - 12 Hello from Task 2
17:56:09,606 pool-2-thread-1 DEBUG myThreadTest:41 - 13 Hello from Task 2
17:56:09,607 pool-2-thread-1 DEBUG myThreadTest:41 - 14 Hello from Task 2
17:56:09,607 pool-2-thread-1 DEBUG myThreadTest:41 - 15 Hello from Task 2
17:56:09,607 pool-2-thread-1 DEBUG myThreadTest:41 - 16 Hello from Task 2
17:56:09,608 pool-2-thread-1 DEBUG myThreadTest:41 - 17 Hello from Task 2
17:56:09,608 pool-2-thread-1 DEBUG myThreadTest:41 - 18 Hello from Task 2
17:56:09,608 pool-2-thread-1 DEBUG myThreadTest:41 - 19 Hello from Task 2
17:56:09,608 pool-2-thread-1 DEBUG myThreadTest:41 - 20 Hello from Task 2
17:56:09,609 pool-1-thread-1 DEBUG myThreadTest:23 - 2 Hello from Task 1
17:56:09,609 pool-2-thread-1 DEBUG myThreadTest:41 - 21 Hello from Task 2
17:56:09,609 pool-2-thread-1 DEBUG myThreadTest:41 - 22 Hello from Task 2
17:56:09,609 pool-2-thread-1 DEBUG myThreadTest:41 - 23 Hello from Task 2
17:56:09,610 pool-2-thread-1 DEBUG myThreadTest:41 - 24 Hello from Task 2
17:56:09,610 pool-2-thread-1 DEBUG myThreadTest:41 - 25 Hello from Task 2
17:56:09,610 pool-2-thread-1 DEBUG myThreadTest:41 - 26 Hello from Task 2
17:56:09,611 pool-2-thread-1 DEBUG myThreadTest:41 - 27 Hello from Task 2
17:56:09,611 pool-2-thread-1 DEBUG myThreadTest:41 - 28 Hello from Task 2
17:56:09,611 pool-2-thread-1 DEBUG myThreadTest:41 - 29 Hello from Task 2
17:56:09,611 pool-2-thread-1 DEBUG myThreadTest:41 - 30 Hello from Task 2
17:56:09,611 pool-1-thread-1 DEBUG myThreadTest:23 - 3 Hello from Task 1
17:56:09,612 pool-2-thread-1 DEBUG myThreadTest:41 - 31 Hello from Task 2
17:56:09,612 pool-2-thread-1 DEBUG myThreadTest:41 - 32 Hello from Task 2
17:56:09,612 pool-2-thread-1 DEBUG myThreadTest:41 - 33 Hello from Task 2
...
答案 0 :(得分:5)
新的Task1()和新的Task2()都是不同的对象。行为符合预期。