java线程 demo_简单的java线程池demo

本文展示了如何创建一个简单的固定大小线程池,包括仓库(BlockingQueue)、工作线程(Worker)以及线程池的初始化、任务提交和关闭方法。线程池在接收到关闭信号后会中断阻塞状态的任务。
摘要由CSDN通过智能技术生成

最近看课程,简单的写了一个线程池的demo,代码如下

package com.mrhy.threaddemo.p2;

import com.sun.corba.se.spi.orbutil.threadpool.Work;

import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.concurrent.BlockingQueue;

import java.util.concurrent.LinkedBlockingQueue;

@Log4j2

public class FixedSizeThreadPool {

//手写线程池需要准备什么?

// 1.需要1个仓库

private BlockingQueue blockingQueue;

// 2.需要一个线程的集合

private List workers;

//3. 需要具体干活的线程

public static class Worker extends Thread {

// 图上的卡车

// 1.到我们的仓库中去拿东西(blockingQueue)

//

private FixedSizeThreadPool pool;

// 创建构造方法,声明自己属于哪个线程池

public Worker(FixedSizeThreadPool pool) {

this.pool = pool;

}

@Override

public void run() {

// 开始工作

while (this.pool.isWorking || this.pool.blockingQueue.size() > 0) {

Runnable task = null;

// 从队列中拿东西的时候,需要的是阻塞

try {

if (this.pool.isWorking) {

task = this.pool.blockingQueue.take();

} else {

task = this.pool.blockingQueue.poll();

}

} catch (Exception e) {

e.printStackTrace();

}

if (task != null) {

task.run();

log.info("线程:{}执行完毕", Thread.currentThread().getName());

// System.out.println("线程:"+Thread.currentThread().getName());

}

}

}

}

// 线程池的初始化,构造函数

public FixedSizeThreadPool(int poolSize, int taskSize) {

if (poolSize <= 0 || taskSize <= 0)

throw new IllegalArgumentException("非法参数");

this.blockingQueue = new LinkedBlockingQueue<>(taskSize);

this.workers = Collections.synchronizedList(new ArrayList<>());

for (int i = 0; i < poolSize; i++) {

Worker work = new Worker(this);

work.start();

workers.add(work);

}

}

// 把任务提交到仓库中的办法

public boolean submit(Runnable task) {

if (isWorking) {

return this.blockingQueue.offer(task);

} else {

return false;

}

}

// 关闭的方法:

//a.仓库停止接收任务

//b.一旦仓库中还有任务就要继续执行

//c. 拿任务就不该阻塞

//d.一旦任务阻塞,我就中断他

private volatile boolean isWorking = true;

public void shutDown() {

// 执行关闭即可

this.isWorking = false;

for (Thread thread : workers) {

if (thread.getState().equals(Thread.State.BLOCKED)) {

thread.interrupt();// 中断线程

}

}

}

public static void main(String[] args) {

FixedSizeThreadPool fixedSizeThreadPool = new FixedSizeThreadPool(3, 6);

for (int i = 0; i < 6; i++) {

fixedSizeThreadPool.submit(

new Runnable() {

@Override

public void run() {

System.out.println("放入线程");

try {

Thread.sleep(2000L);

} catch (InterruptedException e) {

e.printStackTrace();

log.error("一个线程被中断");

}

}

}

);

}

fixedSizeThreadPool.shutDown();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值