user3573426 user3573426 - 1 year ago 96
Java Question

Java Thread stops without Exception/Throwable

I've a very annoying problem and I'm not able to fix this by myself.

I have a simple JavaFX Scene with just 2 buttons for start and killing the thread. The stop-action is not relevant in this case because I'm not able to get the thread working well. The actionListener looks like:

EDIT1: Figured out the problem, it's not thread specific, it's the request against the TimeServer, take a look here: answer

Thanks to everyone of you who answered this or read the question! :)

private FarmAction action = null;

void btnStartListener(ActionEvent event) {
Firefox ff = new Firefox();

BrowserAction browserAction = new BrowserAction(ff.getDriver(), ff.getJse());
browserAction.loginToGame(user, pass);
ObservableList<AttackCommand> list = (ObservableList<AttackCommand>) Serializer.deserialize(Constants.FILE_NAME_TESTPURPOSE);

if(action == null)
action = new FarmAction(ff.getDriver(), ff.getJse(), list);

Thread run = new Thread(action);
try {
} catch (Exception e) {
} catch (Throwable t) {

Note: I'm working with the Selenium Framework to automate some clicks on a

My FarmAction class looks like this:

public class FarmAction implements Runnable{

private WebDriver driver;
private JavascriptExecutor jse;
private ObservableList<AttackCommand> attackList;
private boolean stop = false;

public FarmAction(WebDriver driver, JavascriptExecutor jse, ObservableList<AttackCommand> attackList) {
this.driver = driver;
this.jse = jse;
for(AttackCommand ac : attackList)
this.attackList = attackList;

public void run() {
ArrayList<Integer> unitIds = Constants.UNIT_ID_LIST_ALL;
int count = 0;
while(true) { // Just let the Thread run for duration = lifetime of application
System.out.println(count++); //count how often the thread runs into this while loop before terminating for no reason
for(AttackCommand ac : attackList) {
for(int i = 0; i < unitIds.size(); i ++) {
int unitKey = unitIds.get(i); // Momentane UnitID der Iteration
if(ac.getUnits().containsKey(unitKey) && ( ac.getTurnBackTime() == 0 ||ac.getTurnBackTime() <= DateUtil.getActualServerTime())) // getActualServerTime is request against the timeserver
String textfieldName = Constants.HASHMAP_TEXTFIELD_NAME_ALL_HTML_PLACE.get(unitKey);
WebElement textfield = driver.findElement(;

WebElement textfieldCoordinates = driver.findElement(;
textfieldCoordinates.sendKeys(StringHelper.getPointAsString(new Point(ac.getXCoordinates(), ac.getYCoordinates())));

WebElement submitButton = driver.findElement(;;

WebElement sndAttackSubmitButton = driver.findElement(;

WebElement span = driver.findElement(By.className(Constants.CLASSNAME_TIME));
long duration = Long.parseLong(span.getAttribute(Constants.ATTRIBUTE_DURATION));;

As you see I've wrapped the Thread.start() method into a try-catch for Throwables AND Exceptions but I don't get any Stacktrace if the thread stops for no reason. I expected an StackOverFlow Error but I don't catch anything.

The amount of loops my thread is working is very different. Sometimes it stops after 12, then other times after 370 or 59. All values are examples - it's different each time.

Do you have any Ideas what I'm doing wrong?

Thanks in advance!

Answer Source

OK guys, I've figuered the problem out. It's not thread specific, sorry for that, but I've learned almost a bit more about threads because of your answers :) Thanks to everyone of you!

I've put SystemOuts behind every single codeline and figured out after 25 runs and freezes there are two possible codelines where thread stops working:

Both statements use my DateUtil.getActualServerTime() Method which looks as follows: ​

public static long getActualServerTime() {
    String TIME_SERVER = "";   
    NTPUDPClient timeClient = new NTPUDPClient();
    InetAddress inetAddress;
    long returnTime = 0;

    try {
        inetAddress = InetAddress.getByName(TIME_SERVER);
        TimeInfo timeInfo = timeClient.getTime(inetAddress);
        returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
    } catch (Exception e) {
    } catch (Throwable t) {

    return returnTime;

I'm using the lib for that. If I'm just return a Date d = new Date(); return d.getTime(); my thread works fine for about 60.000 loops until I've stopped it manually.

I don't get an Exception or Throwable here as well. I'd say theres a maximum amount of request against the time server in a specific time or something like that. I can't figure it out on the Website right now.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download