定长线程池,可控制线程最大并发数,超出的线程会在队列中等待
Executors的方式创建定长线程池(不推荐容易,容易内存溢出OOM)ThreadPoolExecutor构造函数创建定长线程池(推荐)
定长线程池,可控制线程最大并发数,超出的线程会在队列中等待当我们在开发大屏的时候一个各大平会返回多个接口每个接口返回速度都不一样,会导致有的模块加载出来,有的模块未加载出来,用这种定长的线程方式,用一个接口返回,使全部模块统一加载,这样不仅能提高客户使用的感官,也可以大大提升前端开发效率
Executors的方式创建定长线程池(不推荐容易,容易内存溢出OOM)package org.data.source.controller; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestFixedThreadPoolController { //创建线程池 ExecutorService exe = Executors.newFixedThreadPool(3); @RequestMapping("/FixedThreadPool") public String test(){ FutureThreadPoolExecutor构造函数创建定长线程池(推荐)s1 = exe.submit(new Callable () { @Override public String call() throws Exception { System.out.println("执行了1"); return "1"; } }); Future s2 = exe.submit(new Callable () { @Override public String call() throws Exception { System.out.println("执行了2"); return "2"; } }); Future s3 = exe.submit(new Callable () { @Override public String call() throws Exception { System.out.println("执行了3"); return "3"; } }); //无返回值,接口返回了不,此方法继续执行直至结束 exe.execute(new Runnable() { //execute执行任务,自动调用run方法 @Override public void run() { try { TimeUnit.MILLISECONDS.sleep(50000); //睡50000毫秒 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("执行了无返回值"); } }); String result = null; try { return s1.get()+s2.get()+s3.get(); }catch (Exception e){ result = "error"; } return result; } }
package org.data.source.controller; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.linkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestFixedThreadPoolNewController { public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new linkedBlockingQueue()); } //创建个线程 ExecutorService exe = newFixedThreadPool(4); @RequestMapping("/FixedThreadPoolNew") public String test(){ Future s1 = exe.submit(new Callable () { @Override public String call() throws Exception { System.out.println("执行了1"); return "1"; } }); Future s2 = exe.submit(new Callable () { @Override public String call() throws Exception { System.out.println("执行了2"); return "2"; } }); Future s3 = exe.submit(new Callable () { @Override public String call() throws Exception { System.out.println("执行了3"); return "3"; } }); //无返回值,接口返回了不,此方法继续执行直至结束 exe.execute(new Runnable() { //execute执行任务,自动调用run方法 @Override public void run() { try { TimeUnit.MILLISECONDS.sleep(50000); //睡50000毫秒 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("执行了无返回值"); } }); String result = null; try { return s1.get()+s2.get()+s3.get(); }catch (Exception e){ result = "error"; } return result; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)