多个线程多把锁
来源:互联网 发布:电信4g网络覆盖范围 编辑:程序博客网 时间:2024/05/22 09:48
public class MulityThread{
private static int num = 0;
public static synchronized void printNum(String tag){//如果加static关键字,那么这个方法就是公使用的,不管实例多少对象,它都只有一把锁
try{
if(tag.equals("a")){
num = 100;
System.out.println("tag a,set num over!");
Thread.sleep(1000);
}else{
num = 200;
System.out.println("tag b,set num over!");
}
System.out.println("tag "+tag+",num = "+num);
}catch(InterruptedException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
final MulityThread m1 = new MulityThread();
final MulityThread m2 = new MulityThread();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
m1.printNum("a");
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
m2.printNum("b");
}
});
t1.start();
t2.start();
}
private static int num = 0;
public static synchronized void printNum(String tag){//如果加static关键字,那么这个方法就是公使用的,不管实例多少对象,它都只有一把锁
try{
if(tag.equals("a")){
num = 100;
System.out.println("tag a,set num over!");
Thread.sleep(1000);
}else{
num = 200;
System.out.println("tag b,set num over!");
}
System.out.println("tag "+tag+",num = "+num);
}catch(InterruptedException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
final MulityThread m1 = new MulityThread();
final MulityThread m2 = new MulityThread();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
m1.printNum("a");
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
m2.printNum("b");
}
});
t1.start();
t2.start();
}
阅读全文
0 0
- 多个线程多把锁
- 架构师之路--线程基础--多个线程多把锁
- 多线程基础学习第二篇(多个线程多把锁)
- 线程-多个线程多个锁
- 多个线程多个锁
- lua 多个线程
- 多个线程调用同一个线程函数
- 把多个menu绑定到treeview
- 把多个pdf合并的操作方法
- 线程传入多个参数
- Mysql kill多个线程
- 多个并行线程运算
- 1.2 多个线程多个锁
- 用一个线程模拟多个线程,减少线程数量~
- 用一个线程模拟多个线程,减少线程数量~
- Lecture 5:把无限多个假设降到有限个
- 线程2:线程通信事件机制(单线程、两个线程、多个线程通信)
- 线程基础学习第一篇(多个线程竞争一把锁)
- Android实现收款成功金额的语音播报功能(Nice tone)
- pyaudio 编译相关问题
- iOS 蓝牙低功耗使用
- LOJ模板题 无源汇有上下界可行流
- [LeetCode] 150. Evaluate Reverse Polish Notation
- 多个线程多把锁
- Flask学习笔记--11
- 终止Java线程
- 调用dialog.show时报异常
- arcgis javascript 鹰眼图
- iOS lable设置富文本后在最后一行没有显示省略号的问题解决
- 资讯精选 | 流量攻击态势严峻_微软7月漏洞汇总和修复建议
- VS 调用动态链接库 PInvokeStackImbalance 对 PInvoke 函数“ ”的调用导致堆栈不对称
- Spring源码-将源码导入到eclipse