线程--同步函数锁是this
来源:互联网 发布:中日韩围棋 知乎 编辑:程序博客网 时间:2024/06/14 22:01
package java09_06;
public class ThreadTest {
public static char[] currenThrad;public static void main(String[] args) throws InterruptedException { Ticket test = new Ticket(); Thread t1 = new Thread(test,"张三"); Thread t2 = new Thread(test,"李四"); t1.start(); Thread.sleep(10); //让主线程把执行资格和执行权让出来 , test.flag = false; t2.start();}
}
class Ticket implements Runnable{
private int ticket = 100;
boolean flag = true;
Object Object = new Object();
@Override
public void run() {
// TODO Auto-generated method stub
if(flag){
while (true) {
// synchronized (Object) { //这个锁是Object
synchronized (this) { //把这个锁也变成this
if(ticket>0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+”–run—剩余的票数是:”+(–ticket));
}
}
}
}else
while (true)
Sale();//同步函数
}/** * 这个锁是this */public synchronized void Sale() { if(ticket>0){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"--Sale--剩余的票数是:"+(--ticket)); }}
}
- 线程--同步函数锁是this
- java-同步函数的锁是this
- JAVA之旅(十三)——线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this
- 多线程--同步函数使用的锁是this
- java多线程-同步函数的锁是this
- 验证多线程中同步函数锁是this对象
- 多线程(多线程-同步函数的锁是this)
- 多线程之同步函数使用的是this锁
- 实现Runnable接口创建线程,并验证同步函数this锁--存疑
- java多线程详解四 验证同步函数的锁是this
- JAVA 同步函数以this为锁
- 同步函数以this为锁
- 同步函数以this为同步对象
- 同步函数的锁是哪个锁
- 线程锁synchronized (this)锁住的是对象还是方法
- 线程同步函数
- 黑马程序员-----同步函数锁,this锁(黑马视频)
- (48)同步函数用的this锁
- 对象的查询和设置(JS高级)
- extern作用详解
- 排序-1-冒泡排序
- HDU 1711 Number Sequence
- 【教程】一、eclipse集成Tomcat服务器并部署项目
- 线程--同步函数锁是this
- Hibernate第三天
- Android APP通过蓝牙耳机录音可行性分析
- TOMCAT源码分析-生命周期管理2
- Gimp 快速入门(3)
- Java中为什么静态方法不能访问非静态方法
- java策略设计模式
- 【Android面试】关于post你真的了解吗?
- 解决父级边框塌陷的方法