同步锁与同步函数锁的使用方法
来源:互联网 发布:足球新闻数据app 编辑:程序博客网 时间:2024/05/10 03:11
class Ticket implements Runnable
{
private int num=300;
//Object obj = new Object();
boolean flag=true;
public void run()
{
if(flag)
while(true)
{
synchronized(this)
{
if(num>0)
{
try{Thread.sleep(10);}
catch (InterruptedException e){}
System.out.println("...obj..."+num--);
}
}
}
else
while(true)
{
show();
}
}
public synchronized void show()
{
if(num>0)
{
try{Thread.sleep(10);}
catch (InterruptedException e){}
System.out.println("...function..."+num--);
}
}
}
class ThreadDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");
Ticket t=new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
try{Thread.sleep(20);}
catch (InterruptedException e){}
t.flag=false;
t2.start();
}
}
{
private int num=300;
//Object obj = new Object();
boolean flag=true;
public void run()
{
if(flag)
while(true)
{
synchronized(this)
{
if(num>0)
{
try{Thread.sleep(10);}
catch (InterruptedException e){}
System.out.println("...obj..."+num--);
}
}
}
else
while(true)
{
show();
}
}
public synchronized void show()
{
if(num>0)
{
try{Thread.sleep(10);}
catch (InterruptedException e){}
System.out.println("...function..."+num--);
}
}
}
class ThreadDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");
Ticket t=new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
try{Thread.sleep(20);}
catch (InterruptedException e){}
t.flag=false;
t2.start();
}
}
0 0
- 同步锁与同步函数锁的使用方法
- 验证同步代码块与同步函数的锁【多线程】
- 验证同步代码块与同步函数的锁【多线程】
- java 多线程 同步函数 。同步函数的锁 和 静态同步函数的锁
- 线程的同步与锁
- 线程的同步与锁
- 线程的同步与锁
- 同步代码块,同步函数,静态同步函数的锁对象
- dispatch同步的使用方法
- 同步函数锁
- Java线程的同步与锁-java同步和锁定
- 线程,同步与锁
- 同步与锁
- 线程同步与锁
- 多线程同步与锁
- 线程同步与锁
- Java 同步与锁
- 同步与锁
- TestNG中的注解说明
- 科大讯飞语音合成,pcm转wav
- mybatis获得新增记录的id值
- 线段树模板
- 简单自绘
- 同步锁与同步函数锁的使用方法
- DAY12_history of cryptography
- 51nod oj 1265 四点共面 【数学几何】
- android launcher开发笔记
- 利用itext操作pdf从数据库导出大量数据
- 思维导图
- linux下 动态链接库的制作
- Python实现Event回调机制
- Centos6.4 升级 centos7.2