线程池

线程池概念

线程池,其实就是一个容纳多个线程的容器,其中的线程可以反复使用,省去了频繁创建线程对象的操作,无需反复创建线程而消耗过资源。

原理图:

%title插图%num

使用线程池方式–Runnable接口

通常,线程池都是通过线程池工厂创建,在调用线程池中的方法获取线程,在通过线程去执行任务方法。

jdk1.5新特性,实现线程池程序
* 使用工厂类Executors中的静态方法创建一个线程池对象,指定线程的个数
* static ExecutorService new FixedThreadPool(int 个数)返回线程池对象
* 返回的是ExecutorService接口实现类(线程池对象)
* 接口实现类的对象,调用方法submit(Runnable r)提交一个线程任务

package cn.hiluna.day26.ThreadDemo05;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * jdk1.5新特性,实现线程池程序
 * 使用工厂类Executors中的静态方法创建一个线程池对象,指定线程的个数
 *  static ExecutorService new FixedThreadPool(int 个数)返回线程池对象
 *  返回的是ExecutorService接口实现类(线程池对象)
 *
 *  接口实现类的对象,调用方法submit(Runnable r)提交一个线程任务
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        //调用工厂类的静态方法
        //返回线程池对象,是返回接口
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        //调用接口实现类对象中的方法submit提交线程任务
        //将Runnable接口实现对象传递
        executorService.submit(new ThreadPoolRunnable());
        executorService.submit(new ThreadPoolRunnable());
    }
}

package cn.hiluna.day26.ThreadDemo05;

public class ThreadPoolRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程提交任务");
    }
}

线程池名字可以有上面代码运行结果看出:

%title插图%num

使用线程池方式–Callable接口

Callable接口:与Runnable接口功能相似,用来指定线程的任务。其中call()方法,用来返回线程任务执行完毕后的结果,call方法可抛出异常。

  • 实现线程程序的第三个方式,实现Callable接口方式
  • 实现步骤
  • 工厂类Executors静态方法newFixedThreadPool方法,创建线程池对象
  • 线程池ExecutorService接口实现类,调用方法submit提交线程任务
  • submit(Callable c)
package cn.hiluna.day26.ThreadDemo06;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 实现线程程序的第三个方式,实现Callable接口方式
 * 实现步骤
 *      工厂类Executors静态方法newFixedThreadPool方法,创建线程池对象
 *      线程池ExecutorService接口实现类,调用方法submit提交线程任务
 *      submit(Callable c)
 */
public class ThreadPoolDemo {
    public static void main(String[] args) throws Exception{
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        //提交线程任务的方法submit返回一个Future接口的实现类
        Future future = executorService.submit(new ThreadPoolCallable());
        String s = future.get();
        System.out.println(s);
    }
}

package cn.hiluna.day26.ThreadDemo06;

import java.util.concurrent.Callable;

/**
 * Callable 接口实现类,作为线程提交任务出现
 * 使用方法的返回值
 */
public class ThreadPoolCallable implements Callable {
    @Override
    public String call() throws Exception {
        return "abc";
    }
}


  • 使用多线程技术,求和
  • 一个线程,计算1+100,另一个线程计算1+200的和
  • 多线程的异步计算
package cn.hiluna.day26.ThreadDemo06;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 使用多线程技术,求和
 * 一个线程,计算1+100,另一个线程计算1+200的和
 * 多线程的异步计算
 */
public class ThreadPoolDemo1 {
    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        Future future = executorService.submit(new GetSumCallabel(100));
        Future future1 = executorService.submit(new GetSumCallabel(200));
        System.out.println(future.get());
        System.out.println(future1.get());
    }
}

package cn.hiluna.day26.ThreadDemo06;

import java.util.concurrent.Callable;

public class GetSumCallabel implements Callable {
    private int a;
    public GetSumCallabel(int a){
        this.a = a;
    }
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1;i <= a ;i++){
            sum = sum + i;
        }
        return sum;
    }
}


暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