Java 執行緒池建立 理解(簡單)(只要反覆理解,就能理解)
阿新 • • 發佈:2018-12-13
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class cachethreadpool {
public static void main (String[] args) {
ExecutorService es = Executors.newFixedThreadPool(3);
Prints pts = new Prints(1);
Prints2 pts2 = new Prints2(2);
while (true) {
es.execute(pts);
es.execute(pts2);
}
/*Prints pts = new Prints(1);
pts.run();*/
}
}
class Prints implements Runnable {
int thnum;
public Prints(int thnum) {
this.thnum = thnum;
}
public void run() {
Long time;
Long time2;
Long time3;
time = System.currentTimeMillis ();
for (int i = 0; i < 20; i++) {
try {
System.out.println("執行緒" + thnum + " NO." + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
time2 = System.currentTimeMillis ();
time3 = time2 - time;
System.out.println(time3);
}
}
class Prints2 implements Runnable {
int thnum;
public Prints2(int thnum) {
this.thnum = thnum;
}
public void run() {
Long time;
Long time2;
Long time3;
time = System.currentTimeMillis ();
for (int i = 0; i < 20; i++) {
try {
System.out.println("執行緒" + thnum + " NO." + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
time2 = System.currentTimeMillis();
time3 = time2 - time;
System.out.println(time3);
}
}