publicclasstestimplementsRunnable{ privatestaticfinal SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:m:ss"); int i=0;
publicstaticvoidmain(String[] args){ ExecutorService es= Executors.newFixedThreadPool(10); for (int i = 0; i <10 ; i++) { es.execute(new test()); } } @Override publicvoidrun(){ try { Date t=sdf.parse("2019-12-28 15:19:"+i/60); System.out.println(i+":"+t); } catch (ParseException e) { e.printStackTrace(); } } }
Exception in thread "pool-1-thread-6" Exception in thread "pool-1-thread-4" Exception in thread "pool-1-thread-7" Exception in thread "pool-1-thread-1" Exception in thread "pool-1-thread-2" java.lang.NumberFormatException: multiple points at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at java.text.DigitList.getDouble(DigitList.java:169) at java.text.DecimalFormat.parse(DecimalFormat.java:2089) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at test.run(test.java:20) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) java.lang.NumberFormatException: multiple points at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at java.text.DigitList.getDouble(DigitList.java:169) at java.text.DecimalFormat.parse(DecimalFormat.java:2089) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at test.run(test.java:20) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at test.run(test.java:20) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Exception in thread "pool-1-thread-5" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at test.run(test.java:20) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at test.run(test.java:20) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at test.run(test.java:20) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)0:Sat Dec 2815:19:00 CST 2019 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019
0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019 0:Sat Dec 2815:19:00 CST 2019
publicvoidset(T value){ Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); }
public T get(){ Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } return setInitialValue(); }
public Random getRandom(){ if(mode==0){ return rnd;// }elseif (mode==1){ return tRnd.get();//ThreadLocal }else { returnnull; } }
@Override public Long call()throws Exception { long b=System.currentTimeMillis(); for (long i = 0; i <GEN_COUNT ; i++) { getRandom().nextInt();//返回随机数,10000000次 } long e=System.currentTimeMillis(); System.out.println(Thread.currentThread().getName()+"花费"+(e-b)+"ms"); return e-b;// java5提供了Future接口来代表Callable接口里的call()方法的返回值,并为Future接口提供了一个FutureTask实现类,该实现类实现了Future接口,并实现了Runnable接口,所以这样可以作为Thread的target。 } }
publicstaticvoidmain(String[] args)throws ExecutionException, InterruptedException { Future<Long>[] f=new Future[THREAD_COUNT];//制作四个线程组 for (int i = 0; i <THREAD_COUNT ; i++) { f[i]=es.submit(new RndTask(0));//提交普通的线程 } long totaltime=0; for (int i = 0; i <THREAD_COUNT ; i++) { totaltime+=f[i].get();//获取返回值 } System.out.println("多线程访问同一个Random实例"+totaltime+"ms");
//Threadlocal for (int i = 0; i <THREAD_COUNT ; i++) { f[i]=es.submit(new RndTask(1));//提交ThreadLocal } totaltime=0; for (int i = 0; i <THREAD_COUNT ; i++) { totaltime+=f[i].get();//获取返回值 } System.out.println("使用ThreadLocal包装Random实例"+totaltime+"ms"); es.shutdown(); } }
这个测试,一个使用全局的Random实例,被多个线程访问,每个线程都执行了100000次return new Random(123)的操作,总共产生了40000000次Random对象。而使用ThreadLocal去包装Random,总共就创建了4次Random对象,之后只是在不断地访问它而已,可见得,效率有着显著的提高。