线程(六)线程池

1.可重用的固定集合线程池,以共享的无界队列的方式来运行这些线程
ExecuterService threadPool = Executors.newFixedThreadPool(3);  //容纳固定的线程
这个线程就是创建一个固定大小的线程池,等待任务来的时候就取线程池中的线程进行任务的执行,这个的好处呢我们大家也是很容易明白的,就是实现创建好了几个线程,至于什么时候用就静待任务的到来了.这个就是我们线程池的基本思想了,我们实现准备好,你来了就直接执行,不需要来了再创建一个线程,这样的话是不是比较方便和节省时间呢,我们来看一下线程执行的效果把:
 * Created by luyangli on 16-6-29.
 */
public class FiledThreadPollTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i=0; i<5; i++) {
            final int threadId = i;
            executorService.execute(new Runnable() {
                @Override public void run() {
                    for(int i=0; i<5; i++){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("任务:" + threadId + "执行了" + i + "");
                    }
                }
            });
        }
    }
}
我们来看一下执行结果:
任务:0执行了0次
任务:1执行了0次
任务:2执行了0次
任务:0执行了1次
任务:1执行了1次
任务:2执行了1次
任务:0执行了2次
任务:1执行了2次
任务:2执行了2次
任务:0执行了3次
任务:2执行了3次
任务:1执行了3次
任务:0执行了4次
任务:2执行了4次
任务:1执行了4次
任务:3执行了0次
任务:4执行了0次
任务:3执行了1次
任务:4执行了1次
我们可以看到的是,线程都是三三一对进行执行的,为什么呢,应为我们是启动了5个任务,每个任务执行5 次,那么三个线程池,肯定是占得满满的,每一次都是三个执行,然后线程执行前三个任务执行完毕之后在执行后面的2个任务
2.可在需要的时候创建一个新的线程,以前创建的线程可以重用

ExecuterService threadPool = Executor.newCachedThreadPool();  //需要的时候创建,可动态分配

这个线程池就是需要几个我们就开辟几个线程,然后一起执行,我们看一下

public class CatchThreadPollTest {
    public static void main(String[] args) {
        for (int i=1; i<=5; i++) {
            ExecutorService catchThreasPoll = Executors.newCachedThreadPool();
            final int threadId = i;
            catchThreasPoll.execute(new Runnable() {
                @Override public void run() {
                    for(int i=0; i<5; i++){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("任务:" + threadId + "执行了" + i + "");
                    }
                }
            });
        }
    }
}
我们可以看到我们并没有执行开辟几个线程但是这个是需要几个我们就开辟几个 ,我们来看一下执行效果:
任务:1执行了0次
任务:2执行了0次
任务:3执行了0次
任务:4执行了0次
任务:5执行了0次
任务:1执行了1次
任务:2执行了1次
任务:3执行了1次
任务:4执行了1次
任务:5执行了1次
任务:2执行了2次
任务:1执行了2次
任务:3执行了2次
任务:4执行了2次
任务:5执行了2次
任务:2执行了3次
任务:1执行了3次
任务:3执行了3次
任务:4执行了3次
任务:5执行了3次
任务:2执行了4次
任务:1执行了4次
任务:3执行了4次
任务:4执行了4次
任务:5执行了4次
每次都是5个任务一起执行哦

3.创建一个单个工作线程的Executer,以无界队列方式运行该线程,
ExecuterService threadPool = Executor.newSingleThreadExecutor(); //创建一个线程的线程池,当当前线程执行任务中断的时候在创建一个新的线程来接着执行该任务

那么党我们需要一个线程执行的时候呢,就是是用这个SingleThreadExecutor线程池思想他呢会在任务中断的时候,重新开辟一个线程,继续接着上面的线程执行的.

 */
public class SingleThreadPollTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new Runnable() {
            @Override public void run() {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while (i == 3) {
                        System.out.println("抛出异常中断线程");
                        try {
                            throw new RuntimeException("抛出异常中断线程");
                        } catch (Exception e) {
                            i++;
                        }
                    }
                    System.out.println("任务:" + Thread.currentThread().getName() + "执行了" + i + "");
                }
            }
        });
    }
}
我们开辟了一个线程执行这个任务,当任务执行到第四边的时候这个线程中断了,按照我们的思想,这个线程池会在创建一个线程继续帮主我们执行完毕:
任务:pool-1-thread-1执行了0次
任务:pool-1-thread-1执行了1次
任务:pool-1-thread-1执行了2次
抛出异常中断线程
任务:pool-1-thread-1执行了4次
我们可以看到第三次是中断了,但是线程池马上又给我们启动了一个新的线程执行该任务了

4.创建一个可以安排给定时延时后的任务执行的线程池

ScheduleExecuterService threadPool = Executor.newScheduleThreadPool(3); 

还有一种就是执行定时任务的线程:

public class ScheduleThreadPollTest {
    public static void main(String[] args) {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        executorService.schedule(new Runnable() {
            @Override public void run() {
                System.out.println("任务:");
            }
        }, 5, TimeUnit.SECONDS);

        executorService.scheduleAtFixedRate(new Runnable() {
            @Override public void run() {
                System.out.println("任务:");
            }
        }, 5, 2, TimeUnit.SECONDS);
    }
}
这个呢我们可以联想到我们的Timer和TimerTask


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值