直接上代码
首先单线程》》》》》》
创建一个1000的循环,里面第一次用单线程跑,一共用时(开始时间:::1619514363141 结束时间 1619514364419 共用时:1278毫秒 )
package com.hansight.atom.custom.webMonitor; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class Test { public static void main(String[] args) throws Exception{ System.out.println("开始时间:::"+System.currentTimeMillis()); for (int i=0;i<10000;i++){ Thread t = new Thread(new TestRunnable(i)); t.start(); } } }
package com.hansight.atom.custom.webMonitor; import java.text.SimpleDateFormat; import java.util.Date; public class TestRunnable implements Runnable { private int i; public TestRunnable(int i){ this.i = i; } @Override public void run() { try { System.out.println(i+"*********" +System.currentTimeMillis()); }catch (Exception e){ e.printStackTrace(); } } }
下面创建一个线程池》》》》》》》
package com.hansight.atom.custom.webMonitor; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class Test { public static void main(String[] args) throws Exception{ ThreadPoolExecutor executor = new ThreadPoolExecutor(5,5,10, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>()); System.out.println("开始时间:::"+System.currentTimeMillis()); for (int i=0;i<10000;i++){ executor.execute(new TestRunnable(i)); } } }
一共用时(开始时间:::1619514741430 结束时间 1619514741540 共用时:110毫秒 )