zoukankan      html  css  js  c++  java
  • 同步方法解决同步问题

    package tongbufangfa.cn;
    /*
     * 同步方法 解决同步问题
     * 同步方法:使用synchronized 关键字将一个方法申明为 同步方法
     * 格式为:
     * synchronized 方法返回值   方法名称 (参数列表){
     * }
     */
    //一个类实现runnable 
    class TongBu implements Runnable{
        private int tiket = 5;
        public void run(){
            for (int i = 0; i < 100; i++) {    
                //调用同步方法
                this.sale();
            }
        }
                //使用同步方法
              public synchronized void sale(){
                    //判断票数是否大于0 ,大于0的话就卖票
                    if (tiket>0) {
                    //设置线程之间延迟
                    try{Thread.sleep(50);}
                    catch(Exception e){}
                    System.out.println("tiket = "+ tiket--);
                }
            }    
    }
    public class TongBuFangFa {
        public static void main(String[] args) {
            TongBu tb = new TongBu();
            Thread tbd1 = new Thread(tb);
            Thread tbd2 = new Thread(tb);
            Thread tbd3 = new Thread(tb);
            tbd1.start();
            tbd2.start();
            tbd3.start();
        }
        
    
    }
  • 相关阅读:
    命令行选项
    损坏的RAID5
    Codeforces Round #600 (Div. 2)
    python 数据分析
    xor or and 线段树
    CCPC哈尔滨E题
    二维偏序
    Codeforces Round #592 (Div. 2)
    Codeforces Round #597 (Div. 2)
    pycharm 安装激活
  • 原文地址:https://www.cnblogs.com/yuanyuan2017/p/6944899.html
Copyright © 2011-2022 走看看