Java线程工具类(基于Thread)

博客围绕Java线程展开,虽未给出具体内容,但可知核心聚焦于Java线程这一信息技术领域的重要内容。Java线程在后端开发中应用广泛,可实现多任务并发处理等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//方法实现
public class App {
	public static void main(String[] args) throws InterruptedException, ExecutionException {

		Threads threadUtils = new Threads.ThreadBuilder().setThreadGroup("PAD").build();

		threadUtils.setOperator(new Threads.operateThread() {

			@Override
			public void operatorBefore(Thread t) {
				System.out.println(
						"Excutor Before-------------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.");
			}

			@Override
			public void operatorAfter(Thread t) {
				System.out.println(
						"Excutor After-------------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.");
			}

		});

		Runnable run = new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 10; i++) {
					System.out.println("----------Currtent Thread------" + Thread.currentThread());
				}
			}
		};

		for (int i = 0; i < 10; i++) {
			threadUtils.excute(run);
		}

	}
}

//自己封装的工具类
class Threads {

	private operateThread operator;

	private ThreadGroup threadGroup;

	public Threads() {
	}

	public synchronized final ThreadGroup getThreadGroup() {
		return threadGroup;
	}

	public synchronized final void setThreadGroup(ThreadGroup threadGroup) {
		this.threadGroup = threadGroup;
	}

	public static class ThreadBuilder {
		private ThreadGroup Group;

		public ThreadBuilder setThreadGroup(String groupID) {
			Group = new ThreadGroup(groupID);
			return this;
		}

		public Threads build() {
			Threads threads = new Threads();
			if (Group != null) {
				threads.setThreadGroup(Group);
			}
			return threads;
		}
	}

	@SuppressWarnings("unchecked")
	public <T, K> FutureTask<K> excute(T task) {

		FutureTask<K> futureTask = null;

		checkThreadGroup();
		checkOperator();

		Thread thread = null;
		if (task instanceof Runnable) {

			thread = new Thread(threadGroup, (Runnable) task);

			operator.operatorBefore(thread);
			thread.start();
			operator.operatorAfter(thread);

		} else if (task instanceof Callable<?>) {

			futureTask = new FutureTask<K>((Callable<K>) task);
			thread = new Thread(threadGroup, futureTask);

			operator.operatorBefore(thread);
			thread.start();
			operator.operatorAfter(thread);

		}

		return futureTask;
	}

	public static interface operateThread {
		public void operatorBefore(Thread t);

		public void operatorAfter(Thread t);
	}

	private void checkOperator() {
		if (operator == null) {
			operator = new operateThread() {
				@Override
				public void operatorBefore(Thread t) {
				}

				@Override
				public void operatorAfter(Thread t) {
				}

			};
		}
	}

	private void checkThreadGroup() {
		if (threadGroup == null) {
			threadGroup = new ThreadGroup("defaultGroup");
		}
	}

	public void setOperator(operateThread operator) {
		this.operator = operator;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值