Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to secure methods in java (overflow and so on)

Tags:

java

watchdog

i have to write a "WatchDog" in Java, who secure that Threads don't perform too long. With the initialization of the Objects it's no Problem, i made a Class, who calls the WatchDog and the constructor with reflections in the run() method.

A Thread is easy to stop, but how i can secure normal methods of objects? For example i call the method of an Object and this method perform a endless loop, how you would do that?

thanks

like image 324
Nicolas Avatar asked Aug 09 '11 11:08

Nicolas


2 Answers

First, I should point out that stopping a thread is NOT easy. In fact, in the general case, threads cannot be stopped safely:

  • You can call Thread.interrupt() on a thread the you want to stop, but there is no guarantee that the thread will notice the interrupt, let alone actually stop.

  • You can call the deprecated Thread.stop() method, but this method is unsafe. If you call it at an unfortunate moment, you can leave data structures in a half-updated state, leave other threads waiting (for ever) on signals that won't arrive and so on.


Here's how I'd implement a watchdog for method execution.

First I'd modify the method to add two calls to the watchdog service; e.g.

public void someMethod(...) {
    Watchdog.startMethod(maxTime);
    // do stuff
    Watchdog.endMethod();
}

Next, I'd implement the Watchdog with a priority queue ordered on expiry time:

  1. The startMethod(maxTime) would add an entry to the queue with expiry time of now + maxTime. The entry would include a reference to the current thread (when the method was called.
  2. The endMethod() would look for a (the) queue entry for the current thread, and remove it if found.
  3. The watchdog thread would periodically look at the first queue entry. If that entry had an expiry less than 'now', the watchdog would remove the entry, stop its thread and check the next entry. Repeat until the next entry hasn't expired.

Some thought would need to be given to the data structures, and to dealing with cases where endMethod calls get skipped. (Indeed, since a method call can terminate due to an exception, the endMethod() call really needs to be done in a finally block.)

Note that the startMethod and endMethod calls could (and maybe should) be inserted by an annotation processor or something like that.


Given the complexity, and the fact that you can't guarantee to stop the thread (safely), I'd think of some solution that doesn't involve a method watchdog.

like image 193
Stephen C Avatar answered Sep 28 '22 08:09

Stephen C


Normal methods of objects are running on some thread. It might be the AWT event dispatcher or whatever it's called. Or it might be the main thread, say, of a console application.

They are no different to the threads that are called with new Thread().

I guess your watchdog needs to be looking at all the threads in the VM and looking for ones which have a utilisation >= some threshold.

What code do you have so far?

Rich

like image 42
4 revs, 4 users 79% Avatar answered Sep 28 '22 06:09

4 revs, 4 users 79%