java 에서 synchronized 에 대한 일반적인 용법

40372 단어 synchronized
package j2se.thread.test;



/***

 * synchronized(class)   ,               class  monitor     .

 * class this              ,          class,           class     .

                   ,           :

    synchronized(class)

    synchronized(this)

    ->      monitor,     .

    synchronized(this)

    synchronized(this)

    ->               ,    ,       ,    .

    synchronized(class)

    synchronized(class)

    ->                      ,    .

 */





/**

 * @author liwei

      synchronized(this) synchronized(class)

 */

public class TestSynchronied8 {

     private  static byte[] lockStatic = new byte[0]; //    instance  

     private byte[] lock = new byte[0]; //    instance  

     

     public synchronized void m4t5() {  

         System.out.println(this);

          int i = 50;  

          while( i-- > 0) {  

               System.out.println(Thread.currentThread().getName() + " : " + i);  

               try {  

                    Thread.sleep(100);  

               } catch (InterruptedException ie) {  

               }  

               Thread.yield();

          }  

    } 

     

     public void m4t0() {  

         synchronized(this) {  

             System.out.println(this);

              int i = 50;  

              while( i-- > 0) {  

                   System.out.println(Thread.currentThread().getName() + " : " + i);  

                   try {  

                        Thread.sleep(100);  

                   } catch (InterruptedException ie) {  

                   }  

                   Thread.yield();

              }  

         }  

    } 

     

     

     public void m4t1() {  

         synchronized(lock) {  

             System.out.println(this);

              int i = 50;  

              while( i-- > 0) {  

                   System.out.println(Thread.currentThread().getName() + " : " + i);  

                   try {  

                        Thread.sleep(100);  

                   } catch (InterruptedException ie) {  

                   } 

                   Thread.yield();

              }  

         }  

    }  

     /**

     * synchronized(class)

        synchronized(class)

        ->                      ,    .

     */

    public static synchronized void m4t2() {  

          int i = 50;  

          while( i-- > 0) {  

               System.out.println(Thread.currentThread().getName() + " : " + i);  

               try {  

                    Thread.sleep(100);  

               } catch (InterruptedException ie) {  

               }  

               Thread.yield();

              

          }  

    } 

    

    public static  void m4t3() { 

        synchronized(TestSynchronied8.class){

            int i = 50;  

            while( i-- > 0) {  

                 System.out.println(Thread.currentThread().getName() + " : " + i);  

                 try {  

                      Thread.sleep(100);  

                 } catch (InterruptedException ie) {  

                 }  

                 Thread.yield();

            }  

        }

  } 

    

    public static  void m4t4() { 

        synchronized(lockStatic){

            int i = 50;  

            while( i-- > 0) {  

                 System.out.println(Thread.currentThread().getName() + " : " + i);  

                 try {  

                      Thread.sleep(100);  

                 } catch (InterruptedException ie) {  

                 }

                 Thread.yield();

            }  

        }

  } 



    

    

 /** 

    synchronized(this)

    synchronized(this)

    ->             ,    .

 */

public static void testObjsyn1(){

    final TestSynchronied8 myt2 = new TestSynchronied8();

    final TestSynchronied8 myt1 = new TestSynchronied8();   

    try {

        System.out.println("          ,     synchronized(this)   -------------------------------------");

        Thread.sleep(500);

    } catch (InterruptedException e) {

        e.printStackTrace();

    }

    Thread t1 = new Thread(  new Runnable() {  public void run() {  myt1.m4t0();  }  }, "t1"  );   

    Thread t2 = new Thread(  new Runnable() {  public void run() {  myt2.m4t0();  }  }, "t2"  );        

    t1.start();   

    t2.start();  

} 



/** 

synchronized(this)

synchronized(this)

->             ,   .

*/

public static void testObjsyn2(){

    final TestSynchronied8 myt1 = new TestSynchronied8();    

    try {

        System.out.println("       ,     synchronized(this)   -------------------------------------");

        Thread.sleep(500);

    } catch (InterruptedException e) {

        e.printStackTrace();

    }

    Thread t3 = new Thread(  new Runnable() {  public void run() {  myt1.m4t0();  }  }, "t3"  );

    Thread t4 = new Thread(  new Runnable() {  public void run() {  myt1.m4t0();  }  }, "t4"  );

    t3.start();   

    t4.start();   

} 



/** 

synchronized(this)

synchronized(this)

->             ,   .

*/

public static void testObjsyn3(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        System.out.println("       ,     synchronized(obj)   -------------------------------------");

        Thread.sleep(500);

    } catch (InterruptedException e) {

        e.printStackTrace();

    }

