package com.jack.test3; public class MyRunnable implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + "卖票中"); } }
package com.jack.test3;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class main {
public static void main(String[] args) {
//创建线程池
ExecutorService t = Executors.newFixedThreadPool(2);
t.submit(new MyRunnable());
t.submit(new MyRunnable());
t.submit(new MyRunnable());
//lamda表达式,替代匿名类对象
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + "执行中");
}).start();
//销毁线程池
t.shutdown();
}
}
pool-1-thread-1卖票中
pool-1-thread-2卖票中
pool-1-thread-1卖票中
Thread-0执行中
Process finished with exit code 0