建立线程的4种方式

方式一:继承Thread类重写run方法

class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("启动自定义线程");
        super.run();
    }
}

//启动线程
new MyThread().start();

方式二:实现Runnable接口

lambda表达式java

new Thread(()->{
    System.out.println("runnable接口启动");
},"runnable-Thread").start();

方式三:使用FutureTask和Callable接口

FutureTask<String> futureTask=new FutureTask<>(()->{
    return "callable方式启动线程";
});
new Thread(futureTask).start();
try {
    String s = futureTask.get();//线程阻塞获取返回值
    System.out.println(s);
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (ExecutionException e) {
    e.printStackTrace();
}

方式四:使用线程池建立

建立线程池的方式一:使用Executors建立线程池

//1.建立10个线程的线程池
ExecutorService executorService = Executors.newFixedThreadPool(10);
//2.建立缓存的线程池,这种方式能够控制内存不会被占满。
ExecutorService executorService2 = Executors.newCachedThreadPool();
//3.建立拥有定时任务的固定线程个数的线程池,能够定时执行
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
//4.建立单线程
ExecutorService executorService = Executors.newSingleThreadExecutor();

建立线程池的方式二:使用ThreadPoolExecutor建立自定义的线程池

new ThreadPoolExecutor(		  int corePoolSize,  //核心线程个数
                              int maximumPoolSize,//最大的线程个数
                              long keepAliveTime,//超过核心线程个数的空闲线程,空闲线程的的存活时间,超过就会被销毁
                              TimeUnit unit,//时间单位
                              BlockingQueue<Runnable> workQueue) //线程队列,超过maximumPoolSize值能存下的线程队列个数
                              ThreadFactory threadFactory,//建立线程的线程工厂
                              RejectedExecutionHandler handler //超过线程池个数的线程的拒绝策略
//1.公平的线程队列,若是不传第二参数则默认是false
ArrayBlockingQueue<Runnable> workQueue1 = new ArrayBlockingQueue<>(10, true);
//2.默认阻塞队列个数,空参构造方法会获得一个默认的线程队列值为Integer.MAX_VALUE 2的31次方
LinkedBlockingDeque<Runnable> workQueue2 = new LinkedBlockingDeque<>(10);

/** *建立自定义的线程池示例 */
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
6, //核心线程6个
20, //最大线程20个
20, //空闲时间20秒(由下面的参数设置),非核心线程的空闲时间超过则销毁
TimeUnit.SECONDS,//时间可是秒,若是要其它单位则换成对应的便可
workQueue2,   //使用的是LinkedBlockingDeque,也能够使用ArrayBlockingQueue
Executors.defaultThreadFactory(),//默认的线程工厂建立新线程
new ThreadPoolExecutor.AbortPolicy()//直接拒绝新任务
);

在这里插入图片描述
分表表明缓存

  1. 直接丢弃新来的任务
  2. 因为没有空闲线程,而且阻塞队列也满了,可是又执行线程call方法l至关于同步调用,不会开一个线程
  3. 丢弃最老的线程,不会抛异常
  4. 直接丢弃新来的任务,不会抛异常
为何须要有线程池?使用线程池的目的是什么?

解答:
若是使用new Thread方式启动线程,那么线程将不受控制,
若是是在where (true)里执行new Thread则会致使一直建立线程,这样很快就会耗尽计算机资源,容易形成死机。ide