java threadpool 线程池 demo

Java

2017-06-22

137

0

技术:java8+ThreadPool

运行环境:IDEA 15.2 + jdk8 + windows 7

demo功能:提供jdk8 线程池类的基本使用demo

ps: 线程池, 批量执行 可执行的任务的, 线程池可以自己管理线程的生老病死。NewScheduledThreadPool,NewSingleThreadExecutor,NewWorkStealingPool这三个类使用方式请看源代码  

参考:http://blog.csdn.net/u011240877/

新建任务类。 这个任务类, 是线程池可以执行的任务

public class MyTask implements Runnable {
    private int taskType;
    private String taskName;
    public MyTask(int taskType, String taskName) {
        this.taskType = taskType;
        this.taskName = taskName;
    }
    @Override
    public void run() {
        System.out.println("task is running at thread " + Thread.currentThread().getId() + ",task type=" + taskType + ",task name=" + taskName);
    }
    public int getTaskType() {
        return taskType;
    }
    public void setTaskType(int taskType) {
        this.taskType = taskType;
    }
    public String getTaskName() {
        return taskName;
    }
    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }
}

NewSingleThreadExecutor Demo

执行的结果是, 所有任务使用的都是一个线程id执行的任务

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class NewSingleThreadExecutorDemo {
    static ExecutorService pool = Executors.newSingleThreadExecutor();
    public static void test() {
        pool.execute(new MyTask(1, "111"));
        pool.execute(new MyTask(2, "222"));
        pool.execute(new MyTask(3, "333"));
        pool.execute(new MyTask(4, "444"));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        pool.shutdown();
    }
}

NewFixedThreadPool Demo

使用固定线程数的线程 执行全部任务, 如果任务数少于线程数, 每个任务执行的线程都不一样;

如果任务数大于线程数, 会出现多个任务使用同一个线程的情况(复用呀)

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class NewFixedThreadPoolDemo {
    static ExecutorService pool = Executors.newFixedThreadPool(10);
    public static void test() {
        pool.execute(new MyTask(1, "111"));
        pool.execute(new MyTask(2, "222"));
        pool.execute(new MyTask(3, "333"));
        pool.execute(new MyTask(4, "444"));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        pool.shutdown();
    }
}

测试类

//执行所有的任务, 都使用一个线程。执行结果是 所有的任务都用的统一个线程id执行
@Test
public void test1() {
    NewSingleThreadExecutorDemo.test();
}
//使用指定数量的线程, 执行全部的任务。 不同的任务可能用到相同的线程id执行
@Test
public void test2() {
    NewFixedThreadPoolDemo.test();
}

结果

test1()

task is running at thread 12,task type=1,task name=111
task is running at thread 12,task type=2,task name=222
task is running at thread 12,task type=3,task name=333
task is running at thread 12,task type=4,task name=444

 

test2()

task is running at thread 15,task type=4,task name=444
task is running at thread 13,task type=2,task name=222
task is running at thread 14,task type=3,task name=333
task is running at thread 12,task type=1,task name=111

 

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think