gpt4 book ai didi

java - 完全退出 Java 方法

转载 作者:行者123 更新时间:2023-12-02 05:08:04 28 4
gpt4 key购买 nike

我的方法 checkConnection()TimeLimiter 内调用 setPort(),如果该方法不存在,它会在 3 秒内退出调用方法完成的。这非常有效,并且在超出时间限制时会出现 com.google.common.util.concurrent.UncheckedTimeoutException 异常。然而,即使抛出此异常后,setPort 仍然运行,并且一旦完成,打开端口的 try 语句中的代码就会运行,但是一旦到达 Thread.sleep(100) 就会出现 InterruptedException被抛出,然后该方法退出。然而,这给我留下了一个开放的端口,这会导致问题。有没有办法一旦超过时间限制,call()方法中的所有代码都会停止?

 public static String checkConnection(final String comPort) {

final String port = comPort;
String result = null;

TimeLimiter limiter = new SimpleTimeLimiter();
try {

result = limiter.callWithTimeout(new Callable<String>() {

public String call() {

// Try to set serial port
String setPort = setPort(comPort);

// Check for any exceptions
if(setPort.contains("jssc.SerialPortException")) {

if(setPort.contains("Port busy")) {

return "Error: The port appears to be busy";

} else {

return "Error: Can't connect to port";

}

}

try {

// Port can't be accessed twice at the same time
synchronized(portLock) {

// Open port if not already opened
if(!serialPort.isOpened())
serialPort.openPort();

// Sleep while response is being sent
Thread.sleep(300);

// Check for response
buffer = serialPort.readBytes(6);//Read 6 bytes from serial port

serialPort.closePort();

}

// Parse response as string
response = new String(buffer);

} catch (SerialPortException | InterruptedException e) {

System.out.println("Serial:: ping() :: Exception while pinging Noteu : " + e);

return "Error";

}

return response;

}
}, 3, TimeUnit.SECONDS, false);

} catch (Exception e) {

System.out.println("Serial:: checkConnection() :: Exception while calling ping : " + e);

}

}



public static String setPort(String port) {

synchronized(portLock) {

try {

System.out.println("Serial:: setPort() :: Opening Port...");

serialPort = new SerialPort(port);

if(!serialPort.isOpened())
serialPort.openPort();

System.out.println("Serial:: setPort() :: Setting Params...");

serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
System.out.println("Setting Port3");

serialPort.closePort();

System.out.println("Serial:: setPort() :: Port Set...");

return "Success";

} catch (SerialPortException e) {

System.out.println("Serial:: setPort() :: Exception at set Port : " + e.toString());

return e.toString();

}

}

}

最佳答案

你可以尝试一下这是否有效这是

的签名
callWithTimeout(Callable<T> callable,
long timeoutDuration,
TimeUnit timeoutUnit,
boolean amInterruptible)

........
amInterruptible - whether to respond to thread interruption by aborting the operation and throwing InterruptedException; if false, the operation is allowed to complete or time out, and the current thread's interrupt status is re-asserted.

您将 amInterruptible 传递为 false。您可以尝试传递 true 并看看它是否有效。而且我强烈感觉 setPort 必须是可中断的。为此,正如用户 Scadge 所评论的,您需要提供其实现。无论如何,只是希望这个快速解决方案可能适合您

关于java - 完全退出 Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27591822/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com