zoukankan      html  css  js  c++  java
  • java多线程安全问题 静态函数的修饰

    /*
    如果同步函数被静态修饰后,使用的锁是什么呢?
    
    通过验证,发现不在是this。因为静态方法中也不可以定义this。
    
    静态进内存是,内存中没有本类对象,但是一定有该类对应的字节码文件对象。
    类名.class  该对象的类型是Class
    
    
    静态的同步方法,使用的锁是该方法所在类的字节码文件对象。 类名.class
    */
    class Ticket implements Runnable
    {
        private static  int tick = 100;
        //Object obj = new Object();
        boolean flag = true;
        public  void run()
        {
            if(flag)
            {
                while(true)
                {
                    synchronized(Ticket.class)
                    {
                        if(tick>0)
                        {
                            try{Thread.sleep(10);}catch(Exception e){}
                            System.out.println(Thread.currentThread().getName()+"....code : "+ tick--);
                        }
                    }
                }
            }
            else
                while(true)
                    show();
        }
        public static synchronized void show()
        {
            if(tick>0)
            {
                try{Thread.sleep(10);}catch(Exception e){}
                System.out.println(Thread.currentThread().getName()+"....show.... : "+ tick--);
            }
        }
    }
    
    
    class  StaticMethodDemo
    {
        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(Exception e){}
            t.flag = false;
            t2.start();
    
    
        }
    }
  • 相关阅读:
    C++中 结构体和类的异同
    c++sizeof大全
    10th week task -3 Arrow function restore
    10th week task -2 Object 的起源
    js 的起源故事
    10th week task -1
    9th week -the history of program 1950~2020
    javascript统计一个字符在一段字符串出现次数
    Grid layout
    BOM DOM区别 来源
  • 原文地址:https://www.cnblogs.com/shuqingstudy/p/4947739.html
Copyright © 2011-2022 走看看