1

我有一个应用程序,它在其主要方法中产生了一百个线程(假设我们模拟了一百个帐户)。我正在试验它,我希望它在被 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) {

        }

    }

}
4

2 回答 2

0

当我运行它时它会被打印出来。不过,您可以添加System.out.flush();到 run() 方法的末尾,这样可以确保立即打印输出。

于 2011-11-22T00:03:28.343 回答
0

正如其他人所说,这应该可行。您使用的是什么操作系统?这可能是CTRL+C完全杀死进程而不是要求它关闭(例如SIGKILL vs SIGINT)。您能否验证您正在向 Java 进程发送哪个信号?

最后,作为最后的手段,您可以尝试以下 Java:

if (FileDescriptor.out.valid()) {
    FileDescriptor.out.sync();
}

我怀疑这不会有任何区别!

于 2011-11-22T00:20:53.440 回答