私は非常に厄介な問題を抱えており、これを自分で修正することはできません。
スレッドを開始して終了するためのボタンが 2 つだけの単純な JavaFX シーンがあります。この場合、スレッドをうまく機能させることができないため、停止アクションは関係ありません。actionListener は次のようになります。
EDIT1:問題を理解しました。これはスレッド固有ではなく、TimeServerに対するリクエストです。ここを見てください:回答
これに回答したり、質問を読んだりした皆さんに感謝します!:)
private FarmAction action = null;
@FXML
void btnStartListener(ActionEvent event) {
Firefox ff = new Firefox();
ff.startBrowser();
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 {
run.start();
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
}
注: 私は Selenium Framework を使用して、Web サイトでのクリックを自動化しています。
私の FarmAction クラスは次のようになります。
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)
{
ac.transferFromPropToAttributes();
}
this.attackList = attackList;
}
@Override
public void run() {
ArrayList<Integer> unitIds = Constants.UNIT_ID_LIST_ALL;
if(!driver.getCurrentUrl().equals(Constants.URL_TESTPURPOSE))
driver.navigate().to(Constants.URL_TESTPURPOSE);
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 de.pool.ntp.org
{
String textfieldName = Constants.HASHMAP_TEXTFIELD_NAME_ALL_HTML_PLACE.get(unitKey);
WebElement textfield = driver.findElement(By.name(textfieldName));
textfield.sendKeys(String.valueOf(ac.getUnits().get(unitKey)));
WebElement textfieldCoordinates = driver.findElement(By.name(Constants.TEXTFIELD_NAME_HTML_PLACE_COODRINATESFORTARGET));
textfieldCoordinates.sendKeys(StringHelper.getPointAsString(new Point(ac.getXCoordinates(), ac.getYCoordinates())));
WebElement submitButton = driver.findElement(By.id(Constants.TA));
submitButton.click();
WebElement sndAttackSubmitButton = driver.findElement(By.name(Constants.SBM));
WebElement span = driver.findElement(By.className(Constants.CLASSNAME_TIME));
long duration = Long.parseLong(span.getAttribute(Constants.ATTRIBUTE_DURATION));
sndAttackSubmitButton.click();
ac.setStartTime(DateUtil.getActualServerTime());
ac.setDurationInSeconds(duration*1000);
ac.setTurnBackTime(ac.getStartTime()+ac.getDurationInSeconds());
}
}
}
}
}}
ご覧のとおり、Thread.start() メソッドを Throwables AND Exceptions の try-catch にラップしましたが、スレッドが理由もなく停止した場合、Stacktrace を取得しません。StackOverFlow エラーが発生すると予想していましたが、何もキャッチされません。
私のスレッドが動作しているループの量は非常に異なります。12 で停止することもあれば、370 または 59 で停止することもあります。すべての値は例であり、毎回異なります。
私が間違っていることについて何かアイデアはありますか?
前もって感謝します!