我是一个Java编程的初学者。我不知道如何改正下面的Java程序。请帮助我知道如何纠正它。非常感谢。
公共类TMA1Q2 {
public static void main(String[] args) {
System.out.println("Usage: java TMA1Q2 {number of Threads}");
// Create tasks
Runnable taskA = new PrintTwoConcurThreads("Thread A ");
Runnable taskB = new PrintTwoConcurThreads(" Thread B ");
// Create threads
Thread thread1 = new Thread(taskA);
Thread thread2 = new Thread(taskB);
// Start threads
thread1.start();
thread2.start();
}}
//实现Runnable的任务
class PrintTwoConcurThreads implements Runnable {
private final String TwoConcurThreads;
private String[] args;
public PrintTwoConcurThreads(String numThreads) {
TwoConcurThreads = numThreads;
}
// Override the run() method
@Override
public void run() {
// Print the value input argument times
int numThreads = Integer.parseInt(args[0]);
Thread[] myThread;
myThread = new Thread[numThreads];
for (int i = 0; i < numThreads; i++) {
System.out.println(TwoConcurThreads + i);
}
}
}发布于 2013-12-08 17:42:56
private String[] args;此args字段从未初始化,因此它的默认值为null。
当您尝试在下面的代码行中访问它时,会得到一个NullPointerException
int numThreads = Integer.parseInt(args[0]);目前还不清楚你想要做什么。至少这将帮助您了解哪里出了问题。
我也不知道为什么要使用下面这几行代码,你创建了Thread[],但从来没有使用过它。
Thread[] myThread;
myThread = new Thread[numThreads];https://stackoverflow.com/questions/20451826
复制相似问题