预期在单线程上有序执行
Is ordered execution expected on the single thread
由于服务是单线程的,monkey1 循环系列总是在 monkey2 之前执行,所以我们可以预期 monkey1 总是大于 monkey2,不是吗?
import java.util.concurrent.*;
public class MonkeyCounter {
private static AtomicInteger monkey1 = new AtomicInteger(0);
private static AtomicLong monkey2 = new AtomicLong(0);
public static void main(String[] args) {
ExecutorService service = null;
try {
service = Executors.newSingleThreadExecutor();
for(int i=0; i<100; i++)
service.submit(() -> monkey1.getAndIncrement());
for(int i=0; i<100; i++)
service.submit(() -> monkey2.incrementAndGet());
System.out.println(monkey1+" "+monkey2);
} finally {
if(service != null) service.shutdown();
}
}
}
霍尔格纠正我后,Executors.newSingleThreadExecutor()
任务保证按顺序执行。
在下面的示例中,即使提交的第一批任务阻塞了 5 秒,
除println
.
外无阻塞任务比第二批任务先完成
示例,
import java.util.concurrent.*;
public class ExecutorServiceTests {
public static void main(String[] args) {
java.util.concurrent.ExecutorService service = Executors.newSingleThreadExecutor();
for (int i = 0; i < 5; i++) {
service.submit(() -> {
block(5000);
System.out.println("execution1");
});
}
for (int i = 0; i < 5; i++) {
service.submit(() -> {
System.out.println("execution2");
});
}
service.shutdown();
}
private static void block(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
输出:
execution1
execution1
execution1
execution1
execution1
execution2
execution2
execution2
execution2
execution2
Executors.newSingleThreadExecutor 上的 Javadoc:
Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.
任务被放入队列中。队列是 FIFO,因此对于单线程,如果 none 的增量失败,则保证 monkey1 > monkey2
。
请记住,monkey1
和 monkey2
的值尚未确定,因为您没有等待作业完成。
由于服务是单线程的,monkey1 循环系列总是在 monkey2 之前执行,所以我们可以预期 monkey1 总是大于 monkey2,不是吗?
import java.util.concurrent.*;
public class MonkeyCounter {
private static AtomicInteger monkey1 = new AtomicInteger(0);
private static AtomicLong monkey2 = new AtomicLong(0);
public static void main(String[] args) {
ExecutorService service = null;
try {
service = Executors.newSingleThreadExecutor();
for(int i=0; i<100; i++)
service.submit(() -> monkey1.getAndIncrement());
for(int i=0; i<100; i++)
service.submit(() -> monkey2.incrementAndGet());
System.out.println(monkey1+" "+monkey2);
} finally {
if(service != null) service.shutdown();
}
}
}
霍尔格纠正我后,Executors.newSingleThreadExecutor()
任务保证按顺序执行。
在下面的示例中,即使提交的第一批任务阻塞了 5 秒,
除println
.
示例,
import java.util.concurrent.*;
public class ExecutorServiceTests {
public static void main(String[] args) {
java.util.concurrent.ExecutorService service = Executors.newSingleThreadExecutor();
for (int i = 0; i < 5; i++) {
service.submit(() -> {
block(5000);
System.out.println("execution1");
});
}
for (int i = 0; i < 5; i++) {
service.submit(() -> {
System.out.println("execution2");
});
}
service.shutdown();
}
private static void block(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
输出:
execution1
execution1
execution1
execution1
execution1
execution2
execution2
execution2
execution2
execution2
Executors.newSingleThreadExecutor 上的 Javadoc:
Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.
任务被放入队列中。队列是 FIFO,因此对于单线程,如果 none 的增量失败,则保证 monkey1 > monkey2
。
请记住,monkey1
和 monkey2
的值尚未确定,因为您没有等待作业完成。