- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应用程序,它的主要方法生成一百个线程(假设我们模拟一百个帐户)。我正在试验它,我希望它在被 Control-C 中断时只打印终止。
我读到您可以使用 ShutDownHooks 来做到这一点,所以我在我的主要方法中添加了以下内容:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Terminating");
}
});
但是,当我运行它时,什么也没有打印出来。
您能否就我出错的地方提供一些指导(所有线程都在 for 循环中声明并从调用其 start 方法开始)?
问候,乔治
编辑:请参阅下面的代码:
银行类别:
public class Bank {
private final double[] accounts;
public Bank(int n, double initialBalance) {
accounts = new double[n];
for (int i=0; i < accounts.length;i++) {
accounts[i] = initialBalance;
}
}
public double getTotalBalance() {
double sum = 0.0;
for (int i=0; i < accounts.length; i++) {
sum += accounts[i];
}
return sum;
}
public synchronized void transfer(int fa, int ta, double amt) throws InterruptedException{
System.out.print(Thread.currentThread());
if (accounts[fa] < amt){
wait();
}
accounts[ta] -= amt;
System.out.println("Transfer of amount: " + amt + " from: " + fa + " Transfer to: " + ta);
accounts[fa] += amt;
System.out.println("Total Balance: " + getTotalBalance());
notifyAll();
}
public int size() {
return accounts.length;
}
public double[] getAccounts(){
return accounts;
}
}
银行测试类:
public class BankTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Bank b = new Bank(100,1000);
int i;
long timeStart = System.currentTimeMillis();
long j = System.currentTimeMillis();
for (i=0; i < b.size(); i++) {
TransferRunnable tr = new TransferRunnable(b, i, 1000,j);
Thread t = new Thread(tr);
t.start();
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Terminating");
}
});
}
}
TransferRunnable 类:
public class TransferRunnable implements Runnable {
private Bank b;
private int fromAccount;
private double maxAmount;
private final int DELAY = 40;
private long timeStart;
public TransferRunnable(Bank b, int from, double max, long timems) {
this.b = b;
fromAccount = from;
maxAmount = max;
timeStart = timems;
}
@Override
public void run() {
try {
while (true) {
int ta = (int) (b.size() * Math.random());
double amount = maxAmount * Math.random();
double[] acc = b.getAccounts();
b.transfer(fromAccount,ta,amount);
Thread.sleep((int) (DELAY*Math.random()));
}
}
catch (InterruptedException e) {
}
}
}
最佳答案
当我运行它时,它会被打印出来。您可以添加System.out.flush();
不过,到 run() 方法的末尾,这可以确保立即打印输出。
关于java - 多线程应用程序中的 ShutDownHook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8220194/
据我所知,System.exit 会触发关闭钩子(Hook)(如果注册了任何钩子(Hook)),因此关闭钩子(Hook)线程正在等待任何其他线程加入如果其他线程调用 System.exit() 我认为
我有一个应用程序,它的主要方法生成一百个线程(假设我们模拟一百个帐户)。我正在试验它,我希望它在被 Control-C 中断时只打印终止。 我读到您可以使用 ShutDownHooks 来做到这一点,
我发现要显示 JOptionPane 消息对话框,我需要 shutdownHooks 许可。它有什么作用?向第三方代码授予此权限可能存在哪些风险? 最佳答案 关闭 Hook 是虚拟机退出前(所有非守护
我遇到了这个异常: org.quartz.SchedulerException: SchedulerPlugin class not specified for plugin 'shutdownhoo
我需要从 ShutdownHook 调用该方法的非静态方法 removeLocksOnExit()。 该方法在 main 所在的同一个类中声明。 主要我有以下代码: //final Thread ma
假设 shutdownHook 为: class ShutdownHolder extends Thread { public void run() { Logger logg
我有一个 shutdownhook,它在进程终止时执行。然而,程序对 h2 数据库所做的更改不是持久的。只有在提交后让关闭线程等待一段时间,我才能在再次启动时看到数据库中的更改。 Runtime.ge
如果我有一个 Java 程序可以由于各种原因退出,例如: 因为设置为“关闭时退出”的主窗口已关闭 因为代码中有一些System.exit(0) 因为根本没有更多的窗口(并且没有设置为在关闭时退出)但是
这是我的主要方法,它包含一个 shutdownhook: public static void main(String args[]) { Runtime.getRuntime().addSh
我正在使用带有常规配置的 logback 1.1.10。我最近发现了shutdownHook preference但我还没有找到使用 groovy 配置启用它的方法。这可能吗?还是缺失的功能? 最佳答
有什么办法可以在关闭 Hook 中显示 joptionpanei.s 我需要在我的 shutdownhook 事件中显示一个确认对话框 最佳答案 如果有,它不会帮助你。 关闭 Hook 作为 JVM
我有以下 ShutdownHook 来检测应用程序何时退出: Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
我试图提供一个非常简单的“你想保存这个文档吗?”关闭我的应用程序时的对话框。 我使用了 WindowAdapter 来做到这一点。如果使用任何 GUI 操作关闭应用程序(单击 CLOSE 按钮、从任务
我们如何在 Web 应用程序中注册Shutdown hook? 在 web.xml 或 applicationContext.xml 中注册它有什么好处吗? 我知道如果我们使用带有主类的应用程序很简单
我正在使用 logback 中的元素; jmxConfigurator 声明如下: Thus, unless your application is a standalone Java applic
拥有 Spring Boot 应用程序,在调用 Shutdown hook @Predestroy 方法时,会调用应用程序关闭。但在打印日志后,控制台和文件、sys out 行中均不打印日志。看起来,
在下面的代码片段中,永远不会调用 finalize() 方法。但是当我在 addShutdownHook 中将 finalize() 替换为 close() 时。 finalize() 方法将被调用。
我正在 Tomcat 中使用 Jax-ws 发布 Web 服务 @WebService() public class ChatService ...followed by a constructor
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: When Shutdown Hooks Break Bad 想知道在JVM崩溃的场景下是否保证shutdown
我有一些 Java 代码来创建一个关闭钩子(Hook),以便在客户端按下 ctrl+C 时干净地退出: private static void shutdownHandler(Thread mainT
我是一名优秀的程序员,十分优秀!