Java线程池

认识线程池

Java线程池_第1张图片

Java线程池_第2张图片 

如何创建线程池

 Java线程池_第3张图片

方法一:使用ExecoutorService的实现类ThreadPoolExecutor创建一个线程池对象 

Java线程池_第4张图片

 创建线程池对象的例子

 ExecutorService pool = new ThreadPoolExecutor(3, 5, 8,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(),
                 new ThreadPoolExecutor.AbortPolicy());

线程池的注意事项

Java线程池_第5张图片

 线程池处理Runnable任务

Java线程池_第6张图片

 Java线程池_第7张图片

package com.itheima.d5;

import java.util.concurrent.*;

public class TheadPoolTest {
    public static void main(String[] args) {
        //1、通过ThreadPoolExecutor创建一个线程池对象
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 8,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        Runnable target = new MyRunnable();
        pool.execute(target);//线程池会自动创建一个新线程,自动处理者个任务,自动执行的
        pool.execute(target);//线程池会自动创建一个新线程,自动处理者个任务,自动执行的
        pool.execute(target);//线程池会自动创建一个新线程,自动处理者个任务,自动执行的
        pool.execute(target);//复用前面的核心线程
        pool.execute(target);//复用前面的核心线程

        //创建临时线程
        pool.execute(target);
        pool.execute(target);

        //拒绝新任务
        pool.execute(target);

        pool.shutdown();//等着线程池的任务全部执行完毕后,再关闭线程池
        pool.shutdownNow();//立即关闭线程池,不管任务是否之心完毕
    }
}
package com.itheima.d5;

public class MyRunnable implements Runnable{
    @Override
    public void run() {
        //任务是干什末的
        System.out.println(Thread.currentThread().getName() + "打印666");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

线程池处理Callable任务

Java线程池_第8张图片

package com.itheima.d5;

import java.util.concurrent.*;

public class ThreadPoolTest2 {
    public static void main(String[] args) throws Exception {
        //1、通过ThreadPoolExecutor创建一个线程池对象
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 8,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        //2、使用线程处理Callable任务
        Future f1 = pool.submit(new MyCallable(100));
        Future f2 = pool.submit(new MyCallable(200));
        Future f3 = pool.submit(new MyCallable(300));
        Future f4 = pool.submit(new MyCallable(400));

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
    }
}
package com.itheima.d5;

import java.util.concurrent.Callable;

public class MyCallable implements Callable {
    private int n;
    public MyCallable(int n){
        this.n = n;
    }

    //2、重写call方法
    @Override
    public String call() throws Exception {
        //描述线程的任务,返回线程执行返回后的结果。
        //需求:1-n的和返回
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return Thread.currentThread().getName() +  "1到n的和" + sum;
    }
}

方法二:Executor工具类实现线程池

Java线程池_第9张图片

package com.itheima.d5;

import java.util.concurrent.*;

public class ThreadPoolTest2 {
    public static void main(String[] args) throws Exception {
        //1、通过ThreadPoolExecutor创建一个线程池对象
//        ExecutorService pool = new ThreadPoolExecutor(3, 5, 8,
//                TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy());


        //1-2 通过Executor创建一个线程对象。
        ExecutorService pool = Executors.newFixedThreadPool(3);
        //计算密集型任务:核心线程数量 = CPU的核数 + 1
        //IO密集型任务:核心线程数量 = CPU的核数 * 2

        //2、使用线程处理Callable任务
        Future f1 = pool.submit(new MyCallable(100));
        Future f2 = pool.submit(new MyCallable(200));
        Future f3 = pool.submit(new MyCallable(300));
        Future f4 = pool.submit(new MyCallable(400));

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
    }
}

Java线程池_第10张图片

你可能感兴趣的:(java,Java多线程,java,开发语言)