TimerTask+;多线程&x2B;java,不适用于第二次执行

TimerTask+;多线程&x2B;java,不适用于第二次执行,java,multithreading,timertask,Java,Multithreading,Timertask,我正在从一个timertask创建多个线程,对于timertask的第一次执行,一切都正常。但当第二次执行timertask时,Thread.start()并没有调用run()方法。我已经尝试了我在互联网上遇到的每一个选择,但没有任何效果。谁能帮帮我吗!!!:( 这是我安排timertask的方式: Timer timer = new Timer(); timer.scheduleAtFixedRate(new orderProcessScheduler(), getDate(), interv

我正在从一个timertask创建多个线程,对于timertask的第一次执行,一切都正常。但当第二次执行timertask时,Thread.start()并没有调用run()方法。我已经尝试了我在互联网上遇到的每一个选择,但没有任何效果。谁能帮帮我吗!!!:(

这是我安排timertask的方式:

Timer timer = new Timer();
timer.scheduleAtFixedRate(new orderProcessScheduler(), getDate(), interval_period);
这是时间任务:

public class orderProcessScheduler extends TimerTask{

public void processOrders()
{
    try
    {

        int totalThreads = 10;
        List<orderThreadImpl> threadPool = new ArrayList<orderThreadImpl>();

        for(int i = 0;i<totalThreads;i++)
        {
            threadPool.add(new orderThreadImpl());
        }

    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
}

@Override
public void run() {
    // TODO Auto-generated method stub
    processOrders();
}
}

下面是您应该做的,使用executor服务线程池来管理您的线程,并为您启动每个线程:

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TimerTaskQuestion {

    public static void main(String[] args) {
        OrderProcessScheduler orderScheduler = new OrderProcessScheduler();
        Timer timer = new Timer();
        timer.schedule(orderScheduler, 500, 1000);
    }

    public static class OrderProcessScheduler extends TimerTask {

        private ExecutorService ex;

        public OrderProcessScheduler() {
            this.ex = Executors.newFixedThreadPool(10);
            try {
                this.ex.awaitTermination(1, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            System.out.println("Number of active thread : " + ((ThreadPoolExecutor)this.ex).getActiveCount());
            this.ex.execute(new orderThreadImpl());
        }

        public void initiateShutdown(){
            this.ex.shutdown();
        }
    }

    public static class orderThreadImpl implements Runnable {

        @Override
        public void run() {
            try {
                System.out.println("Executed from : " + Thread.currentThread().getName());
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

你应该考虑在一个构造函数中使用线程线程执行器不做代码>新线程(this).Stand()代码,它可能允许新线程在部分初始化或未初始化状态下看到<代码>这个< /Cord>对象。谷歌“泄漏”这个词。有关详细信息,您的变量
threadPool
有一个欺骗性名称:欺骗性,因为如果您不重复使用线程,它就不是池。计时器任务每次运行时都会创建所有新线程。re,当第二次执行timertask时,Thread.start()不会调用run()你怎么知道?你期望什么样的行为?你会得到什么?考虑创建一个SSCCE并把它放在这里。谢谢@ XVOLKS为你的帮助。下面的班诺特的回答也提出了相同的API,并且工作了:
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TimerTaskQuestion {

    public static void main(String[] args) {
        OrderProcessScheduler orderScheduler = new OrderProcessScheduler();
        Timer timer = new Timer();
        timer.schedule(orderScheduler, 500, 1000);
    }

    public static class OrderProcessScheduler extends TimerTask {

        private ExecutorService ex;

        public OrderProcessScheduler() {
            this.ex = Executors.newFixedThreadPool(10);
            try {
                this.ex.awaitTermination(1, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            System.out.println("Number of active thread : " + ((ThreadPoolExecutor)this.ex).getActiveCount());
            this.ex.execute(new orderThreadImpl());
        }

        public void initiateShutdown(){
            this.ex.shutdown();
        }
    }

    public static class orderThreadImpl implements Runnable {

        @Override
        public void run() {
            try {
                System.out.println("Executed from : " + Thread.currentThread().getName());
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}