我想编写一个静态方法stressTest (int n, int m),它创建 n 个子进程,将 m 个元素添加到MySafeList所有子进程之间的共享中。如果添加的元素数量与预期数量 (n × m) 不同,或者在执行过程中抛出异常,则该方法必须返回 false。确保您的测试因MySafeList未同步而失败。
然后我尝试使用静态列表并创建一个线程表。
import java.util.ArrayList;
class MySafeList extends ArrayList<Double>{
static ArrayList<Double> mySafeList;
public boolean add(Double d){
mySafeList.add(d);
}
public int size(){
return mySafeList.size();
}
public Double get(int i){
mySafeList.get(i);
}
static boolean stressTest(int n, int m){
Thread threads[] = new Thread[n]; //El nombre de tu variable se llama "threads"
try{
for(int i=0; i<n; i++){
threads[i] = new Thread(new ThreadTest(1));
threads[i].start();
for(int j=0; j<m;j++){
Double d = new Double((double) j);
mySafeList.add(d);
}
}
}catch(Exception e){
return false;
}
}
public static void main(String[] args) {
MySafeList safeList = new MySafeList();
stressTest(2,4);
}
}
然而我得到:
C:\Users\antoi\Documents\Programming\Julien\TD6_multi_threading>javac MySafeList.java
MySafeList.java:9: error: missing return statement
}
^
MySafeList.java:17: error: missing return statement
}
^
MySafeList.java:33: error: missing return statement
}
^
Note: MySafeList.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 errors
慕尼黑5688855
相关分类