当我的类的costructor中更新变量N时,如何更新boolean(choose)和int(number)数组?
public class Lamport_Algorithm implements Runnable {
private static int N = 0;
private static boolean choosing[] = new boolean[N];
private static int number[] = new int[N];
private final int idProcess;
public Lamport_Algorithm(int idProcess) {
this.idProcess = idProcess;
if (N == 0) {
N = 1;
} else {
N += 1;
}
}
public void run() {
System.out.println("Hello" + "[" + idProcess + "]" + " There are: " + N + " Thread ");
for (int i = 0; i < N; i++) {
System.out.println(choosing[i]);
}
} }
这是主要的:
public class MainTest {
public static void main(String[] args){
Lamport_Algorithm a = new Lamport_Algorithm(1);
Thread T1 = new Thread (a);
T1.start();
}
}
答案 0 :(得分:0)
您可以在构造函数中初始化两个数组,如下所示。
您的完整版代码。
class Lamport_Algorithm implements Runnable {
private static int N = 0;
private static boolean choosing[];
private static int number[];
private final int idProcess;
public Lamport_Algorithm(int idProcess) {
this.idProcess = idProcess;
if (N == 0) {
N = 1;
} else {
N += 1;
}
choosing = new boolean[N];
number = new int[N];
}
public void run() {
System.out.println("Hello" + "[" + idProcess + "]" + " There are: " + N + " Thread ");
for (int i = 0; i < N; i++) {
System.out.println(choosing[i]);
}
}
}
class MainTest {
public static void main(String[] args){
Lamport_Algorithm a = new Lamport_Algorithm(1);
Thread T1 = new Thread (a);
T1.start();
}
}