Java线程池 – Java内置线程池

  • Post author:
  • Post category:java


一、Java内置线程池



1、 Java内置线程池 – ExecutorService


1.1 Executors.newCachedThreadPool()



性能优先模式


n个任务就创建n个线程。执行任务时,若以前的线程可用,则使用;若之前的线程正在忙,则会创建新的线程来执行任务。线程的数量不做限制;当线程空闲到一定时间(默认60秒),会自动销毁。


重载方法:指定线程的创建方式:

Executors.newCachedThreadPool(new ThreadFactory() {


int i = 1;

@Override

public Thread newThread(Runnable r) {


return new Thread(r, “自定义的线程名称:” + i++);

}

});

package com.dhu.thread.threadpool.demo02;

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

/**
 * Executors获取ExecutorService,调用方法,获取任务
 * @author zhou
 *
 */
public class MyTest01 {
	public static void main(String[] args) {
		//test1();
		test2();
	}


	static void test2() {
		//1.使用工厂类获取线程池对象,使用 new ThreadFactory()来创建线程
		ExecutorService es = Executors.newCachedThreadPool(new ThreadFactory() {
			int i = 1;
			@Override
			public Thread newThread(Runnable r) {
				return new Thread(r, "自定义的线程名称:" + i++);
			}
		});
		//2.提交执行任务
		for (int i = 0; i < 10; i++) {
			es.submit(new MyRunnable(i));
		}
	 }
	
	
	//newCachedThreadPool()
	 static void test1() {
		//1.使用工厂类获取线程池对象
		ExecutorService es = Executors.newCachedThreadPool();
		//2.提交执行任务
		for (int i = 0; i < 10; i++) {
			es.submit(new MyRunnable(i));
		}
	 }
}

/**
 * 任务类
 * @author zhou
 *
 */
class MyRunnable implements Runnable {
	private int id;
	
	
	public MyRunnable(int id) {
		this.id = id;
	}


	@Override
	public void run() {
		String name = Thread.currentThread().getName();
		System.out.println(name + "执行了任务:" + id);
		
	}
	
}


1.2 Executors.newFixedThreadPool(n)



固定线程数,降低服务器压力


1.3 Executors.newSingleThreadExecutor()



单线程的线程池,安全性



2、Java内置线程池 – ScheduledExecutorService



延迟执行任务



2.1 Executors.newScheduledThreadPool(3)



es.schedule();

//2.创建、提交任务,任务延迟2秒执行

//任务再线程池中执行


es.scheduleAtFixedRate(new MyRunnabel(1), 1, 2, TimeUnit.SECONDS);

package com.dhu.thread.threadpool.demo03;

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

/**
 * 测试ScheduledExecutorService接口 延迟执行任务和重复执行任务的功能
 * @author zhou
 *
 */
public class ScheduledExecutorServiceTest01 {
	public static void main(String[] args) {
		//1.获取一个具备延迟执行任务的线程池对象
		ScheduledExecutorService es = Executors.newScheduledThreadPool(3, new ThreadFactory() {
			int i = 1;
			@Override
			public Thread newThread(Runnable r) {
				
				return new Thread(r, "自定义线程名称:" + i++);
			}
		});
		//2.创建、提交任务,任务延迟2秒执行
		es.scheduleAtFixedRate(new MyRunnabel(1), 1, 2, TimeUnit.SECONDS);
		System.out.println("over");
	}
	
}

class MyRunnabel implements Runnable{
	private int id;
	
	public MyRunnabel(int id) {
		this.id = id;
	}

	@Override
	public void run() {
		String name = Thread.currentThread().getName();
		System.out.println(name +"执行任务" + id);
	}
	
}

//2.创建、提交任务,任务延迟2秒执行

//任务再线程池中执行


es.scheduleWithFixedDelay(new MyRunnabe2(3), 1, 2, TimeUnit.SECONDS);



版权声明:本文为zhouth94原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。