java线程池学习

     通常使用 Executor(执行器) 而不是显式地创建Thread对象。ExecutorService会自动切换上下文执行线程任务。

    1.CachedThreadPool:缓存线程池,会重复利用线程,线程不够会新建。

public class ExecutorCachedThreadPool {
    public static void main(String[] args)
    {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i =0 ; i< 10 ; i++)
        {
            executorService.execute(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        //启动一次顺序关闭,执行以前提交的任务,但不接受新任务。
        executorService.shutdown();
        //如果此执行程序已关闭,则返回 true        boolean isShutdown = executorService.isShutdown();
        //如果关闭后所有任务都已完成,则返回 true        boolean isTerminated = executorService.isTerminated();
        while(!executorService.isTerminated())
        {
            Thread.yield();
        }
        System.out.println("main end!");
    }
}
执行结果:
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-6
pool-1-thread-4
pool-1-thread-3
pool-1-thread-8
pool-1-thread-1
pool-1-thread-5
pool-1-thread-7
main end!
    2.FixedThreadPool:固定数量线程池,在创建时需要预先分配线程的数量,与CachedThreadPool的区别就是限制了线程的数量。
public class ExecutorFixedThreadPool {
    public static void main(String[] args)
    {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for(int i = 0 ; i < 8 ; i ++)
        {
            executorService.execute(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        executorService.shutdown();
        while(!executorService.isTerminated())
        {
            Thread.yield();
        }
        System.out.println("main end");
    }
}
执行结果:

pool-1-thread-2
pool-1-thread-3
pool-1-thread-4
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-1
pool-1-thread-5
main end


    3.SingleThreadPool:单线程池,一个线程执行任务,按照创建任务的顺序依次执行。

public class ExecutorSingleThread {
    public static void main(String[] args)
    {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for(int i =0 ; i< 5 ; i++)
        {
            executorService.execute(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        executorService.shutdown();
        while(!executorService.isTerminated())
        {
            Thread.yield();
        }
        System.out.println("main end");
    }
}
执行结果:

pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
main end


最近复习java基础,若有不妥之处欢迎指点......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值