是否有任何可能的方式來填充列表或多個線程中的任何其他數據?線程的Java填充變量
我已經嘗試使用同步列表。
public class Main {
public static ArrayList<String> list =
Collections.synchronizedList(new ArrayList<String>());
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t1 = new Thread(r);
t1.start();
for(String s : list)
System.out.println(s);
}
}
public class MyRunnable implements Runnable {
@Override
public void run() {
Main.list.add("testing some code");
}
}
是的,有一個可行的辦法。其中一種可能的方法是使用同步列表。你的問題到底是什麼? – yole 2015-02-10 08:28:36
同步列表位於Main類中。在運行線程並填充數據(使用某些字符串)後,從Main類訪問時列表仍爲空 – phantom13 2015-02-10 08:30:05
請向我們顯示代碼。 – yole 2015-02-10 08:30:22