The modification of static function of Java multithreading security problem

Source: Internet
Author: User
Tags ticket

/* If the synchronization function is statically decorated, what is the lock used? Through validation, the discovery is not in this. This cannot be defined in a static method. static into memory is that there is no such object in memory, but there must be a corresponding bytecode file object for that class. Class name. Class The type of the object is a class-static synchronous method that uses a lock that is the bytecode file object of the class in which the method resides. class name. */classTicketImplementsrunnable{Private Static  intTick = 100; //Object obj = new Object ();    BooleanFlag =true;  Public  voidrun () {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 voidShow () {if(tick>0)        {            Try{Thread.Sleep (10);}Catch(Exception e) {} System.out.println (Thread.CurrentThread (). GetName ()+ ".... Show ....:" + tick--); }    }}classstaticmethoddemo{ Public Static voidMain (string[] args) {Ticket T=NewTicket (); Thread T1=NewThread (t); Thread T2=NewThread (t);        T1.start (); Try{Thread.Sleep (10);}Catch(Exception e) {} T.flag=false;    T2.start (); }}

Modification of the Java multithreading security problem static function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.