简体   繁体   中英

TimerTask + multiThreading + java, not working for second execution

I am creating multiple threads from a timertask, and everything is working fine for first execution of timertask. But when timertask is executed for second time,Thread.start() is not invoking run() method. I have tried every option I came across on internet,but nothing works. Can anyone please help me !!! :(

This is how I schedule timertask:

Timer timer = new Timer();
timer.scheduleAtFixedRate(new orderProcessScheduler(), getDate(), interval_period);

Here's the timerTask:

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();
}
}

Here's thread implementation:

public class orderThreadImpl implements Runnable{

private Thread t;


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

    try
    {

        // code for what this thread is suppose to do
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

public orderThreadImpl()
{
    this.t = new Thread(this);
    t.start();
}

Here is what you should do, use an executor service thread pool to manage your threads, and start each thread for you :

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();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM