如果你提交一个runnable给一个多线程的executor service,那么多线程会执行那个runnable吗?
If you submit one runnable to an executor service with multiple threads, will multiple threads execute that runnable?
我很难理解 ExecutorService 在 Java 中的工作原理 8. 我试图理解这个网站上的一些代码:https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/
特别是在他测试不同地图的运行时间的最后。这是代码:
public class CrunchifyConcurrentHashMapVsSynchronizedMap {
public final static int THREAD_POOL_SIZE = 5;
public static Map<String, Integer> crunchifyHashTableObject = null;
public static Map<String, Integer> crunchifySynchronizedMapObject = null;
public static Map<String, Integer> crunchifyConcurrentHashMapObject = null;
public static void main(String[] args) throws InterruptedException {
// Test with Hashtable Object
crunchifyHashTableObject = new Hashtable<String, Integer>();
crunchifyPerformTest(crunchifyHashTableObject);
// Test with synchronizedMap Object
crunchifySynchronizedMapObject = Collections.synchronizedMap(new HashMap<String, Integer>());
crunchifyPerformTest(crunchifySynchronizedMapObject);
// Test with ConcurrentHashMap Object
crunchifyConcurrentHashMapObject = new ConcurrentHashMap<String, Integer>();
crunchifyPerformTest(crunchifyConcurrentHashMapObject);
}
public static void crunchifyPerformTest(final Map<String, Integer> crunchifyThreads) throws InterruptedException {
System.out.println("Test started for: " + crunchifyThreads.getClass());
long averageTime = 0;
for (int i = 0; i < 5; i++) {
long startTime = System.nanoTime();
ExecutorService crunchifyExServer = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
for (int j = 0; j < THREAD_POOL_SIZE; j++) {
crunchifyExServer.execute(new Runnable() {
@SuppressWarnings("unused")
@Override
public void run() {
for (int i = 0; i < 500000; i++) {
Integer crunchifyRandomNumber = (int) Math.ceil(Math.random() * 550000);
// Retrieve value. We are not using it anywhere
Integer crunchifyValue = crunchifyThreads.get(String.valueOf(crunchifyRandomNumber));
// Put value
crunchifyThreads.put(String.valueOf(crunchifyRandomNumber), crunchifyRandomNumber);
}
}
});
}
// Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation
// has no additional effect if already shut down.
// This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.
crunchifyExServer.shutdown();
// Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is
// interrupted, whichever happens first.
crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
long entTime = System.nanoTime();
long totalTime = (entTime - startTime) / 1000000L;
averageTime += totalTime;
System.out.println("500K entried added/retrieved in " + totalTime + " ms");
}
System.out.println("For " + crunchifyThreads.getClass() + " the average time is " + averageTime / 5 + " ms\n");
}
}
所以在 crunchifyPerformTest class 中,他启动了一个有 5 个线程的 ExecutorService,然后每次提交 5 个不同的 runnables 和 500k 读写到 hashmap?执行器服务会自动让 5 个线程执行每个可运行对象吗?
没有。每个 Runnable
只在一个线程上执行。这意味着所有 Runnable
将并行执行,因为 Runnable
的数量与可用线程的数量匹配。
您还可以提交 6 Runnable
。在这种情况下,其中 5 个将并行执行,一旦一个 Runnable
完成执行,第六个将被执行。
顺便说一下,我认为 docs 对行为非常清楚
ExecutorService
.
我很难理解 ExecutorService 在 Java 中的工作原理 8. 我试图理解这个网站上的一些代码:https://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/
特别是在他测试不同地图的运行时间的最后。这是代码:
public class CrunchifyConcurrentHashMapVsSynchronizedMap {
public final static int THREAD_POOL_SIZE = 5;
public static Map<String, Integer> crunchifyHashTableObject = null;
public static Map<String, Integer> crunchifySynchronizedMapObject = null;
public static Map<String, Integer> crunchifyConcurrentHashMapObject = null;
public static void main(String[] args) throws InterruptedException {
// Test with Hashtable Object
crunchifyHashTableObject = new Hashtable<String, Integer>();
crunchifyPerformTest(crunchifyHashTableObject);
// Test with synchronizedMap Object
crunchifySynchronizedMapObject = Collections.synchronizedMap(new HashMap<String, Integer>());
crunchifyPerformTest(crunchifySynchronizedMapObject);
// Test with ConcurrentHashMap Object
crunchifyConcurrentHashMapObject = new ConcurrentHashMap<String, Integer>();
crunchifyPerformTest(crunchifyConcurrentHashMapObject);
}
public static void crunchifyPerformTest(final Map<String, Integer> crunchifyThreads) throws InterruptedException {
System.out.println("Test started for: " + crunchifyThreads.getClass());
long averageTime = 0;
for (int i = 0; i < 5; i++) {
long startTime = System.nanoTime();
ExecutorService crunchifyExServer = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
for (int j = 0; j < THREAD_POOL_SIZE; j++) {
crunchifyExServer.execute(new Runnable() {
@SuppressWarnings("unused")
@Override
public void run() {
for (int i = 0; i < 500000; i++) {
Integer crunchifyRandomNumber = (int) Math.ceil(Math.random() * 550000);
// Retrieve value. We are not using it anywhere
Integer crunchifyValue = crunchifyThreads.get(String.valueOf(crunchifyRandomNumber));
// Put value
crunchifyThreads.put(String.valueOf(crunchifyRandomNumber), crunchifyRandomNumber);
}
}
});
}
// Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation
// has no additional effect if already shut down.
// This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.
crunchifyExServer.shutdown();
// Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is
// interrupted, whichever happens first.
crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
long entTime = System.nanoTime();
long totalTime = (entTime - startTime) / 1000000L;
averageTime += totalTime;
System.out.println("500K entried added/retrieved in " + totalTime + " ms");
}
System.out.println("For " + crunchifyThreads.getClass() + " the average time is " + averageTime / 5 + " ms\n");
}
}
所以在 crunchifyPerformTest class 中,他启动了一个有 5 个线程的 ExecutorService,然后每次提交 5 个不同的 runnables 和 500k 读写到 hashmap?执行器服务会自动让 5 个线程执行每个可运行对象吗?
没有。每个 Runnable
只在一个线程上执行。这意味着所有 Runnable
将并行执行,因为 Runnable
的数量与可用线程的数量匹配。
您还可以提交 6 Runnable
。在这种情况下,其中 5 个将并行执行,一旦一个 Runnable
完成执行,第六个将被执行。
顺便说一下,我认为 docs 对行为非常清楚
ExecutorService
.