Keep references to threads and shut them down more properly
This commit is contained in:
@ -18,7 +18,7 @@ abstract public class ActiveGossipThread implements Runnable {
|
||||
|
||||
private GossipManager _gossipManager;
|
||||
|
||||
private AtomicBoolean _keepRunning;
|
||||
private final AtomicBoolean _keepRunning;
|
||||
|
||||
public ActiveGossipThread(GossipManager gossipManager) {
|
||||
_gossipManager = gossipManager;
|
||||
@ -36,10 +36,12 @@ abstract public class ActiveGossipThread implements Runnable {
|
||||
_keepRunning.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
_keepRunning = null;
|
||||
shutdown();
|
||||
}
|
||||
|
||||
public void shutdown(){
|
||||
_keepRunning.set(false);
|
||||
}
|
||||
/**
|
||||
* Performs the sending of the membership list, after we have
|
||||
* incremented our own heartbeat.
|
||||
|
@ -38,8 +38,10 @@ public abstract class GossipManager extends Thread implements NotificationListen
|
||||
private ExecutorService _gossipThreadExecutor;
|
||||
|
||||
private Class<? extends PassiveGossipThread> _passiveGossipThreadClass;
|
||||
private PassiveGossipThread passiveGossipThread;
|
||||
|
||||
private Class<? extends ActiveGossipThread> _activeGossipThreadClass;
|
||||
private ActiveGossipThread activeGossipThread;
|
||||
|
||||
public GossipManager(Class<? extends PassiveGossipThread> passiveGossipThreadClass,
|
||||
Class<? extends ActiveGossipThread> activeGossipThreadClass, String address, int port,
|
||||
@ -118,10 +120,10 @@ public abstract class GossipManager extends Thread implements NotificationListen
|
||||
}
|
||||
_gossipThreadExecutor = Executors.newCachedThreadPool();
|
||||
try {
|
||||
_gossipThreadExecutor.execute(_passiveGossipThreadClass.getConstructor(GossipManager.class)
|
||||
.newInstance(this));
|
||||
_gossipThreadExecutor.execute(_activeGossipThreadClass.getConstructor(GossipManager.class)
|
||||
.newInstance(this));
|
||||
passiveGossipThread = _passiveGossipThreadClass.getConstructor(GossipManager.class).newInstance(this);
|
||||
_gossipThreadExecutor.execute(passiveGossipThread);
|
||||
activeGossipThread = _activeGossipThreadClass.getConstructor(GossipManager.class).newInstance(this);
|
||||
_gossipThreadExecutor.execute(activeGossipThread);
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | NoSuchMethodException | SecurityException e1) {
|
||||
throw new RuntimeException(e1);
|
||||
@ -142,6 +144,14 @@ public abstract class GossipManager extends Thread implements NotificationListen
|
||||
*/
|
||||
public void shutdown() {
|
||||
_gossipThreadExecutor.shutdown();
|
||||
passiveGossipThread.shutdown();
|
||||
activeGossipThread.shutdown();
|
||||
try {
|
||||
boolean result = _gossipThreadExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS);
|
||||
System.err.println("Terminate retuned " + result);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
_gossipServiceRunning.set(false);
|
||||
}
|
||||
}
|
||||
|
@ -125,6 +125,11 @@ abstract public class PassiveGossipThread implements Runnable {
|
||||
_keepRunning.set(false);
|
||||
}
|
||||
}
|
||||
shutdown();
|
||||
}
|
||||
|
||||
public void shutdown(){
|
||||
_server.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,6 +18,15 @@ public class TenNodeThreeSeedTest {
|
||||
|
||||
@Test
|
||||
public void test() throws UnknownHostException, InterruptedException{
|
||||
abc();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAgain() throws UnknownHostException, InterruptedException{
|
||||
abc();
|
||||
}
|
||||
|
||||
public void abc() throws InterruptedException, UnknownHostException{
|
||||
GossipSettings settings = new GossipSettings();
|
||||
int seedNodes = 3;
|
||||
ArrayList<GossipMember> startupMembers = new ArrayList<GossipMember>();
|
||||
|
Reference in New Issue
Block a user