Wu Yuxiong - born naturally develop common java class library Study Notes: sync with Deadlock

class the MyThread the implements the Runnable {
     Private  int Ticket = 5;     // assumed that a total of five tickets 
    public  void RUN () {
         for ( int I = 0; I <100; I ++ ) {
             IF (Ticket> 0) {     // there counting 
                the try { 
                    the Thread.sleep ( 300);     // added delay 
                } the catch (InterruptedException E) { 
                    e.printStackTrace (); 
                } 
                System.out.println ( "ticket: ticket =" + ticket-- ); 
            }
        } 
    } 
}; 
Public  class SyncDemo01 {
     public  static  void main (String args []) { 
        the MyThread MT = new new the MyThread ();     // define the thread object 
        Thread = T1 new new Thread (MT);     // defines the Thread object 
        Thread T2 = new new Thread (MT);     // defines the Thread object 
        Thread T3 = new new Thread (MT);     // defines the Thread object 
        t1.start (); 
        t2.start (); 
        t3.start ();  
    }
};
class the MyThread the implements the Runnable {
     Private  int Ticket = 5;     // assumed that a total of five tickets 
    public  void RUN () {
         for ( int I = 0; I <100; I ++ ) {
             the synchronized ( the this ) {     // To the current object synchronization 
                IF (ticket> 0) {     // there votes 
                    the try { 
                        the Thread.sleep ( 300);     // added delay 
                    } the catch (InterruptedException E) { 
                        e.printStackTrace ();
                    } 
                    System.out.println ( "ticket: Ticket =" + ticket-- ); 
                } 
            } 
        } 
    } 
}; 
public  class SyncDemo02 {
     public  static  void main (String args []) { 
        the MyThread MT = new new the MyThread ();     / / definitions thread object 
        thread = T1 new new thread (MT);     // defines the thread object 
        thread T2 = new new thread (MT);     // defines the thread object 
        thread T3 = new newThread (MT);     // defines the Thread object 
        t1.start (); 
        t2.start (); 
        t3.start (); 
    } 
};
class the MyThread the implements the Runnable {
     Private  int Ticket = 5;     // assumed that a total of five tickets 
    public  void RUN () {
         for ( int I = 0; I <100; I ++ ) {
             the this .sale ();     // call the synchronization method 
        } 
    } 
    public  the synchronized  void Sale () {     // declare synchronization method of 
        IF (ticket> 0) {     // there votes 
            the try { 
                the Thread.sleep ( 300);     // added delay 
            } the catch(InterruptedException E) { 
                e.printStackTrace (); 
            } 
            System.out.println ( "ticket: Ticket =" + ticket-- ); 
        } 

    } 
}; 
public  class SyncDemo03 {
     public  static  void main (String args []) { 
        MT the MyThread = new new the MyThread ();     // define the thread object 
        thread = T1 new new thread (MT);     // defines the thread object 
        thread T2 = new new thread (MT);     // defines the thread object 
        thread T3 = new newThread (MT);     // defines the Thread object 
        t1.start (); 
        t2.start (); 
        t3.start (); 
    } 
};
class Zhangsan {     // define three types of Zhang 
    public  void say () { 
        System.out.println ( "Joe Smith John Doe said:" You draw me, I'll give you a book "." ); 
    } 
    public  void GET () { 
        System.out.println ( "Joe Smith to get painted." ); 
    } 
}; 
class Lisi {     // define four categories of Lee 
    public  void say () { 
        System.out.println ( "John Doe told Joe Smith : "you give me the book, I'll draw to you" " ;) 
    } 
    public  void gET () { 
        System.out.println ( ." John Doe get the book " ); 
    } 
}; 
public class ThreadDeadLock the implements the Runnable {
     Private  static Zhangsan ZS = new new Zhangsan ();         // instantiate an object of type static 
    Private  static Lisi LS = new new Lisi ();         // instantiate an object of type static 
    Private  Boolean In Flag = to false ;     // declaration flag , it is determined that the first speaker 
    public  void run () {     // override run () method 
        IF (In Flag) {
             the synchronized (ZS) {     // synchronization Zhang 
                zs.say ();
                 the try {
                    Thread.sleep(500) ;
                }catch(InterruptedException e){
                    e.printStackTrace() ;
                }
                synchronized(ls){
                    zs.get() ;
                }
            }
        }else{
            synchronized(ls){
                ls.say() ;
                try{
                    Thread.sleep(500) ;
                }catch(InterruptedException e){
                    e.printStackTrace() ;
                }
                synchronized(zs){
                    ls.get() ;
                }
            }
        }
    }
    public static void main(String args[]){
        ThreadDeadLock t1 = new ThreadDeadLock() ;        // 控制张三
        ThreadDeadLock t2 = new ThreadDeadLock() ;        // 控制李四
        t1.flag = true ;
        t2.flag = false ;
        Thread thA = new Thread(t1) ;
        Thread thB = new Thread(t2) ;
        thA.start() ;
        thB.start() ;
    }
};

 

Guess you like

Origin www.cnblogs.com/tszr/p/12152846.html