以下处理方式有什么区别InterruptedException?最好的方法是什么?
try{
//...
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
Run Code Online (Sandbox Code Playgroud)
要么
try{
//...
} catch(InterruptedException e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
编辑:我也想知道这两个使用的场景.
java multithreading exception-handling interrupted-exception
对这个重复的问题道歉,但我还没有找到任何令人满意的答案.大多数问题都有自己的特定用例:
Java -
thread.sleep的替代方法是否有更好的或替代方法可以跳过/避免在Java中使用Thread.sleep(1000)?
我的问题是非常通用的用例.等待条件完成.做一些操作.检查条件.如果条件不成立,请等待一段时间再进行相同的操作.
例如,考虑一种通过调用其createAPI表来创建DynamoDB表的方法.DynamoDB表需要一些时间才能变为活动状态,因此该方法会调用其DescribeTable API以定期轮询状态,直到某个时间(假设5分钟 - 由于线程调度而导致的偏差是可接受的).如果表在5分钟内变为活动状态,则返回true,否则抛出异常.
这是伪代码:
public void createDynamoDBTable(String name) {
//call create table API to initiate table creation
//wait for table to become active
long endTime = System.currentTimeMillis() + MAX_WAIT_TIME_FOR_TABLE_CREATE;
while(System.currentTimeMillis() < endTime) {
boolean status = //call DescribeTable API to get status;
if(status) {
//status is now true, return
return
} else {
try {
Thread.sleep(10*1000);
} catch(InterruptedException e) {
}
}
}
throw new RuntimeException("Table still not created");
}
Run Code Online (Sandbox Code Playgroud)
我理解通过使用Thread.sleep块当前线程,从而消耗资源.但是在一个相当中等规模的应用程序中,一个主题是一个大问题吗?
我在那里阅读使用 …
有没有人知道为Java提供Thread.sleep()的库,其错误不高于1-2毫秒?
我尝试了Sleep,错误测量和BusyWait的混合,但我不能在不同的Windows机器上获得这种可靠性.
如果该实现也适用于Linux和MacOS,它可以是本机实现.
编辑 Nick提供的链接(http://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks)是一个非常好的资源,可以理解java所有类型的计时器/睡眠/时钟的问题.
我有这个按钮,当点击该按钮时,将在我的文本字段中输入的详细信息保存到 Google App Engine,在调用该 onClickListener 之后,我有一个启动新活动的意图,该活动显示我刚刚输入的详细信息。这是代码:
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.userDetailsCaptureButton) {
new EndpointsTask().execute(getApplicationContext());
}
startActivity(userProfileDisplayIntent);
}
});
Run Code Online (Sandbox Code Playgroud)
现在我希望能够在调用startActivityafter之前等待几秒钟new EnpointsTask().execute(getApplicationContext)。我读到使用Thread.sleep会导致 UI 冻结,因此不适合此操作。还有哪些其他选择?
我想做一个Timer等待400 MSc然后去打印"嗨!" (例如).我知道如何做到这一点javax.swing.Timer
ActionListener action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("hi!");
}
};
Run Code Online (Sandbox Code Playgroud)
加:
timer = new Timer(0, action);
timer.setRepeats(false);
timer.setInitialDelay(400);
timer.start();
Run Code Online (Sandbox Code Playgroud)
但据我所知,这绝对不是一个好方法,因为这种方式Timer适用于Swing的作品.如何以正确的方式做到这一点?(不使用Thread.sleep())
这是我的代码
while (true) {
try {
Thread.sleep(5 * 60 * 1000);
processData();// data processing job
} catch (InterruptedException e) {
SystemMessageBillPay.getInstance().writeMessage("ERROR: CEB.run() - " + e.getMessage());
} catch (NumberFormatException e) {
SystemMessageBillPay.getInstance().writeMessage("ERROR: CEB.run() - " + e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
对此
“线程.睡眠(5 * 60 * 1000);”
代码检查员发出警告
“在循环中调用 Thread.sleep 可能会导致性能问题”
应该使用什么代码来防止出现此警告
如何在Java中实现精确计时?我知道 Thread.sleep() 不准确并导致 InterruptedExceptions。我正在尝试制作一个物理模拟器,我想要准确的代码。到目前为止,这是我的代码:
public class FreeFall() {
public static void main(String[] args) {
double height = Double.parseDouble(args[0]);
double xi = height;
int counter = 0;
while (true) {
height = xi + 9.8 / * counter * counter;
System.out.println(height);
counter++;
Thread.sleep(1000);
}
}
}
Run Code Online (Sandbox Code Playgroud)