changed thread-kill logic

This commit is contained in:
Arndt Brenschede 2015-10-23 19:18:37 +02:00
parent 850e8049fb
commit 723cf90dd2
2 changed files with 44 additions and 4 deletions

View file

@ -158,7 +158,7 @@ public class RoutingEngine extends Thread
OsmTrack[] lastTracks = new OsmTrack[nsections]; OsmTrack[] lastTracks = new OsmTrack[nsections];
OsmTrack track = null; OsmTrack track = null;
ArrayList<String> messageList = new ArrayList<String>(); ArrayList<String> messageList = new ArrayList<String>();
for( int i=0; !terminated; i++ ) for( int i=0;; i++ )
{ {
track = findTrack( refTracks, lastTracks ); track = findTrack( refTracks, lastTracks );
track.message = "track-length = " + track.distance + " filtered ascend = " + track.ascend track.message = "track-length = " + track.distance + " filtered ascend = " + track.ascend
@ -516,6 +516,8 @@ public class RoutingEngine extends Thread
} }
catch( IllegalArgumentException iae ) catch( IllegalArgumentException iae )
{ {
if ( terminated ) throw iae;
// fast partial recalcs: if that timed out, but we had a match, // fast partial recalcs: if that timed out, but we had a match,
// build the concatenation from the partial and the nearby track // build the concatenation from the partial and the nearby track
if ( matchPath != null ) if ( matchPath != null )
@ -530,7 +532,7 @@ public class RoutingEngine extends Thread
if ( track == null ) if ( track == null )
{ {
for( int cfi = 0; cfi < airDistanceCostFactors.length && !terminated; cfi++ ) for( int cfi = 0; cfi < airDistanceCostFactors.length; cfi++ )
{ {
airDistanceCostFactor = airDistanceCostFactors[cfi]; airDistanceCostFactor = airDistanceCostFactors[cfi];
@ -775,8 +777,13 @@ public class RoutingEngine extends Thread
addToOpenset( startPath1 ); addToOpenset( startPath1 );
addToOpenset( startPath2 ); addToOpenset( startPath2 );
} }
while(!terminated) for(;;)
{ {
if ( terminated )
{
throw new IllegalArgumentException( "operation killed by thread-priority-watchdog after " + ( System.currentTimeMillis() - startTime)/1000 + " seconds" );
}
if ( maxRunningTime > 0 ) if ( maxRunningTime > 0 )
{ {
long timeout = ( matchPath == null && fastPartialRecalc ) ? maxRunningTime/3 : maxRunningTime; long timeout = ( matchPath == null && fastPartialRecalc ) ? maxRunningTime/3 : maxRunningTime;

View file

@ -8,10 +8,15 @@ import java.io.OutputStreamWriter;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import btools.router.OsmNodeNamed; import btools.router.OsmNodeNamed;
import btools.router.OsmTrack; import btools.router.OsmTrack;
@ -29,6 +34,7 @@ public class RouteServer extends Thread
private Socket clientSocket = null; private Socket clientSocket = null;
private RoutingEngine cr = null; private RoutingEngine cr = null;
private volatile boolean terminated;
public void stopRouter() public void stopRouter()
{ {
@ -36,6 +42,16 @@ public class RouteServer extends Thread
if ( e != null ) e.terminate(); if ( e != null ) e.terminate();
} }
private static DateFormat tsFormat = new SimpleDateFormat( "dd.MM.yy HH:mm", new Locale( "en", "US" ) );
private static String formattedTimestamp()
{
synchronized( tsFormat )
{
return tsFormat.format( new Date( System.currentTimeMillis() ) );
}
}
public void run() public void run()
{ {
BufferedReader br = null; BufferedReader br = null;
@ -53,7 +69,7 @@ public class RouteServer extends Thread
} }
InetAddress ip = clientSocket.getInetAddress(); InetAddress ip = clientSocket.getInetAddress();
System.out.println( "ip=" + (ip==null ? "null" : ip.toString() ) + " -> " + getline ); System.out.println( formattedTimestamp() + " ip=" + (ip==null ? "null" : ip.toString() ) + " -> " + getline );
String url = getline.split(" ")[1]; String url = getline.split(" ")[1];
HashMap<String,String> params = getUrlParams(url); HashMap<String,String> params = getUrlParams(url);
@ -133,6 +149,7 @@ public class RouteServer extends Thread
if ( br != null ) try { br.close(); } catch( Exception e ) {} if ( br != null ) try { br.close(); } catch( Exception e ) {}
if ( bw != null ) try { bw.close(); } catch( Exception e ) {} if ( bw != null ) try { bw.close(); } catch( Exception e ) {}
if ( clientSocket != null ) try { clientSocket.close(); } catch( Exception e ) {} if ( clientSocket != null ) try { clientSocket.close(); } catch( Exception e ) {}
terminated = true;
} }
} }
@ -165,6 +182,22 @@ public class RouteServer extends Thread
server.serviceContext = serviceContext; server.serviceContext = serviceContext;
server.clientSocket = clientSocket; server.clientSocket = clientSocket;
// cleanup thread list
for(;;)
{
boolean removedItem = false;
for (Map.Entry<Long,RouteServer> e : threadMap.entrySet())
{
if ( e.getValue().terminated )
{
threadMap.remove( e.getKey() );
removedItem = true;
break;
}
}
if ( !removedItem ) break;
}
// kill thread if limit reached // kill thread if limit reached
if ( threadMap.size() >= maxthreads ) if ( threadMap.size() >= maxthreads )
{ {