-3
我已經寫了使用synchronizedList()下面的代碼:爲什麼synchronizedList不能正常工作
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collectionsdemo;
import java.util.*;
class ThreadTest implements Runnable
{
Thread t;
private int var;
public ThreadTest(int var)
{
t = new Thread(this);
this.var = var;
t.start();
}
@Override
public void run()
{
System.out.println(var);
}
}
public class CollectionsDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
List<ThreadTest> synlist = Collections.synchronizedList(new ArrayList<>());
synlist.add(new ThreadTest(1));
synlist.add(new ThreadTest(2));
synlist.add(new ThreadTest(3));
synlist.add(new ThreadTest(4));
synlist.add(new ThreadTest(5));
for (int i = 0; i < 5; i++) {
synlist.get(i).t.join();
}
//System.out.println("Sunchronized list is :"+list);
}
}
現在,還是我在正確的順序獲得輸出沒有。他們每次都以不同的順序進來。現在做什麼?如何正確使用synchronizedList()方法?
您預期的順序是什麼?爲什麼? –
再次閱讀'Collections.synchronizedList'規範。您會發現它與啓動線程的順序無關(但只提供線程安全操作) –
線程的整個點是*並行執行*。你爲什麼期望他們按照任何特定的順序運行? – shmosel