java中线程切换的开销
思路:
开三个线程A,B,C
线程A不断的调用LockSupport.park()阻塞自己,一旦发现自己被唤醒,调用Thread.interrupted()清除interrupt标记位,同时增加自增计数
线程B不断的调用线程A的interrupt()方法,将线程A从阻塞中唤醒,一旦唤醒成功,则自增计数
线程C定时输出计数
代码如下
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport; /**
* Created by cc on 2017/2/6.
*/
public class Test {
public static void main(String[] args) {
final AtomicInteger count = new AtomicInteger(0);
final Thread thread = new Thread(new Runnable() {
public void run() {
while (true) {
LockSupport.park();
Thread.interrupted();//clear interrupt flag
}
}
}); for(int i =0;i<1;i++) {
new Thread(new Runnable() {
public void run() {
while (true) {
if (!thread.isInterrupted()) {
thread.interrupt();
count.incrementAndGet();
}
}
}
}).start();
}
new Thread(new Runnable() {
public void run() {
long last = 0;
while (true) {
try {
Thread.sleep(1000);
System.out.println(String.format("thread park %d times in 1s", count.get() - last));
last = count.get();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start(); thread.start();
}
}
测试环境是
CPU:I5 6500 ,默频3.2G,测试的时候睿频至3.3G出头
内存:ddr4 2400 8g * 2,双通道模式
测试结果如下
thread park 380473 times in 1s
thread park 376364 times in 1s
thread park 371700 times in 1s
thread park 374485 times in 1s
thread park 375717 times in 1s
thread park 380483 times in 1s
thread park 370507 times in 1s
thread park 382291 times in 1s
thread park 377581 times in 1s
thread park 373198 times in 1s
thread park 371367 times in 1s
thread park 372876 times in 1s
thread park 394815 times in 1s
thread park 366434 times in 1s
thread park 391827 times in 1s
thread park 383691 times in 1s
thread park 380567 times in 1s
thread park 385234 times in 1s
thread park 367482 times in 1s
thread park 373650 times in 1s
thread park 375471 times in 1s
thread park 383743 times in 1s
thread park 377532 times in 1s
thread park 377353 times in 1s
thread park 386828 times in 1s
thread park 374503 times in 1s
thread park 373831 times in 1s
thread park 396207 times in 1s
thread park 374918 times in 1s
thread park 370150 times in 1s
thread park 378990 times in 1s
thread park 375449 times in 1s
thread park 406158 times in 1s
thread park 389793 times in 1s
thread park 371424 times in 1s
thread park 354746 times in 1s
thread park 384065 times in 1s
thread park 378894 times in 1s
thread park 358754 times in 1s
thread park 372588 times in 1s
大概每秒钟能完成38w次线程切换,平均每次切换耗时2.63us
做个对比
在单线程运行时,每秒钟可以完成1.6亿次的AtomicLong.increaseAndGet()操作,可以完成33亿次long型变量的自增操作
若干个数量级的差距,所以线程切换是一个开销很大的操作,应当尽量注意
java中线程切换的开销的更多相关文章
- java中线程同步的理解(非常通俗易懂)
转载至:https://blog.csdn.net/u012179540/article/details/40685207 Java中线程同步的理解 我们可以在计算机上运行各种计算机软件程序.每一个运 ...
- Java多线程并发03——在Java中线程是如何调度的
在前两篇文章中,我们已经了解了关于线程的创建与常用方法等相关知识.接下来就来了解下,当你运行线程时,线程是如何调度的.关注我的公众号「Java面典」了解更多 Java 相关知识点. 多任务系统往往需要 ...
- java中线程分两种,守护线程和用户线程。
java中线程分为两种类型:用户线程和守护线程. 通过Thread.setDaemon(false)设置为用户线程: 通过Thread.setDaemon(true)设置为守护线程. 如果不设置次属性 ...
- java中线程机制
java中线程机制,一开始我们都用的单线程.现在接触到多线程了. 多线性首先要解决的问题是:创建线程,怎么创建线程的问题: 1.线程的创建: 四种常用的实现方法 1.继承Thread. Thread是 ...
- Java中线程的使用 (2)-多线程、线程优先级、线程睡眠、让步、阻塞
Java中线程的使用 (2)-多线程.线程优先级.线程睡眠.让步.阻塞 (一)多线程使用方法 说明:创建每个新的线程,一定要记得启动每个新的线程(调用.start()方法) class Xc3 ext ...
- Java中线程的实现:
Java中线程的实现: 一.线程简介: 实现的两种方式为: 1.Thread类 2.Runnable接口 都在java.lang中 都有共通的方法:public void run() 二.线程常用方法 ...
- JAVA中线程同步方法
JAVA中线程同步方法 1 wait方法: 该方法属于Object的方法,wait方法的作用是使得当前调用wait方法所在部分(代码块)的线程停止执行,并释放当前获得的调用wait所 ...
- 多线程(三) java中线程的简单使用
java中,启动线程通常是通过Thread或其子类通过调用start()方法启动. 常见使用线程有两种:实现Runnable接口和继承Thread.而继承Thread亦或使用TimerTask其底层依 ...
- Java中线程池,你真的会用吗?
在<深入源码分析Java线程池的实现原理>这篇文章中,我们介绍过了Java中线程池的常见用法以及基本原理. 在文中有这样一段描述: 可以通过Executors静态工厂构建线程池,但一般不建 ...
随机推荐
- 09GNU C语言程序编译
1. C 语言程序概述 GNU gcc 对 ISO 标准 C89 描述的 C 语言进行了一些扩展,其中一些扩展部分已经包括进 IOS C99 标准中.本节给出了内核中经常用到的一些 gcc 扩展语 ...
- Linux磁盘分区介绍
分区?我们不是已经在BIOS界面分区好了吗?如果领导给你一块磁盘,你怎么用呢?所以就有了分区工具(fdisk和parted),fdisk工具只针对小于2T磁盘分区,且是交互式的:parted很强大,通 ...
- jdk生成证书,网站请求变成https
生成证书的步骤 1.进入jdk的bin目录 keytool -genkey -alias tomcat -keyalg RSA 命名证书的名字叫tomcat 2.将证书拷贝至tomcat的bin目 ...
- session属性的清除和非法登录
有的项目会将登录用户的资料存于session的一个属性中,这样方便获取一些数据使用,但是用户退出时需要将session的这个属性清除,一面造成一些不必要的麻烦,但是有些时候,在后台清除了这个属性,在拦 ...
- day07 类的进阶,socket编程初识
类的静态方法: 正常: 添加静态方法: 就会变成一个函数,不会自动传self 参数,不会调用类的变量和实例的变量 不在需要self 名义上归类管,但是它就是一个单独的函数,不在需要传入self,想怎 ...
- 流式处理框架storm浅析(上篇)
本文来自网易云社区 作者:汪建伟 前言 前一段时间参与哨兵流式监控功能设计,调研了两个可以做流式计算的框架:storm和spark streaming,我负责storm的调研工作.断断续续花了一周的时 ...
- ogre3D学习基础8 --- 资源管理器
资源管理 可管理的资源有: 材质资源:在.material文件中包含的材质脚本定义(技术.通路.纹理单元等数据的定义). 模型资源:经过优化的二进制网格模型文件,扩展名为.mesh.包含几何信息和一些 ...
- ant 入门级详解
ant 入门级详解 [转载的地址(也是转载,未找到原文地址)]https://www.cnblogs.com/jsfx/p/6233645.html 1,什么是antant是构建工具2,什么是构建 ...
- Ext.js给form加背景图片
{ iconCls: 'zyl_icons_showdetail', tooltip: '查看', handler: function(gridView, rowIndex, colIndex) { ...
- 【POJ 2585】Window Pains 拓扑排序
Description . . . and so on . . . Unfortunately, Boudreaux's computer is very unreliable and crashes ...