java vector 池_java中通用的线程池

packagecom.smart.frame.task.autoTask;importjava.util.Collection;importjava.util.Vector;/*** 任务分发器*/

public class TaskManage extendsThread

{protected Vector tasks = new Vector();protected boolean running = false;protected boolean stopped = false;protected boolean paused = false;protected boolean killed = false;privateThreadPool pool;publicTaskManage(ThreadPool pool)

{this.pool =pool;

}public voidputTask(Runnable task)

{

tasks.add(task);

}public voidputTasks(Runnable[] tasks)

{for (int i = 0; i < tasks.length; i++)this.tasks.add(tasks[i]);

}public void putTasks(Collectiontasks)

{this.tasks.addAll(tasks);

}protectedRunnable popTask()

{if (tasks.size() > 0) return (Runnable) tasks.remove(0);else return null;

}public booleanisRunning()

{returnrunning;

}public voidstopTasks()

{

stopped= true;

}public voidstopTasksSync()

{

stopTasks();while(isRunning())

{try{

sleep(5);

}catch(InterruptedException e)

{

TaskException.getResultMessage(e);

}

}

}public voidpauseTasks()

{

paused= true;

}public voidpauseTasksSync()

{

pauseTasks();while(isRunning())

{try{

sleep(5);

}catch(InterruptedException e)

{

TaskException.getResultMessage(e);

}

}

}public voidkill()

{if (!running) interrupt();else killed = true;

}public voidkillSync()

{

kill();while(isAlive())

{try{

sleep(5);

}catch(InterruptedException e)

{

TaskException.getResultMessage(e);

}

}

}public synchronized voidstartTasks()

{

running= true;this.notify();

}public synchronized voidrun()

{try{while (true)

{if (!running || tasks.size() == 0)

{

pool.notifyForIdleThread();this.wait();

}else{

Runnable task;while ((task = popTask()) != null)

{

task.run();if(stopped)

{

stopped= false;if (tasks.size() > 0)

{

tasks.clear();

System.out.println(Thread.currentThread().getId()+ ": Tasks are stopped");break;

}

}if(paused)

{

paused= false;if (tasks.size() > 0)

{

System.out.println(Thread.currentThread().getId()+ ": Tasks are paused");break;

}

}

}

running= false;

}if(killed)

{

killed= false;break;

}

}

}catch(InterruptedException e)

{

TaskException.getResultMessage(e);return;

}

}

}

VectorJava的一种动态数组,它可以根据需要自动增长和缩小。Vector的使用非常广泛,下面详细介绍一下它的使用方法。 1. 创建Vector对象 创建Vector对象的方式有两种: - 使用无参构造函数创建一个空的Vector对象: ```java Vector vector = new Vector(); ``` - 使用带参构造函数创建指定大小的Vector对象: ```java Vector vector = new Vector(int initialCapacity); ``` 2. 添加和获取元素 向Vector添加元素的方式有两种: - 使用add()方法添加元素到Vector的末尾: ```java vector.add(element); ``` - 使用insertElementAt()方法在指定位置插入元素: ```java vector.insertElementAt(element, index); ``` 获取Vector的元素也有两种方式: - 使用get()方法获取指定位置的元素: ```java vector.get(index); ``` - 使用elementAt()方法获取指定位置的元素: ```java vector.elementAt(index); ``` 3. 修改和删除元素 修改Vector的元素使用set()方法: ```java vector.set(index, element); ``` 删除Vector的元素使用remove()方法: ```java vector.remove(index); ``` 4. 判断Vector是否为空 使用isEmpty()方法来判断Vector是否为空: ```java vector.isEmpty(); ``` 5. 获取Vector的大小 使用size()方法获取Vector的大小: ```java vector.size(); ``` 6. 遍历Vector 使用for循环遍历Vector的元素: ```java for (int i = 0; i < vector.size(); i++) { Object element = vector.get(i); // do something with element } ``` 使用迭代器Iterator遍历Vector的元素: ```java Iterator iterator = vector.iterator(); while (iterator.hasNext()) { Object element = iterator.next(); // do something with element } ``` 以上就是Vector的使用方法,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值