Вот с таким решением не проходит и вывод не соответствует, хотя человек в комментариях здесь пишет, что с таким кодом прошло)
LinkedBlockingQueue<Runnable> linkedBlockingQueue = new LinkedBlockingQueue<>();
for (int i = 1; i <= 10; i++) {
int finalI = i;
linkedBlockingQueue.add(new Runnable() {
@Override
public void run() {
doExpensiveOperation(finalI);
}
});
}
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 5, 1000, TimeUnit.MILLISECONDS, linkedBlockingQueue);
threadPoolExecutor.prestartAllCoreThreads();
threadPoolExecutor.shutdownNow();
threadPoolExecutor.awaitTermination(5, TimeUnit.SECONDS);
}
Сам я пытался решить аналогично предыдущей задаче с таким кодом:
LinkedBlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();
ThreadPoolExecutor service = new ThreadPoolExecutor(3, 5, 1000, TimeUnit.MILLISECONDS, blockingQueue);
for (int i = 0; i < 10; i++) {
final int finalI = i+1;
service.submit(new Runnable() {
@Override
public void run() {
doExpensiveOperation(finalI);
}
});
service.prestartAllCoreThreads();
service.shutdown();
service.awaitTermination(5, TimeUnit.SECONDS);
}
}
Но так не прошло и выдавало ошибку в строке вызова service.submit.
Объясните пожалуйста.package com.javarush.task.task28.task2807;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/*
Знакомство с ThreadPoolExecutor
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
// Add your code here
LinkedBlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();
ThreadPoolExecutor service = new ThreadPoolExecutor(3, 5, 1000, TimeUnit.MILLISECONDS, blockingQueue);
for (int i = 0; i < 10; i++) {
final int finalI = i+1;
service.submit(new Runnable() {
@Override
public void run() {
doExpensiveOperation(finalI);
}
});
service.prestartAllCoreThreads();
service.shutdown();
service.awaitTermination(5, TimeUnit.SECONDS);
}
/* Example output
pool-1-thread-2, localId=2
pool-1-thread-3, localId=3
pool-1-thread-1, localId=1
pool-1-thread-3, localId=5
pool-1-thread-2, localId=4
pool-1-thread-3, localId=7
pool-1-thread-1, localId=6
pool-1-thread-3, localId=9
pool-1-thread-2, localId=8
pool-1-thread-1, localId=10
*/
}
private static void doExpensiveOperation(int localId) {
System.out.println(Thread.currentThread().getName() + ", localId=" + localId);
}
}