天天看點

線程池和直接建立線程的簡單性能對比

轉自https://blog.csdn.net/u014104286/article/details/71775038

都說線程池的性能比直接建立線程(新學了一個詞,叫野線程😂)性能要高,簡單的做了個實驗,未必準确僅供參考,先上結論

實驗平台mac系統,8代i7處理器,16G記憶體,十萬次建立線程,差距是50倍以上。

通過線程池建立:

package concurrent.performanceTestWithOriginThread;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class OneHundredThousandWithThreadPool {

  private ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
  //	private ArrayList<String> queue = new ArrayList<String>();
//	private CyclicBarrier barrier = new CyclicBarrier(10000000);
  private CountDownLatch latch = new CountDownLatch(100000);
  ExecutorService excutor = Executors.newFixedThreadPool(4);

  public static void main(String[] args) {
    OneHundredThousandWithThreadPool test001 = new OneHundredThousandWithThreadPool();
    long timeStart = System.currentTimeMillis();
    test001.start();
    System.out.println(System.currentTimeMillis() - timeStart);
  }

  public void start() {
    for (int i = 0; i < 100000; i++) {
      Runnable001 runnable001 = this.new Runnable001(i);
      excutor.submit(runnable001);
//			new Thread(runnable001).start();
    }
    excutor.shutdown();
    try {
      //等待latch計數為0
      latch.await();
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println(queue.size());
  }

  private class Runnable001 implements Runnable {
    private int value;

    public Runnable001(int value) {
      this.value = value;
    }

    @Override
    public void run() {
      try {
//				barrier.await();
      } catch (Exception e) {
        e.printStackTrace();
      }
      queue.offer(value + "");
      latch.countDown();//latch計數減一
    }

  }
}

           

線程池和直接建立線程的簡單性能對比

通過野線程建立:

package concurrent.performanceTestWithOriginThread;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;

public class OneHundredThousandWithThread
{
  private ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
  //	private ArrayList<String> queue = new ArrayList<String>();
//	private CyclicBarrier barrier = new CyclicBarrier(10000000);
  private CountDownLatch latch = new CountDownLatch(100000);

  public static void main(String[] args) {
    OneHundredThousandWithThread test001 = new OneHundredThousandWithThread();
    long timeStart = System.currentTimeMillis();
    test001.start();
    System.out.println(System.currentTimeMillis() - timeStart);
  }

  public void start() {
    for (int i = 0; i < 100000; i++) {
      OneHundredThousandWithThread.Runnable001 runnable001 = this.new Runnable001(i);
//      excutor.submit(runnable001);
			new Thread(runnable001).start();
    }
    try {
      //等待latch計數為0
      latch.await();
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println(queue.size());
  }

  private class Runnable001 implements Runnable {
    private int value;

    public Runnable001(int value) {
      this.value = value;
    }

    @Override
    public void run() {
      try {
//				barrier.await();
      } catch (Exception e) {
        e.printStackTrace();
      }
      queue.offer(value + "");
      latch.countDown();//latch計數減一
    }
  }
}
           
線程池和直接建立線程的簡單性能對比

繼續閱讀