Keep references to threads and shut them down more properly

This commit is contained in:
Edward Capriolo
2015-01-12 22:41:21 -05:00
parent 5072be6636
commit 6565b03192
4 changed files with 34 additions and 8 deletions

View File

@ -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.

View File

@ -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);
}
}

View File

@ -125,8 +125,13 @@ abstract public class PassiveGossipThread implements Runnable {
_keepRunning.set(false);
}
}
shutdown();
}
public void shutdown(){
_server.close();
}
/**
* Abstract method for merging the local and remote list.
* @param gossipManager The GossipManager for retrieving the local members and dead members list.

View File

@ -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>();
@ -39,6 +48,6 @@ public class TenNodeThreeSeedTest {
}
for (int i = 0; i < clusterMembers; ++i) {
clients.get(i).shutdown();
}
}
}
}