Java 问:;答:复杂的僵局评估测试和答案的评分
以下代码是为测试而构造的。在这个测试中,要求读者解释为什么代码在启动代码后不到一秒钟就会进入死锁 有人能准确地描述一下是什么导致了这段代码中的死锁吗Java 问:;答:复杂的僵局评估测试和答案的评分,java,multithreading,Java,Multithreading,以下代码是为测试而构造的。在这个测试中,要求读者解释为什么代码在启动代码后不到一秒钟就会进入死锁 有人能准确地描述一下是什么导致了这段代码中的死锁吗 public class Test { static class FailerThread implements Runnable { final Object[] objects; final Random random; final int number; public FailerThread(fin
public class Test {
static class FailerThread implements Runnable {
final Object[] objects;
final Random random;
final int number;
public FailerThread(final Object[] objects, final int number) {
this.objects = objects;
this.random = new Random();
this.number = number;
}
@Override
public void run() {
final boolean isWriter = number % 2 == 0;
int index = random.nextInt(objects.length);
try {
while (Thread.interrupted() == false) {
synchronized (objects) {
if (isWriter) {
while (objects[index] == null) {
System.out.println(number + ": Index " + index + " is null, waiting...");
objects.wait();
}
for (int copyIndex = 0; copyIndex < objects.length; ++copyIndex) {
if (objects[copyIndex] == null) {
objects[copyIndex] = this.objects[index];
}
}
objects.notifyAll();
} else {
objects[index] = null;
}
}
++index;
if (index >= objects.length) {
index = 0;
}
}
} catch (InterruptedException e) {
}
}
}
public static void main(String[] args) throws InterruptedException {
final Object[] objects = new Object[10];
for (int i = 0; i < objects.length; ++i) {
objects[i] = new Object();
}
final int NUM_THREADS = 32;
final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
for (int i = 0; i < NUM_THREADS; ++i) {
executor.execute(new FailerThread(objects, i));
}
}
}
公共类测试{
静态类FailerThread实现可运行{
最终目标[]目标;
最终随机;
最终整数;
public FailerThread(最终对象[]对象,最终整数){
this.objects=对象;
this.random=新的random();
这个数字=数字;
}
@凌驾
公开募捐{
最终布尔值isWriter=number%2==0;
int index=random.nextInt(objects.length);
试一试{
while(Thread.interrupted()==false){
已同步(对象){
if(isWriter){
while(对象[索引]==null){
System.out.println(number+“:Index“+Index+”为空,正在等待…”);
对象。wait();
}
对于(int-copyIndex=0;copyIndex=objects.length){
指数=0;
}
}
}捕捉(中断异常e){
}
}
}
公共静态void main(字符串[]args)引发InterruptedException{
最终对象[]对象=新对象[10];
对于(int i=0;i
编辑:此测试的官方答案(与都铎所写内容类似,只是更详细)
上述构造死锁,因为在某一点上,所有“编写器”都等待空值,但由于这些编写器是唯一可以释放它们的编写器,它们将无限期地挂起。然而,更重要的问题是:为什么
乍一看,代码似乎是由那些作者主导的。每个循环都选择一个线程(写入器或空值器)在数组上进行处理,但是当空值器只写入一个空值时,写入器会消除数组中的所有空值。因此,我们可以预期死锁——虽然可能——是不太可能的(但令人惊讶的是,代码在一秒钟内就死锁了)。然而,仔细看,这个假设是错误的,因为我们正在处理线程
给定足够的执行时间,在多线程应用程序中,重要的是:代码的哪一部分实际上能够阻塞?让我们来看看作者/Nulels: 可能出现的最坏情况。
- 空值器可以在最坏的情况下执行而不产生任何效果。即:它将null写入数组中已为null的位置
- 在最坏的情况下,编写器可以无限期地阻止
预见到一些人会认为这个问题对于评估测试来说太难了,并且给出你的测试者的观点,这就是候选人将被评定为:
是的,这个问题太难了,而且没有人会在考试中找到正确答案,是什么 while (!Thread.interrupted())
} else {
objects[index] = null;
while (objects[index]==null) {
System.out.println(number + ": Index " + index + " is still null, waiting...");
objects.wait();
}
}
while (Thread.interrupted() == false) {
synchronized (objects) {
System.out.println("running isWriter=" + isWriter + " thread #" + number);