我使用了Timer定时器 来定时执行任务,现在的问题是 我只做到了 定时执行单个任务,想问一下 如何同时执行多个任务,以下是我的代码 请指点一下 谢谢。
/**
 * 定时器
 * */
public class TimeThread extends Thread {  
    public void run() {  
    
      Timer time = new Timer();  
      TimerTask tst = new TimerTask();//定时执行Rptlog类中的run方法
          time.schedule(tst, 1000,10000);//1秒后执行,每10秒执行一次    
    }  }接下来是实际执行的类
public class TimerSendTask extends  java.util.TimerTask{


public void run() {
TimerTask tt = new TimerTask();
for(int i=0;i<service.getList().size();i++){//循环查询任务
              定时清理任务 实现代码段...
....
}
  
}
}
上面代码中的 service.getList().size(); 是获取所有任务列表 开始执行,但我希望通过查询所有的任务 然后给每个任务单独的空间(或者说是线程)去执行 最好可以在我的代码基础上修改一下.谢谢 对并发任务处理不太了解.

解决方案 »

  1.   

    你再写多个任务的类  
    在这个地方执行以下就好了
    TimerTask tst = new TimerTask();//定时执行Rptlog类中的run方法
    time.schedule(tst, 1000,10000);//1秒后执行,每10秒执行一次  
      

  2.   

    并发任务有可能是10个,也有可能是15个,并且执行的类代码较多,直接在NEW十几个任务类会影响性能,不太可取.首先不能确定到底有多少个任务会同时执行,只能根据service.getList().size();的方法查询出有多少个任务,关于service.getList()方法是定时去数据库中查询的,所以要做定时器.
      

  3.   

    建议你考虑下 Spring的quartz
      

  4.   

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(int corePoolSize)
    executor.shedule...
    Timer就不要去用了,会有诸多问题的,尤其是定时执行多个任务
      

  5.   

    项目既然已经使用了Timer现在在去修改放弃使用 恐怕复杂程度远远大于Timer本身需要解决的问题
      

  6.   

    定时任务都是固定操作。你把多个方法写在一起。再调用不就可以了。。
    public void aa()
    {
              方法1
              方法2
              //
    }