- class Ticket implements Runnable{
- private static int num = 100;
- boolean flag =true;
- public void run(){
- if(flag){
- while(true){
- synchronized(Ticket.class){
- if(num>100)
- try{Thread.sleep(10);}
- catch (InterruptedException e){e.printStackTrace();}
- System.out.println(Thread.currentThead().getName()+"..."+num--);
- }}}
- else
- while(true){
- show();
- }
-
- public static synchronized void show(){
- if(num>0){
- try {Thread.sleep(10);}
- catch (InterruptedException e){e.printStackTrace();}
- System.out.println(Thread.currentThread().getName()+"...function..."+num--);}
-
- }}
- class Srun{
- public static void main(String[] args){
- Ticket t =new Ticket();
- Thread t1= new Thread(t);
- Thread t2= new Thread(t);
- t1.start();
- try {Thread.sleep(10);}
- catch (InterruptedException e){e.printStackTrace();}
- t.flag = flase;
- t2.start();
- }}
复制代码 |
-
捕获.PNG
(8.96 KB, 下载次数: 0)
什么情况
|