在JUC高并发中会有详细讲解
并发线程很多,有很多线程执行的时间很短,就需要频繁的创建和销毁,使得系统效率降低。
提前创建好多个线程,放入线程池,使用时直接提取,使用后放回线程池,增加线程的复用性。
提高了程序执行的效率。
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
public class CallableTest {
public static void main(String[] args) {
//1.提供指定数量的线程池
ExecutorService service= Executors.newFixedThreadPool(10);
ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
//设置线程池属性
// System.out.println(service.getClass());//ThreadPoolExecutor
service1.setMaximumPoolSize(50);//设置线程池线程数上限
//2.执行指定线程的操作。需要实现runnable接口或者callable接口实现类的对象
numberSingle ns=new numberSingle();
numberDouble nd=new numberDouble();
service.execute(ns);//适合runnable
service.execute(nd);
// service.submit(Callable callable);//适合Callable
//3.线程池关闭
service.shutdown();
}
}
class numberSingle implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if(i%2==0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}
class numberDouble implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if(i%2!=0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}
推荐阅读:
vue.js:634 [Vue warn]: Property or method "userName" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See:
SpringBoot 整合 webapp时 访问404的解决办法
状态码:415,发送HTTP请求返回415状态码的解决办法
SpringCloud怎么调用多个服务的信息
Vue 打开页面时就加载方法,例如查询
Spring整合Mybatis
SpringCloud 加入 thymleaf前端页面的方法
Controller之间的跳转
Spring AOP的实现原理
This may be the result of an unspecified view, due to default view name generation
行动消除疑虑