同步锁synchronized 关键字的使用效果

package com.sf.LianCheng.demo;

public class Runnable_demo implements Runnable{  
    private int ticket=10;  
    public Runnable_demo(){       
    }  
    @Override  
    public void run() {  
        for(int i=0;i<20;i++){  
                if(this.ticket>0){  
                    //休眠1s秒中,为了使效果更明显,否则可能出不了效果  
                    try {  
                        Thread.sleep(1000);  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                    this.sale();
                }  

        }  
    }  
    public  void sale(){  
        if(this.ticket>0){  
            System.out.println(Thread.currentThread().getName()+"号窗口卖出:"+this.ticket--+"号票");  
        }  
    }  

     public static void main(String args[]){  
         Runnable_demo demo=new Runnable_demo();  
         //基于火车票创建三个窗口  
         new Thread(demo,"a").start();  
         new Thread(demo,"b").start();  
         new Thread(demo,"c").start();  
     } 
}

同步锁synchronized 关键字的使用效果_第1张图片


使用同步锁关键字synchronized 后

package com.sf.LianCheng.demo;

public class Runnable_demo1 implements Runnable {
    private int ticket = 10;

    public Runnable_demo1() {
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if (this.ticket > 0) {
                // 休眠1s秒中,为了使效果更明显,否则可能出不了效果
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                this.sale();
            }

        }
    }

    public synchronized void sale() {

        System.out.println(Thread.currentThread().getName() + "号窗口卖出:" + this.ticket-- + "号票");

    }

    public static void main(String args[]) {
        Runnable_demo1 demo = new Runnable_demo1();
        // 基于火车票创建三个窗口
        new Thread(demo, "a").start();
        new Thread(demo, "b").start();
        new Thread(demo, "c").start();
    }

}

同步锁synchronized 关键字的使用效果_第2张图片

你可能感兴趣的:(java,学习)