同步

2012-02-09 45 views
-1

可能重複:
Synchronization of non-final field同步

我有一個ArrayList中,當我使用這個:

//declare it as a global ArrayList 
private ArrayList <Human> myList = new ArrayList <Human>() ; 

//inside a method 
synchronized (myList){  
    for( ListIterator<Human> it = myList.listIterator();it.hasNext();){ 
     Human = it.next(); 

它告訴我,同步在非最終場。我該怎麼辦?

+0

http://stackoverflow.com/questions/6910807/synchronization-of-non-final-field – kosa 2012-02-09 20:08:23

+0

我在互聯網上搜索沒有計算器。我很抱歉,我可以刪除嗎? – 2012-02-09 20:09:45

+0

在問題的底部,在標籤下應該有一個'delete'鏈接。 – 2012-02-09 20:17:41

回答

2

您可以在非最終場同步(在這個例子中myList中)

您可以使用synchronized塊不帶參數

synchronized{  
    for( ListIterator<Human> it = myList.listIterator();it.hasNext();){ 
     Human = it.next(); 

或者創建擁有「最終」屬性的另一個類的屬性。

PS。您可以使用foreach循環以及

for(Human h : myList){ 
      h.getName(); 
}