synchronized关键字
来源:互联网 发布:软件无线电原理与技术 编辑:程序博客网 时间:2024/05/22 16:05
//线程同步——synchronized关键字
package button2;
public class getmain implements Runnable{
int num=10;
public void run() {
while(true) {
synchronized("") {//解决资源共享问题,同步块,一个时段只允许一个访问
if(num>0) {
try {
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("tickets"+--num);
}
}
}
}
public static void main(String []args) {
getmain t=new getmain();
Thread ta=new Thread(t);
Thread tb=new Thread(t);
Thread tc=new Thread(t);
Thread td=new Thread(t);
ta.start();
tb.start();
tc.start();
td.start();
}
}除此之外,还可以将每一个方法设为sychronized
阅读全文
0 0
- synchronized 关键字
- synchronized关键字
- synchronized关键字
- synchronized关键字
- 关键字synchronized
- synchronized关键字
- synchronized关键字
- synchronized关键字
- synchronized关键字
- synchronized 关键字
- synchronized关键字
- synchronized关键字
- synchronized关键字
- Synchronized关键字
- Synchronized关键字
- synchronized关键字
- synchronized 关键字
- synchronized 关键字
- 2017 JUST Programming Contest 3.0 H. Eyad and Math
- 51Nod-1837-砝码称重
- 用construct 2制作一个简单的飞机游戏
- Eclipse 单步调试
- angular4.0的模板式表单、响应式表单及其错误提示
- synchronized关键字
- 常见网络攻击及处理办法
- 奔小康赚大钱 HDU
- Java字符流、字节流的超类及子类的API记录和结构图备忘
- 2017 JUST Programming Contest 3.0 D. Dice Game
- 一行代码让你的TableView动起来-iOS动画
- DTD与XSD区别
- Android 自定义 View
- Docker下,实战mongodb副本集(Replication)