🔄Update: 创建线程池
This commit is contained in:
@@ -1,11 +1,34 @@
|
|||||||
package cn.hezhaohui.threadpool;
|
package cn.hezhaohui.threadpool;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class ThreadPoolApplication {
|
public class ThreadPoolApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
SpringApplication.run(ThreadPoolApplication.class, args);
|
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||||
|
for (int i = 0; i < 100000000; i++) {
|
||||||
|
executorService.execute(() -> {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
int random = new Random().nextInt(1000);
|
||||||
|
for (int j = 1; j < random; j++) {
|
||||||
|
builder.append(j);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
TimeUnit.HOURS.sleep(1);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.error("[Error]");
|
||||||
|
}
|
||||||
|
log.info(builder.toString());
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
executorService.shutdown();
|
||||||
|
executorService.awaitTermination(1, TimeUnit.HOURS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user