创建一个无界的可缓存的线程池,若线程长时间没用会自动销毁,直接上代码好了: import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Threadd3 { public static void main(String[] args) { ExecutorService es = Executors.newCachedThreadPool(); run r = n…
class MyThread implements Runnable{ private int index; public MyThread(int index){ this.index = index; } @Override public void run() { System.out.println("处理任务:" + index); } } public class Test1 { public static void main(String[] args){ // 创建线程池…
newCachedThreadPool 缓存默认60s 猜下你的结果 package com.juc.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * Created by Administrator on 2018/6/27. */ public class CachedThreadPoolDemo { public static void…
1.PriorityBlockingQueue里面存储的对象必须是实现Comparable接口. 2.队列通过这个接口的compare方法确定对象的优先级priority. 规则是:当前和其他对象比较,如果compare方法返回负数,那么在队列里面的优先级就比较高. 下面的测试可以说明这个断言: 查看打印结果,比较take出来的Entity和left的entity,比较他们的priority public class TestPriorityQueue { static Random r=new…