Blocking action monitoring

I am trying to write a thread that keeps track of how long a blocking action takes. For example, I have a blocking action like this:

class BlockingThread extends Thread{ public volatile boolean success = false; public volatile long startedTime = 0; public void run(){ startedTime = System.currentTimeMillis(); success = doBlockingAction(); //no idea how long this action will take } } 

and I want to have another Thread that basically calls the "timeout" function if the lock action takes too long:

 class MonitorThread extends Thread{ public void run(){ while(System.currentTimeMillis() - blockingThread.startedTime > TIMEOUT_DURATION) { ..keep waiting until the time runs out.. } if(!blockingThread.success){ listener.timeout(); //Took too long. } } } 

I'm having trouble understanding how BlockingThread is actually currently in a blocking action while I measure time in MonitorThread.

If I do something like this,

 Thread blockingThread = new BlockingThread(); blockingThread.start(); Thread monitorThread = new MonitorThread(); monitorThread.start(); 

there is no guarantee that one of the threads actually runs the code in front of the other, and therefore I do not currently know if my time stream really measures the time of the blocking action. I assume the answer is related to locking and wait ing, but I cannot figure it out.

+4
source share
3 answers

I can advise you to rewrite the code using the java.util.concurrent.ThreadPoolExecutor class. This class has a nice awaitTermination() method, which I suppose is what you need.

EDIT:

Here is your BlockingThread running 10 seconds and a monitor waiting 5 seconds:

 package sample.threadexecutor; import java.text.SimpleDateFormat; import java.util.Date; public class BlockingThread implements Runnable{ public boolean succsess = false; @Override public void run() { SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.SSS"); System.out.println(df.format(new Date()) + " start"); try { Thread.sleep(10000L); } catch (InterruptedException e) { System.out.println(df.format(new Date()) + " interrupted"); succsess = false; return; } System.out.println(df.format(new Date()) + " end"); succsess = true; } } 

and the main function with the performer:

 package sample.threadexecutor; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class main { public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.SSS"); ExecutorService service= Executors.newSingleThreadExecutor(); service.submit(new BlockingThread()); try { service.shutdown(); if(service.awaitTermination(5, TimeUnit.SECONDS)){ System.out.println(df.format(new Date()) + " execution completed"); }else{ System.out.println(df.format(new Date()) + " execution timeout"); } } catch (InterruptedException e) { System.out.println(df.format(new Date()) + " monitoring interrupted"); } } } 

and conclusion:

 22:28:37.005 start 22:28:42.006 execution timeout 22:28:47.006 end 

if we set the timeout to 20 seconds:

 22:30:20.210 start 22:30:30.213 end 22:30:30.214 execution completed 
+4
source

You may need only a few cycles before Thread.startingTime initialization is blocked.

 class MonitorThread extends Thread{ public void run(){ boolean weWantToKeepRunning = true; // here is your new event loop while(weWantToKeepRunning){ if (blockingThread.startedTime != 0) && (System.currentTimeMillis() - blockingThread.startedTime > TIMEOUT_DURATION)&& (!blockingThread.success){ listener.timeout(); // too long weWantToKeepRunning = false; // quit this loop } } } } 
0
source

what does doBlockingAction do, exactly? Most blocking actions in the Java API have options that allow you to set timeouts. Using these timeout options is the most accurate way.

0
source

All Articles