线程基础
来源:互联网 发布:网络数据分析 编辑:程序博客网 时间:2024/06/11 20:26
java开发经常会问到是不是线程安全的?什么叫线程安全呢,就是多个线程访问这个类的时候,这个类仍然能返回正确的结果.
请看下面的demo
public class MyThread extends Thread{
private int count =5;
@Override
public synchronized void run() {//这里加上关键词synchronize,才能保证这个方法是上锁的,这样就能保证一个线程访问的时候,其他线程不能访问.
count--;
System.out.println(this.currentThread().getName()+" count="+count);
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread t1 = new Thread(myThread,"t1");
Thread t2 = new Thread(myThread,"t2");
Thread t3 = new Thread(myThread,"t2");
Thread t4 = new Thread(myThread,"t2");
Thread t5 = new Thread(myThread,"t2");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
阅读全文
0 0
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 线程基础
- 从零开始学--Java搭建 Spring MVC 框架
- easyUI textbox填坑
- PHP设计模式——简单工厂
- $http请求之options
- Windows无法安装到这个磁盘,选中的磁盘采用GPT分区形式
- 线程基础
- MFC添加窗体菜单
- 对表的一些操作
- (poj-1163)教你彻底学会动态规划——入门篇
- 主题通配符
- [游戏编程模式].(Game.Programming.Patterns).(美)Robert.Nystrom.pdf
- 数据库水平切分的实现原理解析——分库,分表,主从,集群,负载均衡器(转)
- 归并排序
- CodeIgniter框架源码学习之配置类--Config.php