Java同步:同步方法和同步块

use*_*592 1 java multithreading

我有一个mulitThread Java应用程序.在一种方法中,需要同步ArrayList.由于arrayList不是线程安全的,所以我必须使用同步.问题是ArrayList类型的对象不是对象的成员变量.该方法的原型如下:

public void simultaneousAccess(ArrayListWrapper aListWrapper){
  ArrayList list = aListWrapper.getList();
  //...Codes manipulate the list            
}
Run Code Online (Sandbox Code Playgroud)

由于多线程,我应该使用

一个)

 public void synchronized simultaneousAccess(ArrayListWrapper aListWrapper){
         ArrayList list = aListWrapper.getList();
         //...Codes manipulate the list            
    }
Run Code Online (Sandbox Code Playgroud)

要么

B)

public void simultaneousAccess(ArrayListWrapper aListWrapper){
     ArrayList list = aListWrapper.getList();
     Synchronized(list){
         //...Codes manipulate the list 
     }           
 }
Run Code Online (Sandbox Code Playgroud)

从性能测试来看,都不起作用.但我不知道为什么?

这里有完整的源代码:

package com.juhani.prototype.sync;

import java.util.ArrayList;

public class ArrayListWrapper {
    public ArrayList<Integer> aList = new ArrayList<Integer>();

    public ArrayListWrapper(){
        Integer one = new Integer(1);
        Integer two = new Integer(2);
        Integer three = new Integer(3);

        aList.add(one);
        aList.add(two);
        aList.add(three);
    }
}

package com.juhani.prototype.sync;

import java.util.ArrayList;

public class TestClass {

    public int count_test=0;

    public synchronized void test(ArrayListWrapper listWrapper){        
        ArrayList<Integer> list = listWrapper.aList;  
        int temp = list.get(1)+1;
        list.set(1,temp);       
    }

    public void testBlock(ArrayListWrapper listWrapper){
        ArrayList<Integer> list = listWrapper.aList;  
        synchronized(list){
            int temp = list.get(1)+1;
            list.set(1,temp);     
        }
    }

}


package com.juhani.prototype.sync;

public class WorkerSyncObj extends Thread {

    ArrayListWrapper listWrapper = null;
    TestClass tc = null;
    int number;

    public WorkerSyncObj(int aNumber){
       number = aNumber;    
    }

    public void setListWrapper(ArrayListWrapper aListWrapper){
        listWrapper = aListWrapper;
    }

    public void setTestClass(TestClass aTc){
        tc = aTc;
    }

    public void run(){
        int i = 1000;
        for(int j=0;j<i;j++){
            tc.testBlock(listWrapper);
            System.out.println("Thread "+number+" is runing at loop "+j+" . index 1 value is:"+listWrapper.aList.get(1)); 
        }       
    }   
}

package com.juhani.prototype.sync.main;

import com.juhani.prototype.sync.ArrayListWrapper;
import com.juhani.prototype.sync.TestClass;
import com.juhani.prototype.sync.WorkerSyncObj;

public class TestMain {

    public static void main(String[] args){

        ArrayListWrapper list = new ArrayListWrapper();
        TestClass tc = new TestClass();

        WorkerSyncObj work1 = new WorkerSyncObj(1);
        work1.setListWrapper(list);
        work1.setTestClass(tc);
        WorkerSyncObj work2 = new WorkerSyncObj(2);
        work2.setListWrapper(list);
        work2.setTestClass(tc);
        WorkerSyncObj work3 = new WorkerSyncObj(3);
        work3.setListWrapper(list);
        work3.setTestClass(tc);

        work1.start();
        work2.start();
        work3.start();



    }

}
Run Code Online (Sandbox Code Playgroud)

zel*_*ler 5

在第一种情况下,您在this对象上的第二种情况下锁定list对象.如果从不同的对象调用方法但列表相同,则可能会出现问题.这可能是第一种情况下例外的原因.
或者你可以尝试一些内置的并发类型,如Collections.synchronizedListCopyOnWriteArrayList.