public class MyRun implements Runnable {

        int count = 1000;
@Override
public void run() {
while (true) {
if (count > 0) {
Log.e("", Thread.currentThread().getName() + "|" + "running");
synchronized (this) {
count--;
Log.e("", Thread.currentThread().getName() + "|" + count);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
break;
}
}
Log.e("", Thread.currentThread().getName() + "|" + "end");
}
}
    MyRun myRun = new MyRun();

    new Thread(myRun, "aaa").start();
new Thread(myRun, "bbb").start();
new Thread(myRun, "ccc").start();

总结:

多线程访问同一个全局变量时,要使用synchronized来同步,否则,线程间全局变量的值会有差异

最新文章

  1. Mac OSX:Powerline风格的zsh配置
  2. 前端学PHP之面向对象系列第二篇——魔术方法
  3. Windows Azure Traffic Manager (6) 使用Traffic Manager,实现本地应用+云端应用的高可用
  4. 根据字符串生成类---类的类型.self---根据字符串创建控制器对象
  5. webpack 教程 那些事儿04-webpack项目实战分析
  6. [安卓]Android窗口、视图、布局
  7. 你会用Python做出装逼的东西吗
  8. Oracle行列转换
  9. devexpress中如何绑定ASPxTreeList控件
  10. Alignment ( 最长上升(下降)子序列 )
  11. XSS初体验
  12. Windows 端口和任务 查看 相关命令
  13. Django介绍
  14. nginx 配置本地https(免费证书)
  15. 定制FileField中的上传文件名称
  16. PowerBuilder编程新思维2:嵌入(Thread多线程)
  17. tomcat 使用 cronolog 切割日志
  18. IOS6新特性之下拉刷新<UIRefreshControl>
  19. Python3 笔记01:求两数之和
  20. css雪碧技术的用法。

热门文章

  1. spring AOP 实现事务和主从读写分离
  2. 给table行换色
  3. c++作用域运算符---7
  4. Yahoo Pure 中文参考手册
  5. [Codeforces Round #275 (Div. 2)]B - Friends and Presents
  6. Boot loader: Grub进阶[转]
  7. Keepalived 双机热备
  8. js兼容性记录
  9. 调Windows 7的图片浏览器查看图片
  10. Property ClientHeight does not exist 问题解决