0

I am executing a program for a network where i have a certain number of tasks execution in loop, it works fine but when there a any flaws occurs due to network problem it got stuck in one of any task. so i want to create a thread which start at the time when control goes in to loop and after some delay it terminate it self with continuing the process.

for example-

for ( /*itearting condition */)
{
    //thread start with specified time.
    task1;
    task2;
    task3;
    //if any execution delay occur then wait till specified time and then
    //continue.
}

Please give me some clue regarding this, a snippets can help me a lot as i need to fix it shortly.

RTA
  • 1,251
  • 2
  • 14
  • 33

2 Answers2

1

A thread can only be terminated with its cooperation (assuming you want to save the process). With the thread's cooperation, you can terminate it with any termination mechanism it supports. Without its cooperation, it cannot be done. The usual way to do it is to design the thread to sanely handle being interrupted. Then you can have another thread interrupt it if too much time passes.

Community
  • 1
  • 1
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • I wish to create a independent thread just before executing tasks and start taking look for time repeatedly and terminate itself on specified time. – RTA Apr 11 '13 at 11:10
  • Right. Follow my answer. 1) Design the thread you want to terminate to sanely handle being interrupted. 2) Launch another thread to interrupt the thread if it takes too long. – David Schwartz Apr 11 '13 at 11:11
0

I think you may need something like this:

import java.util.Date;

public class ThreadTimeTest {

    public static void taskMethod(String taskName) {
        // Sleeps for a Random amount of time, between 0 to 10 seconds
        System.out.println("Starting Task: " + taskName);
        try {
            int random = (int)(Math.random()*10);
            System.out.println("Task Completion Time: " + random + " secs");
            Thread.sleep(random * 1000);
            System.out.println("Task Complete");
        } catch(InterruptedException ex) {
            System.out.println("Thread Interrupted due to Time out");
        }
    }

    public static void main(String[] arr) {
        for(int i = 1; i <= 10; i++) {
            String task = "Task " + i;
            final Thread mainThread = Thread.currentThread();
            Thread interruptThread = new Thread() {
                public void run() {
                    long startTime = new Date().getTime();
                    try {
                        while(!isInterrupted()) {
                            long now = new Date().getTime();
                            if(now - startTime > 5000)  {
                                //Its more than 5 secs
                                mainThread.interrupt();
                                break;
                            } else
                                Thread.sleep(1000);
                        }
                    } catch(InterruptedException ex) {}
                }
            };
            interruptThread.start();
            taskMethod(task);
            interruptThread.interrupt();
        }
    }
}
Saurabh
  • 63
  • 8