zoukankan      html  css  js  c++  java
  • java synchronized

    package com.test;
    
    import lombok.SneakyThrows;
    import lombok.Synchronized;
    
    public class Ticket implements  Runnable {
        private volatile int ticketNums=100;
        @SneakyThrows
        @Override
        public void run() {
            while(true) {
                sale();
    //            synchronized (Ticket.class) {//第一种代码块
    //                if (ticketNums > 0) {
    //                    Thread.sleep(10);
    //                    System.out.println(Thread.currentThread().getName() + " sale " + ticketNums + " ticket");
    //                    ticketNums--;
    //                }
    //            }
            }
        }
        public synchronized void sale() throws InterruptedException {//第二种方法块
    if (ticketNums > 0) { Thread.sleep(10); System.out.println(Thread.currentThread().getName() + " sale " + ticketNums + " ticket"); ticketNums--; } } }

      

  • 相关阅读:
    一. js高级(1)-面向对象编程
    tips01- 定位
    h5c3 part6 flex
    h5c3 part5 background and transform
    template and pagination
    h5c3 part4
    h5c3 part3
    h5c3 part2
    h5c3 part1
    学习博客
  • 原文地址:https://www.cnblogs.com/howhy/p/15205257.html
Copyright © 2011-2022 走看看