java 中关于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)        



    } 

}

 

你可能感兴趣的:(synchronized)