zoukankan      html  css  js  c++  java
  • Synchnorized 辨析 (1)

    示例代码:


    public class SyncTest {

     

          private ListString list= new ArrayListString();

     

          public void syncAddPrint(){

                 synchronized(list){

                        for(int i = 0; i 10; i++) {

                                                                         list.add(""+ i);

                              System.out.println(list);

                        }

                 }

         } 

          public void syncAdd2(){

                 synchronized(list){

                        list.add("A");

                        list.add("B");

                        list.add("C");

                 }

          }

     

          public void  noSyncAdd() {

                 list.add("X");

                 list.add("Y");

                 list.add("Z");

          }

     

          public void noSyncNew(){

                 list= new ArrayListString();

                 list.add("Ha");

                 list.add("Ha");

                 list.add("Ha");

          }

     

          

          public static void main(String[] args){

                 final SyncTest test =new SyncTest();

     

                 Thread thread1 = new Thread() {

                        public void run() {

                              test.syncAddPrint();

                        }

                 };

                 thread1.start();

     

                 Thread thread2 = new Thread() {

                        public voidrun() {

                              test.syncAdd2();

                        }

                 };

                 thread2.start();

     

                 Thread thread3 = new Thread() {

                        public voidrun() {

                              test.noSyncAdd();

                        }

                 };

                 thread3.start();

     

                 Thread thread4 = new Thread() {

                        public voidrun() {

                              test.noSyncNew();

                        }

                 };

                 thread4.start();

          }

     

    }

     

    输出结果可能是:

    [0]

    [0,1]

    [0, 1,2]

    [0, 1, 2,X, 3]

    [0, 1, 2,X, 3, Y, 4]

    [Ha, 5]

    [Ha, 5,Z, 6]

    [Ha, 5,Z, 6, Ha, Ha, 7]

    [Ha, 5,Z, 6, Ha, Ha, 7, 8]

    [Ha, 5,Z, 6, Ha, Ha, 7, 8, 9]


    thread1执行方法syncAddPrint的过程中,thread1获得了锁list


    thread1获得锁list未释放线程thread2执行syncAdd2,但是得不到锁list故而被阻塞。


    thread3执行noSyncAdd,不需要获得锁list所以在方法中轻易将锁list的元素改变


    thread4执行noSyncNew,不需要获得锁list所以在方法中轻易将锁list替换成另一个对象。


    thread3thread4这些对list的改变动作都是在thread1还持有锁list的情况下发生的。

     

    由此可见,要保证锁list在被某线程持有的情况下不被改变,则必须访问锁list的所有代码块都必须使用synchronized(list)才可以,一旦某个方法遗漏,则不可保证。

  • 相关阅读:
    AUDIT审计的一些使用
    HOW TO PERFORM BLOCK MEDIA RECOVERY (BMR) WHEN BACKUPS ARE NOT TAKEN BY RMAN. (Doc ID 342972.1)
    使用BBED理解和修改Oracle数据块
    Using Class of Secure Transport (COST) to Restrict Instance Registration in Oracle RAC [ID 1340831.1]
    调试利器GDB概念
    第4章 思科IOS
    第3章 ip地址和子网划分
    第2章 TCPIP
    2020年阅读过的黑客资源推荐篇
    第1章 计算机网络
  • 原文地址:https://www.cnblogs.com/leeeee/p/7276472.html
Copyright © 2011-2022 走看看