J2Me线程池

本文介绍了一种适用于手机平台的简易线程池设计方案,通过合理管理线程资源以提高应用程序性能。文中详细解释了线程池的工作原理及关键代码实现。

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

鉴于手机平台的特殊性,而且项目中多处用到线程,为便于线程管理,节约资源,提高运行速度,创建了线程池,对线程统一进行管理。
用到的原则:
当调用线程池的addTask(TaskThread task,String name)时,如果此时线程池中的线程有空闲的,从freeQueue中调用一个空闲的线程进行处理;如果线程池中没有空闲线程,且线程总量没达到最大,则批量创建新的线程,将其加到freeQueue中,然后从中取用一个执行任务;如果此时线程池中的线程已达到最大容量,且缓冲队列blockingQueue未满,将其加到缓冲队列中,等待执行。如果缓冲队列blockingQueue已满,抛出异常。此处应该采取异常处理机制,尚未处理。
以下是线程池类的主要代码:
/**
* @建立简单的线程池,实现线程的初步管理
* @2009.11.22.14.37
*/
public class ThreadPool
{
private String classname = this.getClass().getName();
// 优先级低
public static final int PRIORITY_LOW = 0;

// 普通
public static final int PRIORITY_NORMAL = 1;

// 高
public static final int PRIORITY_HIGH = 2;
/**
* 线程池中最少的线程数
*/
private static final int corePoolSize = 3;
/**
* 线程池中线程的最大数目
*/
private static final int produceTaskMaxNum = 15;
private static final int increment = 3;
/**
* 线程池所使用的缓冲队列
*/
private Stack blockingQueue;
/**
* 线程池所使用的空闲队列
*/
private Stack freeQueue;
/**
* 线程池所使用的工作队列
*/
private Hashtable workQueue;

/**
* 线程池所中目前所有的线程
*/
private Hashtable allQueue;
/**
* 缓冲队列的容量
*/
private static final int tasknum = 15;

private static ThreadPool instance = null;
private int _threadCount = 0;
private ThreadPool()
{
blockingQueue = new Stack();
freeQueue = new Stack();
workQueue = new Hashtable();
allQueue = new Hashtable();
//先创建最小线程数的线程
for (int i = 0; i < corePoolSize; i++) {
TaskThread t = new TaskThread();
t.set_pool(this);
freeQueue.push(t);
allQueue.put(t.getName(),t);
_threadCount++;
}
}

/**
* @return
* 得到线程池的实例
*/
public synchronized static ThreadPool getInstance()
{
if(instance == null)
{
instance = new ThreadPool();
}
return instance;
}




/**
* @param task 要执行的任务
* @param name 任务的名字
* 添加并执行新的任务
*/
public void addTask(Runnable task,String name)
{
if (!freeQueue.empty())
{
TaskThread tt= (TaskThread) freeQueue.pop();
System.out.println("*****"+classname+"tt:tt.getKey():"+tt.getKey()+"****"+tt.getName()+"******"+tt.getTarget());
freeQueue.push(tt);
}
System.out.println("***添加执行新的任务:"+name);
int len = _threadCount;
System.out.println("***目前的线程数:"+len);
boolean isStart = false;
TaskThread t = null;
//如果线程没有到达最大值
if (len<produceTaskMaxNum)
{
//如果空闲列表非空
if (freeQueue.size()>0)
{
System.out.println("***线程空闲队列中有闲置线程");
//从空闲队列pop一个线程
t = (TaskThread) freeQueue.pop();
t.setKey(name);
t.setTarget(task);
t.set_pool(this);
if (t.get_shutdown()) {
isStart = true;
//打开开关
System.out.println("*********是已回收线程,打开开关****************");
synchronized (t) {
t.open();
}
}
//加入工作字典
workQueue.put(t.key,t);
if (!isStart) {
System.out.println("***第一次,要start()");
t.start();
}
}
else
{
//无空闲线程,创建新的线程
incrementThread(task,name);
}
}
else
{
//如果线程达到max就把任务加入等待队列
if (blockingQueue.size()<tasknum) {
t= new TaskThread();
t.setKey(name);
t.setTarget(task);
t.set_pool(this);
blockingQueue.push(task);
}
else
{
try {
throw new Exception("******缓冲队列已满,线程池已达到极限!");
} catch (Exception e) {
e.printStackTrace();
}

}

}
}

/**
* @param task
* @param name
* 当空闲线程用完时,调用此函数,创建新的线程。
*/
public void incrementThread(Runnable task,String name)
{
TaskThread t = null;
int len = getThreadsCount();
//如果没有空闲队列了,就根据增量创建线程
for (int i = 0; i < increment; i++) {
//判断线程的总量不能超过max
if ((len+i)<produceTaskMaxNum) {
t = new TaskThread();
t.set_pool(this);
_threadCount++;
//加入线程字典
allQueue.put(t.getName(),t);
freeQueue.push(t);
}
else
{
try
{//采取异常处理策略。未处理**
throw new Exception("*******线程数量已达到最大!");
}
catch (Exception e)
{
e.printStackTrace();
}
break;
}
}
//从空闲队列提出出来设置后开始执行
t = (TaskThread) freeQueue.pop();
//填充任务线程
t.setTarget(task);
t.setKey(name);
//加入工作字典
workQueue.put(t.key,t);
t.start();
}

/**
* @param task
* 线程执行完毕触发的事件
*/
public void complete(TaskThread task)
{
System.out.println("***开始回收线程"+task.getName());
if (workQueue.containsKey(task.key)) {
//首先因为工作执行完了,所以从正在工作字典里删除
workQueue.remove(task.key);
}
else {
System.out.println("***没有将task.key加入");
}
task.shutDown();
//从等待任务队列提取一个任务
if (blockingQueue.size()>0) {
System.out.println("***从缓冲队列中取任务执行");
TaskThread t = (TaskThread) blockingQueue.pop();
TaskThread nt = null;
//如果有空闲的线程,就是用空闲的线程来处理
if (freeQueue.size()>0) {
System.out.println("***从空闲队列中取线程");
nt = (TaskThread) freeQueue.pop();
nt.setKey(t.getKey());
nt.setTarget(t.getTarget());
//添加到工作字典中
workQueue.put(nt.key,nt);
if (!nt.get_shutdown()) {
//线程尚未运行过,许调用start()启动。
nt.start();
}
else
{
synchronized (nt)
{//线程已经运行过,再次运行,不需调用start()方法
nt.open();
}
}
}
else
{
//无空闲线程,创建新的线程
incrementThread(task.getTarget(),task.getName());
}
}
else
{
//如果没有等待执行的操作就回收多余的工作线程
if (freeQueue.size()>corePoolSize-1) {
//当空闲线程超过最小线程数就回收多余的
//线程自然执行完毕,出入死亡状态
allQueue.remove(task.getName());
task.shutDown();
_threadCount--;
System.out.println("***空闲线程池已满,线程"+task.getName()+"销毁"+"目前的线程数:"+_threadCount);

}
else
{
try
{//让该线程处于等待状态,不让其死去
//如果没超过就把线程从工作字典放入空闲队
System.out.println("***把线程"+task.getName()+"放入空闲队");
freeQueue.push(task);
System.out.println("***调用"+task+".wait()让线程"+task.getName()+"处于等待状态freeQueue.size():"+freeQueue.size());
System.out.println("回收线程"+task.getName()+"结束");
task.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

public int getThreadsCount()
{
return this._threadCount;
}

public Hashtable getWorkQueue() {
return workQueue;
}

public void setWorkQueue(Hashtable workQueue) {
this.workQueue = workQueue;
}

}

任务执行类:TaskThread
package com.pojaa.util;

/**
* @author yuandawei
* @类描述:执行线程池传来的任务
*/
public class TaskThread extends Thread{

private TaskThread instance;
/**
* 标志该线程
*/
String key = null;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}

/**
* 线程池
*/
private ThreadPool _pool;
public ThreadPool get_pool() {
return _pool;
}
public void set_pool(ThreadPool _pool) {
this._pool = _pool;
}

/**
* 需要执行的目标对象
*/
private Runnable _target;
/**
* 开关
*/
private boolean _shutdown = false;
public boolean get_shutdown() {
return _shutdown;
}
public void set_shutdown(boolean _shutdown) {
this._shutdown = _shutdown;
}

private boolean _idle = false;
public TaskThread(){
instance = this;
//super();
}
/**
* @param target
*/
public TaskThread(Runnable target)
{
super(target);
}

public TaskThread(Runnable target,String name)
{
super(target,name);
}

public TaskThread(Runnable target, String name, ThreadPool pool)
{
super(name);
this._pool = pool;
this._target = target;
}

public TaskThread(String name)
{
super(name);
this.key = name;
}

public Runnable getTarget()
{
return this._target;
}
public boolean isIdle()
{
return this._idle;
}

public synchronized void shutDown() {
this._shutdown = true;
notifyAll();
}

public synchronized void setTarget(Runnable target) {
this._target = target;
//notifyAll();
}

public void run()
{
System.out.println("***是否执行 标记:"+_shutdown);
while (!this._shutdown) {
this._idle = false;
if (this._target != null) {
System.out.println("****任务开始执行"+this.getClass().getName()+this.getKey());
this._target.run();
System.out.println("****任务结束执行"+this.getClass().getName()+this.getKey());
}
this._idle = true;
synchronized (this)
{
if (this.get_pool()!= null) {
System.out.println(this+"._pool.complete(this)开始"+this.getClass().getName()+this.getKey());
this._pool.complete(this);
System.out.println(this+"._pool.complete(this)结束"+this.getClass().getName()+this.getKey());
this._idle = false;
}
else {
System.out.println("**********this.getKey()"+this.getKey()+"*******"+"this.get_pool()"+this.get_pool());
}
}
}
}

public void open()
{
this._shutdown = false;
notifyAll();
}

}
内容概要:本文介绍了基于Python实现的SSA-GRU(麻雀搜索算法优化门控循环单元)时间序列预测项目。项目旨在通过结合SSA的全局搜索能力和GRU的时序信息处理能力,提升时间序列预测的精度和效率。文中详细描述了项目的背景、目标、挑战及解决方案,涵盖了从数据预处理到模型训练、优化及评估的全流程。SSA用于优化GRU的超参数,如隐藏层单元数、学习率等,以解决传统方法难以捕捉复杂非线性关系的问题。项目还提供了具体的代码示例,包括GRU模型的定义、训练和验证过程,以及SSA的种群初始化、迭代更新策略和适应度评估函数。; 适合人群:具备一定编程基础,特别是对时间序列预测和深度学习有一定了解的研究人员和技术开发者。; 使用场景及目标:①提高时间序列预测的精度和效率,适用于金融市场分析、气象预报、工业设备故障诊断等领域;②解决传统方法难以捕捉复杂非线性关系的问题;③通过自动化参数优化,减少人工干预,提升模型开发效率;④增强模型在不同数据集和未知环境中的泛化能力。; 阅读建议:由于项目涉及深度学习和智能优化算法的结合,建议读者在阅读过程中结合代码示例进行实践,理解SSA和GRU的工作原理及其在时间序列预测中的具体应用。同时,关注数据预处理、模型训练和优化的每个步骤,以确保对整个流程有全面的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值