    Thread t5 = new Thread(  new Runnable() {  public void run() {  myt1.m4t1();  }  }, "t5"  );

    Thread t6 = new Thread(  new Runnable() {  public void run() {  myt1.m4t1();  }  }, "t6"  );

    t5.start();   

    t6.start();     

} 



/** 

synchronized(this)

synchronized(this)

->             ,   .

*/

public static void testObjsyn4(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        System.out.println("       ,     synchronized  -------------------------------------");

        Thread.sleep(500);

    } catch (InterruptedException e) {

        e.printStackTrace();

    }

    Thread t7 = new Thread(  new Runnable() {  public void run() {  myt1.m4t5();  }  }, "t7"  );

    Thread t8 = new Thread(  new Runnable() {  public void run() {  myt1.m4t5();  }  }, "t8"  );

    t7.start();   

    t8.start(); 

} 



/** 

synchronized(this)

synchronized(this)

->             ,    .

*/

public static void testObjsyn5(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    final TestSynchronied8 myt2 = new TestSynchronied8();

    try {

        System.out.println("         ,     synchronized  -------------------------------------");

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

     Thread t9 = new Thread(  new Runnable() {  public void run() {  myt1.m4t5();  }  }, "t9"  );

     Thread t10 = new Thread(  new Runnable() {  public void run() {  myt2.m4t5();  }  }, "t10"  );

     t9.start();   

     t10.start(); 

} 



/** 

synchronized(this)

synchronized  

->            ,   syschronized(this),       ,   .

*/

public static void testObjsyn6(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

     Thread t9 = new Thread(  new Runnable() {  public void run() {  myt1.m4t0();  }  }, "t9"  );

     Thread t10 = new Thread(  new Runnable() {  public void run() {  myt1.m4t5();  }  }, "t10"  );

     t9.start();   

     t10.start(); 

}



/** 

synchronized(lock)

synchronized  

->            ,   syschronized(lock),       ,    .

*/

public static void testObjsyn7(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

     Thread t9 = new Thread(  new Runnable() {  public void run() {  myt1.m4t1();  }  }, "t9"  );

     Thread t10 = new Thread(  new Runnable() {  public void run() {  myt1.m4t5();  }  }, "t10"  );

     t9.start();   

     t10.start(); 

}



/** 

synchronized(lock)

synchronized  

->            ,   syschronized(lock),   syschronized(this),    .

*/

public static void testObjsyn71(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

     Thread t9 = new Thread(  new Runnable() {  public void run() {  myt1.m4t0();  }  }, "t9"  );

     Thread t10 = new Thread(  new Runnable() {  public void run() {  myt1.m4t1();  }  }, "t10"  );

     t9.start();   

     t10.start(); 

}



/**

     * synchronized(class)

        synchronized(class)

        ->               ,   .

 */

public static void testObjsyn8(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    final TestSynchronied8 myt2 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

   Thread t2 = new Thread(  new Runnable() {  public void run() { myt2.m4t2();   }  }, "t2"  ); 

   Thread t4 = new Thread(  new Runnable() {  public void run() { myt1.m4t2();   }  }, "t4"  ); 

   t2.start(); 

   t4.start(); 

}



/**

 * synchronized(class)

    synchronized(class)

    ->               ,   .

*/

public static void testObjsyn9(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

    Thread t2 = new Thread(  new Runnable() {  public void run() { myt1.m4t2();   }  }, "t2"  ); 

    Thread t4 = new Thread(  new Runnable() {  public void run() { myt1.m4t2();   }  }, "t4"  ); 

    t2.start(); 

    t4.start(); 

}



/**

 * synchronized(class)

    synchronized(this)

    ->      monitor,     .

 */

public static void testObjsyn10(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    final TestSynchronied8 myt2 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

    Thread t7 = new Thread(  new Runnable() {  public void run() { myt2.m4t0();   }  }, "t7"  ); 

    Thread t8 = new Thread(  new Runnable() {  public void run() { myt1.m4t3();   }  }, "t8"  ); 

    t7.start(); 

    t8.start(); 

}



/**

 * synchronized(class)

    synchronized(this)

    ->      monitor,     .

 */

public static void testObjsyn11(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

    Thread t7 = new Thread(  new Runnable() {  public void run() { myt1.m4t0();   }  }, "t7"  ); 

    Thread t8 = new Thread(  new Runnable() {  public void run() { myt1.m4t3();   }  }, "t8"  ); 

    t7.start(); 

    t8.start(); 

}



/**

 * synchronized(class)

    synchronized(lock)

    ->      monitor,     .

 */

public static void testObjsyn12(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

    Thread t7 = new Thread(  new Runnable() {  public void run() { myt1.m4t1();   }  }, "t7"  ); 

    Thread t8 = new Thread(  new Runnable() {  public void run() { myt1.m4t3();   }  }, "t8"  ); 

    t7.start(); 

    t8.start(); 

}





/**

 * synchronized(class)

    synchronized(lockStatic)

    ->      monitor,     .

 */

public static void testObjsyn13(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

    Thread t7 = new Thread(  new Runnable() {  public void run() { myt1.m4t4();   }  }, "t7"  ); 

    Thread t8 = new Thread(  new Runnable() {  public void run() { myt1.m4t3();   }  }, "t8"  ); 

    t7.start(); 

    t8.start(); 

}



/**

 * synchronized  

    synchronized(lockStatic)

    ->      monitor,     .

 */

public static void testObjsyn14(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

    Thread t7 = new Thread(  new Runnable() {  public void run() { myt1.m4t4();   }  }, "t7"  ); 

    Thread t8 = new Thread(  new Runnable() {  public void run() { myt1.m4t2();   }  }, "t8"  ); 

    t7.start(); 

    t8.start(); 

}



/**

 * synchronized  

    synchronized(lockStatic)

    ->      monitor,     .

 */

public static void testObjsyn15(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

    Thread t7 = new Thread(  new Runnable() {  public void run() { myt1.m4t3();   }  }, "t7"  ); 

    Thread t8 = new Thread(  new Runnable() {  public void run() { myt1.m4t2();   }  }, "t8"  ); 

    t7.start(); 

    t8.start(); 

}



/**

 * synchronized  

    synchronized(lockStatic)

    ->      monitor,     .

 */

public static void testObjsyn16(){

    final TestSynchronied8 myt1 = new TestSynchronied8();

    try {

        Thread.sleep(500);

     } catch (InterruptedException e) {

        e.printStackTrace();

     } 

    Thread t7 = new Thread(  new Runnable() {  public void run() { myt1.m4t1();   }  }, "t7"  ); 

    Thread t8 = new Thread(  new Runnable() {  public void run() { myt1.m4t4();   }  }, "t8"  ); 

    t7.start(); 

    t8.start(); 

}







    

    /**

     *  synchronized(class) static synchronized            

     *  synchronized(class)           synchronized(lockStatic)            

     *          synchronized(this)         synchronized            

     * (      ) synchronized(this) (      )  synchronized(lock)            

     * 

     *     synchronized(class) static synchronized         

     *         synchronized(this)         synchronized                   

     *  synchronized(lockStatic)          

     *  synchronized(lock)            

     *            ,       ;          

     */

    public static void main(String[] args) {  

        //        ,      synchronized(this)          

        //        ,      synchronized(lock)      lock

        

//        testObjsyn1();//             ,    . synchronized(this)

//        testObjsyn2();//             ,   . synchronized(this)  

//        testObjsyn3();//             ,   . synchronized(lock)

//        testObjsyn4();//             ,   . synchronized   

//        testObjsyn5();//             ,    . synchronized   

//        testObjsyn6();//            ,   syschronized(this),       ,   

//        testObjsyn7(); //            ,   syschronized(lock),       ,    ,         

//        testObjsyn71(); //            ,   syschronized(lock),   syschronized(this),    ,         

        

        

        /**

           * synchronized(class)

              synchronized(class)

              ->                      ,    .

           */

//        testObjsyn8();//               ,   .synchronized(class)

//        testObjsyn9();//               ,   .synchronized(class)

//        testObjsyn10();//               ,    .synchronized(class),synchronized(this)       

//        testObjsyn11();//               ,    .synchronized(class),synchronized(this)   

//        testObjsyn12();//               ,    .synchronized(class),synchronized(lock)        

//        testObjsyn13();//               ,    .synchronized(class),synchronized(lockStatic)  

//        testObjsyn14();//               ,    .synchronized()  ,synchronized(lockStatic)        

//        testObjsyn15();//               ,   .synchronized()  ,synchronized(class)        

//        testObjsyn16();//               ,    .synchronized(lock)  ,synchronized(lockStatic)        



    } 

}

좋은 웹페이지 즐겨찾기