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