package com.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * @author tzq * @date 2019-03-13 16:48 */ public class Test { //公共變量 int count=0; static List<Integer> list = new ArrayList<>(); public static void main(String[] args) throws Exception{ //new一個實現Runnable的類 Test test=new Test(); //創建1個任務 MyRunnable myRunnable1=test.new MyRunnable(); //創建5個線程 for(int i=0;i<99;i++){ Thread t = new Thread(myRunnable1); t.start(); t.join(); } System.out.println("線程結束"); System.out.println(list.size()); long s1=System.nanoTime(); for (Integer i :list){ } System.out.println("foreach程序耗時:"+(System.nanoTime()-s1)+" ms"); long s2=System.nanoTime(); Iterator iter = list.iterator(); while(iter.hasNext()){ Object o = iter.next(); } System.out.println("Iterator程序耗時:"+(System.nanoTime()-s2)+" ms"); long s3=System.nanoTime(); int size = list.size(); for(int i=0;i<size;i++){ Object o= list.get(i); } System.out.println("for程序耗時:"+(System.nanoTime()-s3)+" ms"); long s4=System.nanoTime(); for(int i=0;i<list.size();i++){ Object o= list.get(i); } System.out.println("for程序耗時:"+(System.nanoTime()-s4)+" ms"); long s5=System.nanoTime(); list.stream().forEach(x->{ Object o= x; }); System.out.println("stream程序耗時:"+(System.nanoTime()-s5)+" ms"); } //創建一個實現Runnable的類 class MyRunnable implements Runnable{ public void run() { while(true){ //鎖住的是同一對象 synchronized(this){ if(count>=1000000){ break; } list.add(count); System.out.println(Thread.currentThread().getName()+":count:"+(++count)); //測試時,線程更容易切換 Thread.yield(); } } } } }
foreach程序耗時:27658646 ms
Iterator程序耗時:12158150 ms
for程序耗時:7618257 ms
for程序耗時:7031677 ms
stream程序耗時:101515437 ms