マルチスレッド(4)-スレッドプール
3969 ワード
Java 5の後にスレッドプールThreadPoolが提供され、
じょうふごう
実際のコードはtry catch finallyを使用することに注意し、必要に応じてfinallyでスレッドプールを閉じます.上記コードはjdk 1にある.8以降lambda式を使用すると
戻り結果がある場合はrunnableの代わりにcallableとfutureを使用します.
コード学的姿勢
Callableタスクのセットをコミットする必要がある場合は、CompletionServiceを使用して、期間take()メソッドを使用して対応するFutureオブジェクトを返します.
じょうふごう
concurrent Executors , , 。
newFixedThreadPool ,newCachedThreadPool ( ),newSingleThreadExecutor ( ),newScheduledThreadPool( ).....
じょうふごう
public class ThreadPoolTest {
public static void main(String[] args) {
//ExecutorService threadPool = Executors.newFixedThreadPool(3);
//ExecutorService threadPool = Executors.newCachedThreadPool();
ExecutorService threadPool = Executors.newSingleThreadExecutor();
for(int i=1;i<=10;i++){
final int task = i;
threadPool.execute(new Runnable(){
@Override
public void run() {
for(int j=1;j<=10;j++){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + task);
}
}
});
}
System.out.println("all of 10 tasks have committed! ");
//threadPool.shutdownNow();
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(
new Runnable(){
@Override
public void run() {
System.out.println("bombing!");
}},
6,
2,
TimeUnit.SECONDS);
}
}
実際のコードはtry catch finallyを使用することに注意し、必要に応じてfinallyでスレッドプールを閉じます.上記コードはjdk 1にある.8以降lambda式を使用すると
public class ThreadPoolTest {
public static void main(String[] args) {
//ExecutorService threadPool = Executors.newFixedThreadPool(3);
//ExecutorService threadPool = Executors.newCachedThreadPool();
ExecutorService threadPool = Executors.newSingleThreadExecutor();
for(int i=1;i<=10;i++){
final int task = i;
threadPool.execute(() -> {
for(int j=1;j<=10;j++){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + task);
}
});
}
System.out.println("all of 10 tasks have committed! ");
//threadPool.shutdownNow();
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(
() -> System.out.println("bombing!"),
6,
2,
TimeUnit.SECONDS);
}
}
戻り結果がある場合はrunnableの代わりにcallableとfutureを使用します.
コード学的姿勢
ExecutorService threadPool = Executors.newSingleThreadExecutor();
Future future =
threadPool.submit(
() -> {
Thread.sleep(2000);
return "hello";
}
);
System.out.println(" ");
try {
System.out.println(" :" + future.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Callableタスクのセットをコミットする必要がある場合は、CompletionServiceを使用して、期間take()メソッドを使用して対応するFutureオブジェクトを返します.
じょうふごう
ExecutorService threadPool2 = Executors.newFixedThreadPool(10);
CompletionService completionService = new ExecutorCompletionService(threadPool2);
for(int i=1;i<=10;i++){
final int seq = i;
completionService.submit(() -> {
Thread.sleep(new Random().nextInt(5000));
return seq;
});
}
for(int i=0;i<10;i++){
try {
System.out.println(
completionService.take().get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}