
public class ThreadPoolManager {
private static ThreadPoolManager instance = null;
private List<Upload> taskQueue = CollectionssynchronizedList(new LinkedList<Upload>());//任务队列
private WorkThread[] workQueue ; //工作线程(真正执行任务的线程)
private static int worker_num = 6; //工作线程数量(默认工作线程数量是6)
private static int worker_count = 0;
private ThreadPoolManager(){
this(6);
}
private ThreadPoolManager(int num){
worker_num = num;
workQueue = new WorkThread[worker_num];
for(int i=0;i<worker_num;i++){
workQueue[i] = new WorkThread(i);
}
}
public static synchronized ThreadPoolManager getInstance(){
if(instance==null)
instance = new ThreadPoolManager();
return instance;
}
public void addTask(Upload task){
//对任务队列的 *** 作要上锁
synchronized (taskQueue) {
if(task!=null){
taskQueueadd(task);
taskQueuenotifyAll();
Systemoutprintln("task id "+taskgetInfo() + " submit!");
}
}
}
public void BatchAddTask(Upload[] tasks){
//对任务队列的修改 *** 作要上锁
synchronized (taskQueue) {
for(Upload e:tasks){
if(e!=null){
taskQueueadd(e);
taskQueuenotifyAll();
Systemoutprintln("task id "+egetInfo() + " submit!");
}
}
}
}
public void destory(){
Systemoutprintln("pool begins to destory ");
for(int i = 0;i<worker_num;i++){
workQueue[i]stopThread();
workQueue[i] = null;
}
//对任务队列的 *** 作要上锁
synchronized (taskQueue) {
taskQueueclear();
}
Systemoutprintln("pool ends to destory ");
}
private class WorkThread extends Thread{
private int taksId ;
private boolean isRuning = true;
private boolean isWaiting = false;
public WorkThread(int taskId){
thistaksId= taskId;
thisstart();
}
public boolean isWaiting(){
return isWaiting;
}
// 如果任务进行中时,不能立刻终止线程,需要等待任务完成之后检测到isRuning为false的时候,退出run()的方法
public void stopThread(){
isRuning = false;
}
@Override
public void run() {
while(isRuning){
Upload temp = null;
//对任务队列的 *** 作要上锁
synchronized (taskQueue) {
//任务队列为空,等待新的任务加入
while(isRuning&&taskQueueisEmpty()){
try {
taskQueuewait(20);
} catch (InterruptedException e) {
Systemoutprintln("InterruptedException occre");
eprintStackTrace();
}
}
if(isRuning)
temp = taskQueueremove(0);
}
//当等待新任务加入时候,终止线程(调用stopThread函数)造成 temp = null
if(temp!=null){
Systemoutprintln("task info: "+tempgetInfo()+ " is begining");
isWaiting = false;
tempuploadPic();
isWaiting = true;
Systemoutprintln("task info: "+tempgetInfo()+ " is finished");
}
}
}
}
}
复制代码
然后定义任务接口(Task):这里我定义的是上传的功能接口(这里用抽象类或者接口随你自己)
Upload
然后定义具体任务类:我这里简单,让它睡眠2秒钟。当然你也可以定义很多实现Upload的任务类。
TaskUpload
测试这个简单的线程池:
public class ThreadPoolManagerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Upload[] tasks = createBatchTask(7);
ThreadPoolManager pool = ThreadPoolManagergetInstance();
poolBatchAddTask(tasks);
pooldestory();
}
private static Upload[] createBatchTask(int n){
Upload[] tasks = new TaskUpload[n];
for(int i = 0;i<n ;i++ ){
tasks[i] = new TaskUpload("我的工作线程 "+ i);
}
return tasks;
}
}
计时器,每分钟或每秒执行一次,获取当前系统时间,判断是 630 的话,响。。
quartz 作业jar包。设定执行作业时间,设定执行内容,然后运行。
其他作业实现。
后台。
经查询相关资料显示java同一订单前台有结算页提供用户去结算,后台在收到前台用户点击去结算的 *** 作时,就会开始处理下单服务,因此是后台。
Java是一种计算机编程语言,拥有跨平台,面向对象,泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发,任职于太阳微系统的詹姆斯·高斯林等人于1990年代初开发Java语言的雏形,最初被命名为Oak,目标设置在家用电器等小型系统的程序语言,应用在电视机,电话,闹钟,烤面包机等家用电器的控制和通信。
public void run() {
while(i<=0&&i>=-60)
{
try {
i--;
Threadsleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
可以这样写i可以设置为你相要让他停止的时间,由于这是一个runnable接口所以你可以在其他的类中实例化这个类。让他在后台运行
以上就是关于求一个 java 多线程闹钟的例子,就是可以同时设置多个不同时段的闹钟全部的内容,包括:求一个 java 多线程闹钟的例子,就是可以同时设置多个不同时段的闹钟、用Java写一个闹钟 每天早上6点30响 怎么做、java同一订单提示谁正在处理等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)