Java线程无异常停止/可丢弃
我有一个非常恼人的问题,我不能自己解决这个问题 我有一个简单的JavaFX场景,只有两个按钮用于启动和终止线程。停止操作在本例中不相关,因为我无法使线程正常工作。actionListener看起来像: EDIT1:解决了问题,它不是线程特定的,是针对时间服务器的请求,请看这里:Java线程无异常停止/可丢弃,java,multithreading,exception,selenium,throwable,Java,Multithreading,Exception,Selenium,Throwable,我有一个非常恼人的问题,我不能自己解决这个问题 我有一个简单的JavaFX场景,只有两个按钮用于启动和终止线程。停止操作在本例中不相关,因为我无法使线程正常工作。actionListener看起来像: EDIT1:解决了问题,它不是线程特定的,是针对时间服务器的请求,请看这里: 感谢所有回答此问题或阅读此问题的人!:) private FarmAction action=null; @FXML void btnStartListener(ActionEvent事件){ Firefox ff=新F
感谢所有回答此问题或阅读此问题的人!:)
private FarmAction action=null;
@FXML
void btnStartListener(ActionEvent事件){
Firefox ff=新Firefox();
ff.startBrowser();
BrowserAction BrowserAction=新的BrowserAction(ff.getDriver(),ff.getJse());
browserAction.loginToGame(用户,通行证);
ObservableList=(ObservableList)序列化程序。反序列化(Constants.FILE\u NAME\u TESTPURPOSE);
if(action==null)
action=newfarmaction(ff.getDriver(),ff.getJse(),list);
线程运行=新线程(操作);
试一试{
run.start();
}捕获(例外e){
e、 printStackTrace();
}捕获(可丢弃的t){
t、 printStackTrace();
}
}
注意:我正在使用Selenium框架来自动单击
网站
我的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 run = new Thread(action);
run.setUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
}
}
);
run.start();
public类FarmAction实现可运行{
私有网络驱动程序;
私有JavascriptExecutor jse;
私人观察者攻击列表;
私有布尔停止=false;
公共FarmAction(WebDriver驱动程序、JavascriptExecutor jse、ObservableList攻击列表){
this.driver=driver;
this.jse=jse;
用于(AttackCommand ac:attackList)
{
ac.从Prop转移到Attributes();
}
this.attackList=attackList;
}
@凌驾
公开募捐{
ArrayList unitIds=Constants.UNIT\u ID\u LIST\u ALL;
如果(!driver.getCurrentUrl().equals(Constants.URL\u TESTPURPOSE))
driver.navigate().to(Constants.URL\u TESTPURPOSE);
整数计数=0;
while(true){//只需让线程在应用程序的持续时间=生命周期内运行即可
System.out.println(count++);//计算线程在无故终止之前运行到此while循环的频率
对于(AttackCommand ac:attackList){
对于(int i=0;i 如果(ac.getUnits().containsKey(unitKey)和&(ac.getTurnBackTime()==0 | | ac.getTurnBackTime(),则需要检查线程类的run()
方法内部是否存在异常
调用start()
方法后,创建的线程有自己的堆栈,这与调用start()
方法的主线程堆栈不同
因此,线程的run()
方法中抛出的异常对于与start()
方法相关的catch
语句将不可见。正如@Immibis在注释中提到的,捕获thread.start()
异常没有意义,因为它将立即返回
检查异常的一种方法是使用UncaughtExceptionHandler
,如下所示:
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 run = new Thread(action);
run.setUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
}
}
);
run.start();
好了,伙计们,我已经解决了这个问题。这不是特定于线程的,很抱歉,但由于你们的回答,我对线程了解了更多:)谢谢大家
我将SystemOuts放在每一个代码行后面,并在25次运行和冻结后发现有两个可能的代码行线程停止工作:
这两条语句都使用myDateUtil.getActualServerTime()
方法,如下所示:
public static long getActualServerTime() {
String TIME_SERVER = "de.pool.ntp.org";
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) {
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
return returnTime;
}
我正在使用apache.commons.net库来实现这一点。
如果我只是返回一个
dated=newdate();返回d.getTime();
在手动停止之前,我的线程可以正常运行大约60000个循环
我在这里也没有异常或可丢弃。我想说,在特定时间或类似的时间内,对时间服务器的请求量是最大的。我现在无法在网站上找到它。我也有同样的问题,我的线程在没有任何异常/可丢弃的情况下被终止
我有一个接受套接字的线程(nio,但是阻塞模式)。这很好,如果套接字以较慢的速度接收请求,但是当一次接收大量请求时,线程停止
我在linux中更改了一些选项,比如mem[net.ipv4.tcp\u rmem,net.core.wmem\u default,net.ipv4.tcp\u mem],可能是为了实现这一点?想象一下,如果这样做有效……那么start
将无法返回,直到线程完成(以便知道没有异常)这将破坏使用线程的意义!您能检查您创建的线程是否是守护进程线程吗?我猜(尚未验证)事件调度线程实际上是守护进程线程,这导致您创建的线程也是守护进程。您的应用程序只是优雅地结束,守护进程线程被强制终止,因此没有特殊异常,也没有可预测的线程数loops@immibis你确定吗?使用线程的要点是,生成一个单独的线程来执行t提供的Runnable
start()
的run()away@AdrianShum嗯,start()
不可能在异常返回后抛出异常。(我说的是一种假设情况,如果run
通过抛出一个异常终止,那么该异常将传播到start
的调用方)@immibis oops!这里有你的观点:)你完全正确:)谢谢你的回答!我也将整个while循环包装在一个try-catch块之间,但仍然没有异常,没有可丢弃的。如果FarmAction使用try-catch while循环扩展线程本身,也没有异常或可丢弃的。试着隔离问题。例如,将print语句放在每个com之前