JDK1.5 线程池

 



 

Executors类主要方法:

a.       static ExecutorService newCachedThreadPool()

产生一个ExecutorService对象,这个对象带有一个线程池,线程池的大小会根据需要调整,线程执行完任务后返回线程池,供执行下一次任务使用

b.       static ExecutorService newFixedThreadPool(int poolSize)

产生一个ExecutorService对象,这个对象带有一个大小为poolSize的线程池,若任务数量大于poolSize,任务会被放在一个queue里顺序执行

c.       static ExecutorService newSingleThreadExecutor()

产生一个ExecutorService对象,这个对象只有一个线程可用来执行任务,若任务多于一个,任务将按先后顺序执行。

 

一.static ExecutorService newFixedThreadPool(int poolSize)创建一个可重用固定线程集合的线程池,以共享的无界队列方式来运行这些线程。

实例1

package com.bijian.thread;

public class MyThread extends Thread {
	
	private String nickName;
	
	public MyThread(String nickName) {
		super();
		this.nickName = nickName;
	}

	@Override
	public void run() {

		try {
			Thread.sleep(50);
		} catch(Exception e) {
			
		}

		System.out.println(Thread.currentThread().getName() + ": "+ nickName + "正在执行。。。");
		
		try {
			Thread.sleep(100);
		} catch(Exception e) {
			
		}
	}
}

 

package com.bijian.thread;

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

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//创建一个可重用固定线程数的线程池
		ExecutorService pool = Executors.newFixedThreadPool(2);
		//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
		Thread t1 = new MyThread("A");
		Thread t2 = new MyThread("B");
		Thread t3 = new MyThread("C");
		Thread t4 = new MyThread("D");
		Thread t5 = new MyThread("E");
		//将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		//pool.exe
		
		//关闭线程池
		pool.shutdown();
	}
}

 

运行结果:
pool-1-thread-2: B正在执行。。。
pool-1-thread-1: A正在执行。。。
pool-1-thread-2: C正在执行。。。
pool-1-thread-1: D正在执行。。。
pool-1-thread-1: E正在执行。。。

 

 

二.static ExecutorServicenewSingleThreadExecutor()创建一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程。

实例2(修改Main类如下):

package com.bijian.thread;

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

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//创建一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程
		ExecutorService pool = Executors.newSingleThreadExecutor();
		//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
		Thread t1 = new MyThread("A");
		Thread t2 = new MyThread("B");
		Thread t3 = new MyThread("C");
		Thread t4 = new MyThread("D");
		Thread t5 = new MyThread("E");
		//将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		//关闭线程池
		pool.shutdown();
	}
}

 

运行结果:
pool-1-thread-1: A正在执行。。。
pool-1-thread-1: B正在执行。。。
pool-1-thread-1: C正在执行。。。
pool-1-thread-1: D正在执行。。。
pool-1-thread-1: E正在执行。。。

 

 

三.static ExecutorServicenewCachedThreadPool()创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。

实例3修改Main类如下): 

package com.bijian.thread;

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

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们
		ExecutorService pool = Executors.newCachedThreadPool();
		//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
		Thread t1 = new MyThread("A");
		Thread t2 = new MyThread("B");
		Thread t3 = new MyThread("C");
		Thread t4 = new MyThread("D");
		Thread t5 = new MyThread("E");
		//将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		
		try {
			Thread.sleep(4000);
		} catch(Exception e) {
			
		}
		
		Thread t6 = new MyThread("F");
		pool.execute(t6);
		
		//关闭线程池
		pool.shutdown();
	}
}

 运行结果: 

pool-1-thread-1: A正在执行。。。
pool-1-thread-5: E正在执行。。。
pool-1-thread-4: D正在执行。。。
pool-1-thread-2: B正在执行。。。
pool-1-thread-3: C正在执行。。。
pool-1-thread-1: F正在执行。。。

 

 四.static ScheduledExecutorServicenewScheduledThreadPool(int corePoolSize): 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。

       ScheduledExecutorService接口方法:

        ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)创建并执行在给定延迟后启用的一次性操作。

实例4修改Main类如下): 

package com.bijian.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行
		ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
		//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
		Thread t1 = new MyThread("A");
		Thread t2 = new MyThread("B");
		Thread t3 = new MyThread("C");
		Thread t4 = new MyThread("D");
		Thread t5 = new MyThread("E");
		//将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		
		//1000毫秒后执行t4线程
		pool.schedule(t4, 1000, TimeUnit.MILLISECONDS);
		//1000毫秒后执行t5线程
		pool.schedule(t5, 1000, TimeUnit.MILLISECONDS);
		//关闭线程池
		pool.shutdown();
	}
}

 运行结果: 

pool-1-thread-2: B正在执行。。。
pool-1-thread-1: A正在执行。。。
pool-1-thread-1: C正在执行。。。
pool-1-thread-2: D正在执行。。。
pool-1-thread-1: E正在执行。。。

 

 

五.static ScheduledExecutorServicenewSingleThreadScheduledExecutor()创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。

实例5修改Main类如下): 

package com.bijian.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行
		ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
		//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
		Thread t1 = new MyThread("A");
		Thread t2 = new MyThread("B");
		Thread t3 = new MyThread("C");
		Thread t4 = new MyThread("D");
		Thread t5 = new MyThread("E");
		//将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		
		pool.schedule(t4, 5000, TimeUnit.MILLISECONDS);
		pool.schedule(t5, 1000, TimeUnit.MILLISECONDS);
		//关闭线程池
		pool.shutdown();
	}
}

 运行结果: 

pool-1-thread-1: A正在执行。。。
pool-1-thread-1: B正在执行。。。
pool-1-thread-1: C正在执行。。。
pool-1-thread-1: E正在执行。。。
pool-1-thread-1: D正在执行。。。

  

.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) :用给定的初始参数和默认的线程工厂及处理程序创建新的 ThreadPoolExecutor

实例6修改Main类如下): 

package com.bijian.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//创建等待队列
		BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<Runnable>(20);
		//用给定的初始参数和默认的线程工厂及处理程序创建新的 ThreadPoolExecutor
		ThreadPoolExecutor pool = new ThreadPoolExecutor(2,3,2,TimeUnit.MILLISECONDS,bqueue);
		//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
		Thread t1 = new MyThread("A");
		Thread t2 = new MyThread("B");
		Thread t3 = new MyThread("C");
		Thread t4 = new MyThread("D");
		Thread t5 = new MyThread("E");
		Thread t6 = new MyThread("F");
		Thread t7 = new MyThread("G");
		
		bqueue.add(t7);
		bqueue.add(t2);
		bqueue.add(t3);
		bqueue.add(t1);
		bqueue.add(t5);
		bqueue.add(t6);
		bqueue.add(t4);
		bqueue.add(t1);
		
		//关闭线程池
		pool.shutdown();
	}
}

 运行结果(线程池依次执行队列中的线程对象): 

pool-1-thread-1: G正在执行。。。
pool-1-thread-1: B正在执行。。。
pool-1-thread-1: C正在执行。。。
pool-1-thread-1: A正在执行。。。
pool-1-thread-1: E正在执行。。。
pool-1-thread-1: F正在执行。。。
pool-1-thread-1: D正在执行。。。
pool-1-thread-1: A正在执行。。。

 

JDK1.5自带线程池的好处:

a.       分配线程、线程使用完回收至线程池、线程池没有线程可分配等待这些工作都是自动完成的

b.       方法的操作非常简便

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值