线程池newFixedThreadPool的使用
2008-09-24 11:17
459 查看
新的线程加入后,如果正在运行的线程达到了上限,则会阻塞,直到有了空闲的线程来运行。import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* 线程池newFixedThreadPool的使用。
*
* @author 赵学庆,Java世纪网(java2000.net)
*
*/
public class ExecutorTest {
public static void main(String args[]) {
Random random = new Random();
// 建立一个容量为3的固定尺寸的线程池
ExecutorService executor = Executors.newFixedThreadPool(3);
// 判断可是线程池可以结束
int waitTime = 500;
for (int i = 0; i < 10; i++) {
String name = "线程 " + i;
int time = random.nextInt(1000);
waitTime += time;
Runnable runner = new ExecutorThread(name, time);
System.out.println("增加: " + name + " / " + time);
executor.execute(runner);
}
try {
Thread.sleep(waitTime);
executor.shutdown();
executor.awaitTermination(waitTime, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignored) {
}
}
}
class ExecutorThread implements Runnable {
private final String name;
private final int delay;
public ExecutorThread(String name, int delay) {
this.name = name;
this.delay = delay;
}
public void run() {
System.out.println("启动: " + name);
try {
Thread.sleep(delay);
} catch (InterruptedException ignored) {
}
System.out.println("完成: " + name);
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* 线程池newFixedThreadPool的使用。
*
* @author 赵学庆,Java世纪网(java2000.net)
*
*/
public class ExecutorTest {
public static void main(String args[]) {
Random random = new Random();
// 建立一个容量为3的固定尺寸的线程池
ExecutorService executor = Executors.newFixedThreadPool(3);
// 判断可是线程池可以结束
int waitTime = 500;
for (int i = 0; i < 10; i++) {
String name = "线程 " + i;
int time = random.nextInt(1000);
waitTime += time;
Runnable runner = new ExecutorThread(name, time);
System.out.println("增加: " + name + " / " + time);
executor.execute(runner);
}
try {
Thread.sleep(waitTime);
executor.shutdown();
executor.awaitTermination(waitTime, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignored) {
}
}
}
class ExecutorThread implements Runnable {
private final String name;
private final int delay;
public ExecutorThread(String name, int delay) {
this.name = name;
this.delay = delay;
}
public void run() {
System.out.println("启动: " + name);
try {
Thread.sleep(delay);
} catch (InterruptedException ignored) {
}
System.out.println("完成: " + name);
}
}
相关文章推荐
- 线程池newFixedThreadPool的使用
- android 多线程 - 线程池 Executors.newFixedThreadPool 的使用例子
- 线程池的使用 ExecutorService newFixedThreadPool(int nThreads)
- 线程池的使用(newCachedThreadPool、newFixedThreadPool、newScheduledThreadPool、newSingleThreadExecutor)
- Executors.newFixedThreadPool线程池的使用
- ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
- Java 自带的线程池Executors.newFixedThreadPool
- 使用无限大小线程池 newCachedThreadPool 可能遇到的问题
- 线程池Executors.newFixedThreadPool
- JAVA线程池 newSingleThreadExecutor,newFixedThreadPool,newCachedThreadPool,newScheduledThreadPool(一)
- ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
- Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- 使用无限大小线程池 newCachedThreadPool 可能遇到的问题
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- 使用newFixedThreadpool(size)时,系统线程数超过size。
- ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
- ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor