java中线程切换的开销

思路:

开三个线程A,B,C

线程A不断的调用LockSupport.park()阻塞自己,一旦发现自己被唤醒,调用Thread.interrupted()清除interrupt标记位,同时增加自增计数

线程B不断的调用线程A的interrupt()方法,将线程A从阻塞中唤醒,一旦唤醒成功,则自增计数

线程C定时输出计数

 

代码如下

 1 import java.util.concurrent.atomic.AtomicInteger;
 2 import java.util.concurrent.locks.LockSupport;
 3 
 4 /**
 5  * Created by cc on 2017/2/6.
 6  */
 7 public class Test {
 8     public static void main(String[] args) {
 9         final AtomicInteger count = new AtomicInteger(0);
10         final Thread thread = new Thread(new Runnable() {
11             public void run() {
12                 while (true) {
13                     LockSupport.park();
14                     Thread.interrupted();//clear interrupt flag
15                 }
16             }
17         });
18 
19         for(int i =0;i<1;i++) {
20             new Thread(new Runnable() {
21                 public void run() {
22                     while (true) {
23                         if (!thread.isInterrupted()) {
24                             thread.interrupt();
25                             count.incrementAndGet();
26                         }
27                     }
28                 }
29             }).start();
30         }
31         new Thread(new Runnable() {
32             public void run() {
33                 long last = 0;
34                 while (true) {
35                     try {
36                         Thread.sleep(1000);
37                         System.out.println(String.format("thread park %d times in 1s", count.get() - last));
38                         last = count.get();
39                     } catch (InterruptedException e) {
40                         e.printStackTrace();
41                     }
42                 }
43             }
44         }).start();
45 
46         thread.start();
47     }
48 }
View Code

 

测试环境是

CPU:I5 6500 ,默频3.2G,测试的时候睿频至3.3G出头

内存:ddr4 2400 8g * 2,双通道模式

 

测试结果如下

 1 thread park 380473 times in 1s
 2 thread park 376364 times in 1s
 3 thread park 371700 times in 1s
 4 thread park 374485 times in 1s
 5 thread park 375717 times in 1s
 6 thread park 380483 times in 1s
 7 thread park 370507 times in 1s
 8 thread park 382291 times in 1s
 9 thread park 377581 times in 1s
10 thread park 373198 times in 1s
11 thread park 371367 times in 1s
12 thread park 372876 times in 1s
13 thread park 394815 times in 1s
14 thread park 366434 times in 1s
15 thread park 391827 times in 1s
16 thread park 383691 times in 1s
17 thread park 380567 times in 1s
18 thread park 385234 times in 1s
19 thread park 367482 times in 1s
20 thread park 373650 times in 1s
21 thread park 375471 times in 1s
22 thread park 383743 times in 1s
23 thread park 377532 times in 1s
24 thread park 377353 times in 1s
25 thread park 386828 times in 1s
26 thread park 374503 times in 1s
27 thread park 373831 times in 1s
28 thread park 396207 times in 1s
29 thread park 374918 times in 1s
30 thread park 370150 times in 1s
31 thread park 378990 times in 1s
32 thread park 375449 times in 1s
33 thread park 406158 times in 1s
34 thread park 389793 times in 1s
35 thread park 371424 times in 1s
36 thread park 354746 times in 1s
37 thread park 384065 times in 1s
38 thread park 378894 times in 1s
39 thread park 358754 times in 1s
40 thread park 372588 times in 1s
View Code

大概每秒钟能完成38w次线程切换,平均每次切换耗时2.63us

 

做个对比

在单线程运行时,每秒钟可以完成1.6亿次的AtomicLong.increaseAndGet()操作,可以完成33亿次long型变量的自增操作

 

若干个数量级的差距,所以线程切换是一个开销很大的操作,应当尽量注意

posted @ 2017-02-21 01:10  qeDVuHG  阅读(5398)  评论(0编辑  收藏  举报