Note that there are some explanatory texts on larger screens.

plurals
  1. POInterrupt thread after specified time - problems
    primarykey
    data
    text
    <p>I am developing an application where every <strong>TimeMax_Sec</strong> seconds a complex function (optimisation) is run. The function can potentially enter in a loop or just to take too much time, and the time <strong>TimeMax_Sec</strong> is set by the user.</p> <p>Therefore, I'm trying to run the function in a separate thread. To execute constantly the function, I use the command <strong>ScheduledExecutorService.scheduleAtFixedRate</strong>.</p> <p>Before the new thread is started, I have to be sure that the existing thread is not active any more and has released all the resources used. For this, inside the thread, I compare the time elapsed from the start of the thread with <strong>TimeMax_Sec</strong>, and if the it is greater, the thread is interrupted.</p> <p>Now, I have two problems:</p> <ul> <li>the code to call the threads looks too much complex and redundant and I'm wondering if it is possible to make it more elegant and clear. </li> </ul> <p>The code is like this: </p> <pre><code>public class Main { public static void main(String[] args) { Runnable threadShell = new Runnable() { public void run() { Thread thread_Object = new rtr.cicle_thread(... args ...); try { thread_Object.start(); Thread.currentThread().sleep( timeMax_Sec*1000 ); if ( thread_Object.isAlive() ) { config_Obj_Final.stopThread = true; System.out.println("Thread forced to stop") thread_Object.join(); } } catch (InterruptedException e) { flagException(); } } }; ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); executor.scheduleAtFixedRate(ThreadShell, 0, timeMax_Sec, TimeUnit.SECONDS); } } </code></pre> <ul> <li>more important, but maybe linked to the previous, the thread doesn't stops in time, and I have the impression that the time seen by the thread is slower than the actual time. </li> </ul> <p>An example of the code of the function in the thread is:</p> <pre><code>public class cicle_thread extends Thread{ private ... args_thread ...; public cicle_thread(... args ...){ args_thread = args; } public void run(){ Calendar threadStartTime = Calendar.getInstance(); Calendar threadCurrentTime = Calendar.getInstance(); long loopFrequencyMilliseconds = timeMax_Sec*1000; long threadLifeInMilliseconds = 0; threadCurrentTime = Calendar.getInstance(); threadLifeInMilliseconds = threadCurrentTime.getTimeInMillis()-threadStartTime.getTimeInMillis(); if(threadLifeInMilliseconds&gt;loopFrequencyMilliseconds){ if(Thread.currentThread().isInterrupted()){ interrupt(); return; } } function_1(); threadCurrentTime = Calendar.getInstance(); threadLifeInMilliseconds = threadCurrentTime.getTimeInMillis()-threadStartTime.getTimeInMillis(); if(threadLifeInMilliseconds&gt;loopFrequencyMilliseconds){ if(Thread.currentThread().isInterrupted()){ interrupt(); return; } } function_2(); ... threadCurrentTime = Calendar.getInstance(); threadLifeInMilliseconds = threadCurrentTime.getTimeInMillis()-threadStartTime.getTimeInMillis(); if(threadLifeInMilliseconds&gt;loopFrequencyMilliseconds){ if(Thread.currentThread().isInterrupted()){ interrupt(); return; } } function_n(); } </code></pre> <p>but printing the time elapsed <strong>ThreadLifeInMilliseconds</strong> and the current time I have this result:</p> <pre><code>Thread_1_Start 17:12:20 Function_1 ThreadLifeInMilliseconds = 0 Function_2 ThreadLifeInMilliseconds = 31 Function_3 ThreadLifeInMilliseconds = 624 Function_4 ThreadLifeInMilliseconds = 1357 threads forced to stop Thread_2_Start 17:12:28 Function_1 ThreadLifeInMilliseconds = 0 Function_2 ThreadLifeInMilliseconds = 47 Function_3 ThreadLifeInMilliseconds = 624 Function_4 ThreadLifeInMilliseconds = 1263 threads forced to stop Thread_3_Start 17:12:35 Function_1 ThreadLifeInMilliseconds = 0 Function_2 ThreadLifeInMilliseconds = 16 Function_3 ThreadLifeInMilliseconds = 577 Function_4 ThreadLifeInMilliseconds = 1014 threads forced to stop </code></pre> <p>in the first case for example, the second thread is started after 8 seconds, but <strong>ThreadLifeInMilliseconds</strong> at Function_4 is only 1357 milliseconds. Note that <strong>TimeMax_Sec</strong> was set to 3 seconds (3000 milliseconds), therefore when the intermediate thread wake up from its sleep, it find the child thread still alive and write "threads forced to stop".</p> <p>Any idea?</p> <p>Thanks</p> <p>pardon, it looks like that the formatting of the code doesn't work too well...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload