-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