public class MyThread implements Runnable { public void run(){ }
}
public static void main(String args[]) { for ( int i=0;i<100;i++) {
Thread t = new Thread(new MyThread());
t.start();
}
}OK
}
public static void main(String args[]) { for ( int i=0;i<100;i++) {
Thread t = new Thread(new MyThread());
t.start();
}
}OK
new Thread(){
public void run(){
............
..................
}
}.start();
}
以下是一段简单的代码.仅供参考.import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;public class TestThread implements Runnable {
public static int num = 0;
public void run() {
System.out.println(num++);
}
/**
* @param args
*/
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new
ThreadPoolExecutor(10,10,0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
for (int i = 0; i < 100; i++) {
TestThread testThread = new TestThread();
threadPoolExecutor.execute(testThread);
}
}
}
}
public static void main(String args[]) { for ( int i=0;i <100;i++) {
Thread t = new Thread(new MyThread());
t.start();
}
}
但存要开100个线程来运行,当然直接创建就行,如一个单机的处理代码,测试100个线程下的并行处理。
如果要100个线程反复使用,则需要用线程池,如服务器代码的服务端处理。