java執行緒測試

killalllsd發表於2010-07-22

測試解決的問題:

執行緒的上下文切換真的很耗時麼,耗時的話到底耗時到什麼程度。

測試的機器是4核cpu,處理同樣多的任務,看看耗時情況。

程式碼如下:

    private static void testContextSwitch()
    {
        AtomicInteger count = new AtomicInteger(0);
        long totolLoopNum = 1000000000;
        int threadNum = 1000;
        long loopNum = totolLoopNum/threadNum;
        System.out.println("totolLoopNum:"+totolLoopNum);
        System.out.println("threadNum:"+threadNum+"\tloopNum:"+loopNum);
        ExecutorService e = Executors.newFixedThreadPool(threadNum);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < threadNum; i++)
        {
            e.execute(createLoopRunnable(count, loopNum, threadNum, startTime));
        }

    }

    private static Runnable createLoopRunnable(final AtomicInteger count, final long loopNum, final int threadNum,
            final long startTime)
    {
        return new Runnable()
        {

            @Override
            public void run()
            {
                for (int j = 0; j < 100; j++)
                {
                    int i = 0;
                    while (true)
                    {
                        if (i > loopNum)
                            break;
                        i++;
                    }
                }  
                int total = count.incrementAndGet();
                if (total == threadNum)
                    System.out.println("cost:" + (System.currentTimeMillis() - startTime));
            }
        };
    }

 

輸出結果如下:

//    	totolLoopNum:1000000000
//    	threadNum:1	loopNum:1000000000
//    	cost:127198 cpu使用20%-40%
    	
//    	totolLoopNum:1000000000
//    	threadNum:2	loopNum:500000000
//    	cost:67120 cpu使用50%
    	
//    	totolLoopNum:1000000000
//    	threadNum:4	loopNum:250000000
//    	cost:36015 cpu使用100%
    	
//    	totolLoopNum:1000000000
//    	threadNum:64	loopNum:15625000
//    	cost:35905 cpu使用100%
    	
//    	totolLoopNum:1000000000
//    	threadNum:512	loopNum:1953125
//    	cost:35905

//    	totolLoopNum:1000000000
//    	threadNum:1000	loopNum:1000000
//    	cost:35955

 

可以看出:

1.線上程小於4的情況下cpu是不能跑到100%的。

2.隨著執行緒數目的增加,處理任務的耗時越來越短,知道開到1000個執行緒的時候才開始有微弱的增加。

可以看出執行緒的上下文切換貌似不是很耗時。。。

 

實際應用中,比如阻塞讀資料。經常會寫如下類似程式碼:

//socket連線沒有中斷,阻塞讀資料
while(socket != null)
{
 //阻塞的讀寫資料
}

程式碼1:

				while (true) {
					// 阻塞的讀寫資料
					try {
						if (queue.size() != 0) {
							System.out.println(1);
							// read;
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

 程式碼2:

 

                                while (true) {
					// 阻塞的讀寫資料
					try {
						if (queue.take() != null) {
							System.out.println(1);
							// read;
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
 

  加入幾個阻塞執行緒到執行緒池中發現,程式碼2加入後消耗的實踐較少,程式碼1加入後消耗的實踐較多。

通過jstack分析發現:

程式碼2中的執行緒處於WAITING狀態,而程式碼1所有執行緒都是RUNNABLE。

java.util.concurrent.locks.LockSupport

http://forums.sun.com/thread.jspa?threadID=5353864

 

"pool-1-thread-3" prio=6 tid=0x01a0f800 nid=0x1fc4 waiting on condition [0x0c07f000..0x0c07fbe8]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x03ae66f0> (a java.util.concurrent.SynchronousQueue$TransferStack)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
        at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:422)
        at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:323)
        at java.util.concurrent.SynchronousQueue.take(SynchronousQueue.java:857)
        at lsd.Main$2.run(Main.java:205)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:619)

相關文章