0
0

More than 1 year has passed since last update.

executorservice interface

Last updated at Posted at 2023-01-16

newSingleThreadExecutor 1つのみ
newFixedThreadPool 指定した数のthreadpool
newCachedThreadPool 60秒経つと廃棄されるthreadpool

newCachedThreadPool sample

public class Outer {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService e = Executors.newCachedThreadPool();
        for(int i=0;i<5;i++) {
            e.submit(() -> {
                System.out.println("a:" + Thread.currentThread().getId());
            }); 
        }
        Thread.sleep(5000);
        for(int i=0;i<5;i++) {
            e.submit(() -> {
                System.out.println("a:" + Thread.currentThread().getId());
            }); 
        }
        Thread.sleep(70000);
        for(int i=0;i<5;i++) {
            e.submit(() -> {
                System.out.println("a:" + Thread.currentThread().getId());
            }); 
        }
    }
}
a:17
a:14
a:15
a:13
a:16
a:15
a:13
a:13
a:13
a:13
a:19
a:18
a:20
a:21
a:22
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0