Ho copiato questo codice da un libro per imparare i tread.Però il codice dà errore e non capisco perchè.Potete aiutarmi?
[CODE]
public class ThredPriority implements Runnable {
CounterThread thread1 = new CounterThread();
CounterThread thread2 = new CounterThread();
Thread thisThread = new Thread(this);
int duration;
public ThredPriority(int priority1, int priority2,int duration) {
this.duration = duration;
thisThread.setPriority(Thread.MAX_PRIORITY);
thread1.setPriority(priority1);
thread2.setPriority(priority2);
thread1.start();
thread2.start();
thisThread.start();
}
public void run() {
try {
for (int i = 0; i < duration; i++){
System.out.println("Thread1: priority: " + thread1.getPriority() + " count: " + thread1.count);
System.out.println("Thread2: priority: " + thread2.getPriority() + " count: " + thread2.count);
thisThread.sleep(1000);
}
}
catch (InterruptedException e){}
thread1.terminate();
}
public static void main(String[] args) {
new ThredPriority(Integer.parseInt(args[0]),Integer.parseInt(args[1]), Integer.parseInt(args[2]));
}
}
Questa è la classe CounterThread
[CODE]
public class CounterThread extends Thread {
boolean terminated = false;
int count = 0;
public void run() {
while (!terminated) {
count++;
for (int i = 0; i < 1000; i++) {
}
}
}
public void terminate() {
terminated = true;
}
public int getCount() {
return count;
}
}
L'errore che dà è:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ThredPriority.main(ThredPriority.java:30)
Quello che non capisco è nonostante viene passato un Integer al metodo new Thread, che valore ha l'Integer e sopratutto dove lo prende se viene passato un arg[0] e via dicendo?