/* 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