【发布时间】:2021-05-28 18:04:59
【问题描述】:
为什么我没有将 list1 和 list2 的列表大小设为 1000 在下面的代码中:
我的主要方法代码如下:
ExecutorService executor = Executors.newFixedThreadPool(2);
for(int j=0; j<2; j++) {
executor.submit(new Worker(j));
}
executor.shutdown();
System.out.println("All tasks submitted: ");
try {
executor.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
}
long end = System.currentTimeMillis();
System.out.println("Time taken: " + (end - start));
System.out.println("List1: " + list1.size() + "; List2: " + list2.size());
这里是Worker 类:
class Worker implements Runnable{
private int id;
public Worker(int id) {
this.id = id;
}
private Random random = new Random();
private final Object lock1 = new Object();
private final Object lock2 = new Object();
public void stageOne() {
synchronized(lock1) {
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
WorkerThreadPool.list1.add(random.nextInt(100));
}
}
public void stageTwo() {
synchronized(lock2) {
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
WorkerThreadPool.list2.add(random.nextInt(100));
}
}
public void process() {
for(int i=0; i<500; i++) {
stageOne();
stageTwo();
}
}
@Override
public void run() {
System.out.println("Starting thread: " + id);
process();
System.out.println("Completed: " + id);
}
}
一次运行我得到了
List1: 986; List2: 989
我得到的其他时间
List1: 994; List2: 981
但每次都应该给
List1: 1000; List2: 1000
这里有什么问题?
【问题讨论】:
-
点击 pastebin 链接..无法在此处发布整个代码..
-
可能是因为 ArrayList 不是线程安全的
-
但是我在同步块中调用它..所以没关系..可能是不同的原因?
-
它必须是线程安全的,因为如果我这样做
List<Integer> list1 = Collections.synchronizedList(new ArrayList<>())并且与 list2 相同,那么它总是 1000。自己尝试一下,不要相信我的话 :) 如果我确切地知道为什么我会把它作为一个答案 -
我想这与你使用两把锁有关。
标签: java multithreading executorservice