Tuesday, February 17, 2009

簡單的multithread

很久沒有寫過programme,連一些很簡單的Java Syntax也要Google,真的要練習練習~



// CounterThread.java
// 1st Argument = No Of Threads
// 2nd Argument = Counter Value

public class CounterThread extends Thread {

public static int counterValue = 0;

public void run(){
for (int i = 0; i < counterValue ;i++){
System.out.println(Thread.currentThread().getName() + "\t" + i);
}
}

public static void main(String arg[]){
if (arg.length==2){
int noOfThreads = Integer.parseInt(arg[0]);
counterValue = Integer.parseInt(arg[1]);

CounterThread [] ct = new CounterThread[noOfThreads];
System.out.println("No Of Threads = " + noOfThreads);
System.out.println("Counter Value = " + counterValue);

for (int i = 0; i < noOfThreads ; i++){
ct[i] = new CounterThread();
ct[i].start();
}
}
else{
System.out.println("Argument incorrct!!");
}
}
}

No comments: