zoukankan      html  css  js  c++  java
  • 线程安全方式02Runnable---同步方法

    /**
    * Runnable方式线程安全---同步方法
    *
    * @Author: ITYW
    * @Create 2020 - 10 - 26 - 18:59
    */
    public class RunnableSecurity02 {
    public static void main(String[] args) {
    Runnable_Security02 runnable_security02 = new Runnable_Security02();
    Thread t1 = new Thread(runnable_security02);
    Thread t2 = new Thread(runnable_security02);
    Thread t3 = new Thread(runnable_security02);

    t1.setName("窗口1");
    t2.setName("窗口2");
    t3.setName("窗口3");

    t1.start();
    t2.start();
    t3.start();
    }
    }

    class Runnable_Security02 implements Runnable{
    private static int ticket = 100;
    @Override
    public void run() {
    while (true){
    sale();
    }

    }

    private static synchronized void sale(){
    if (ticket > 0){
    System.out.println(Thread.currentThread().getName()+"卖出了第"+ticket+"张");
    ticket--;
    }
    }
    }
  • 相关阅读:
    452.用最少数量的箭引爆气球
    134.加油站
    Javascript
    spring-JDBC模板
    AOP注解方式ApsectJ开发
    AOP通知类型
    AOP的使用
    AOP相关术语
    AOP
    IOC注解详解
  • 原文地址:https://www.cnblogs.com/ITYW/p/13880505.html
Copyright © 2011-2022 走看看