一个计时器,同时开启100个线程,每个线程休眠1ms钟后,将全局静态变量count加1,这100个线程创建完之后,休眠500ms,计算总耗时,程序如下:

public class Counter {

    public volatile static int count = 0;
public static void inc() {
// 这里延迟1毫秒,使得结果明显
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
count++;
}
public static void main(String[] args) {
long time1 = System.currentTimeMillis();
System.out.println();
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
Counter.inc();
}
}).start();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 这里每次运行的值都有可能不同,可能为100
System.out.println("Counter.count=" + Counter.count);
long time2 = System.currentTimeMillis();
System.out.println("耗时:"+(time2-time1));
}
}

运行结果:

Counter.count=96
耗时:506

Counter.count不等于100是因为count++线程不安全

加上同步代码块:

public class Counter {

	public volatile static int count = 0;
static Object obj = new Object();
public static void inc() {
// 这里延迟1毫秒,使得结果明显
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
synchronized (obj) {
count++;
}

} public static void main(String[] args) {
long time1 = System.currentTimeMillis();
System.out.println();
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
Counter.inc();
}
}).start();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 这里每次运行的值都有可能不同,可能为1000
System.out.println("Counter.count=" + Counter.count);
long time2 = System.currentTimeMillis();
System.out.println("耗时:"+(time2-time1));
}
}

运行结果:

Counter.count=100
耗时:507

那么synchronized对运行的速度有多大的影响,于是我分别100,1000,10000,100000与500000次,将未同步、synchronized (obj)与synchronized (Counter.class)结果对比,结果如下:

线程数量 未同步<测试更多的数据> synchronized (obj) synchronized (Counter.class)
100 Counter.count=97
耗时:508
<98-506><98-507><97-507><100-508>
Counter.count=100
耗时:507
<100-507><100-507><100-508><100-508>
Counter.count=100
耗时:506
<100-508><100-508><100-506><100-509>
1000 Counter.count=997
耗时:572
<993-562><994-562><995-561><990-561>
Counter.count=1000
耗时:562
<1000-572><1000-560><1000-559><1000-560>
Counter.count=1000
耗时:570
<1000-585><1000-560><1000-562><1000-561>
10000 Counter.count=9947
耗时:1067
<9951-1077><9962-1079><9936-1066><9953-1081>
Counter.count=10000
耗时:1074
<10000-1075><10000-1068><10000-1079><10000-1074>
Counter.count=10000
耗时:1072
<10000-1089><10000-1076><10000-1108><10000-1076>
100000 Counter.count=99399
耗时:6098
<99347-6111><99374-6099><99416-6196><99459-6137>
Counter.count=100000
耗时:6103

<100000-6101><100000-6104><100000-6155><100000-6107>

Counter.count=100000
耗时:6150

<100000-6287><100000-6182><100000-6159><100000-6104>

500000 Counter.count=497242
耗时:28892

<497394-30674><497274-29864><497129-29470><497216-29529>

Counter.count=500000
耗时:31232

<500000-29297><500000-29053><500000-29166><500000-29006>

Counter.count=500000
耗时:29251

<500000-29743><500000-29334><500000-29600><500000-29848>

将上面耗时时间求平均值,得到:

线程数量 未同步平均用时 synchronized (obj)平均用时 synchronized (Counter.class)平均用时
100 507.2 507.4 507.4
1000 563.6 562.6 567.6
10000 1074 1074 1084.2
100000 6128.2 6114 6176.4
500000 29685.8 29550.8 29555.2

于是,得出结论:
1、synchronized可以解决多线程造成的count++不准确的问题。

2、随着线程数量的增加,加过synchronized后的代码,并不会对速度产生很明显的影响。

3、从1万与10万的结果来看,经过synchronized(obj)处理后的程序,要比synchronized(Counter.class)处理后的程序大约要快10ms。而100、1000与50万,synchronized(obj)与synchronized(Counter.class)执行速度几乎相等。从总体来看,随着线程数量的增加,synchronized(Counter.class)执行的效率并没有明显的低于synchronized(obj)。

最新文章

  1. ViewPager,实现真正的无限循环(定时+手动)
  2. 从头学Qt Quick(1) --体验快速构建动态效果界面
  3. android 学习随笔二十四(动画:帧动画)
  4. 图解Windows Server 2012 桌面图标
  5. IIS由于无法创建应用程序域,因此未能执行请求。错误: 0x80070005 拒绝访问
  6. 2-SAT 及 一点习题
  7. Android上解析Json格式数据
  8. 【SqlServer系列】AS的用法
  9. Java为什么要配置环境变量及如何配置环境变量
  10. 设计模式之——工厂模式(B)
  11. 值得注意的CSS属性
  12. LEDAPS1.3.0版本移植到windows平台----HuSr大气校正模块
  13. 02.Numpy
  14. 快速排序 之添加复合插入排序和原始序列取中值左pivot
  15. JMeter接口压测——ServerAgent监控服务端性能指标
  16. {MySQL数据库初识}一 数据库概述 二 MySQL介绍 三 MySQL的下载安装、简单应用及目录介绍 四 root用户密码设置及忘记密码的解决方案 五 修改字符集编码 六 初识sql语句
  17. 联想拯救者win10+ubuntu14.04
  18. python seek()方法报错:“io.UnsupportedOperation: can&#39;t do nonzero cur-relative seeks”
  19. Python读取图片尺寸、图片格式
  20. 关于go v1.11安装后出现不能正常运行测试程序的问题

热门文章

  1. C/C++基础----函数
  2. bzoj2656 数列
  3. tcp端口检测
  4. monkey亲测
  5. oracle11g的dmp文件导入oracle10g时报错:头部验证失败
  6. Redis安装测试
  7. 并发工具类(四)线程间的交换数据 Exchanger
  8. 好久没玩laravel了,5.6玩下(三)
  9. 使用 IIS 过程中遇到的一些问题
  10. sql server自增列值的获取