如何更改最大等待线程?

时间:2016-07-13 17:58:44

标签: java multithreading

我遇到了同步线程的问题,其中我使用了wait()方法。它工作正常,但片刻之后程序冻结了。我检查了处于等待状态的线程数,程序在达到10个等待线程后总是冻结。我在哪里可以更改最大等待线程数,以便程序在任何时候都不会冻结?

public class ThreadTest2 {
    private final static int size = 100;

    public static void main(String[] args) throws InterruptedException {

        Account bank = new Account(size,1000);
        for(int i=0; i<10; i++){
            Transfer t1 = new Transfer(bank, size);
            Thread bankTransfer = new Thread(t1);
            bankTransfer.start();
        }
    }

}

class Account{

    public static int u = 0;
    double[] account;
    int size;
    public Account(int size, double initValue){
        account = new double[size];
        this.size = size;
        for(int i=0; i<size; i++){
            account[i] = initValue;
        }
    }

    public synchronized double getTotalBalance(){
        double total = 0;
        for(int i=0;i<size;i++)
            total+=account[i];
        return total;
    }

    public int getSize(){
        return size;
    }

    public double getBalance(int i){
        return account[i];
    }


    public synchronized void transfer(int from, int to, double amount) throws InterruptedException{

        int i = 0;
        if(account[from]<0){ i++; u++;}
            System.out.printf("Account no %d balance: %.2f subtracts %.2f adds to %d\n", from, getBalance(from),amount,to);
            while(account[from]<0)
                wait();
            if(i==1) u--;
            account[from]-=amount;
            System.out.println(" ");
            account[to]+=amount;
            System.out.printf("Total balance: %.2f thread awaiting: %d\n",getTotalBalance(), u);    

            notifyAll();

        }

}

class Transfer implements Runnable{
    Account bank;
    int size;
    public Transfer(Account bank, int size){
        this.bank=bank;
        this.size = size;
    }

    public void run(){
        while(true){
        try {
            bank.transfer((int)(size * Math.random()), (int)(size * Math.random()), 999*Math.random());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

当你随意转账时,你的账户就会碰到钱。运行的时间越长,帐户随机变为负数。

防止这种问题的唯一方法是拥有与帐户一样多的线程,因为在最坏的情况下,总会有一个帐户有钱。

答案 1 :(得分:0)

正确使用以下代码(感谢@ peter-lawrey)。

public class ThreadTest2 {
    private final static int size = 10;

    public static void main(String[] args) throws InterruptedException {

        Account bank = new Account(size,1000);
        for(int i=0; i<10; i++){
            Transfer t1 = new Transfer(bank, size);
            Thread bankTransfer = new Thread(t1);
            bankTransfer.start();
        }
    }

}

class Account{

    public static int u = 0;
    double[] account;
    int size;
    boolean[] waiting;
    public Account(int size, double initValue){
        account = new double[size];
        this.size = size;
        for(int i=0; i<size; i++){
            account[i] = initValue;
        }
        waiting = new boolean[size];
        for(int i=0;i<size;i++)
            waiting[i] = false;
    }

    public synchronized double getTotalBalance(){
        double total = 0;
        for(int i=0;i<size;i++)
            total+=account[i];
        return total;
    }

    public int getSize(){
        return size;
    }

    public double getBalance(int i){
        return account[i];
    }


    public synchronized void transfer(int from, int to, double amount) throws InterruptedException{
        if(from!=to && waiting[from]!=true){
        int i = 0;
        System.out.printf("Account no %d balance: %.2f subtracts %.2f adds to %d\n", from, getBalance(from),amount,to);
        if(account[from]-amount<0){ i++; u++;}
            while(account[from]-amount<0){
                waiting[from] = true;
                wait();

            }

            if(i==1){ u--; waiting[from] = false;}
            account[from]-=amount;
            System.out.println(" ");
            account[to]+=amount;
            System.out.printf("Total balance: %.2f thread awaiting: %d\n",getTotalBalance(), u);    

            notifyAll();
        }
        }

}

class Transfer implements Runnable{
    Account bank;
    int size;
    public Transfer(Account bank, int size){
        this.bank=bank;
        this.size = size;
    }

    public void run(){
        while(true){
        try {
            bank.transfer((int)(size * Math.random()), (int)(size * Math.random()), 999*Math.random());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }
    }
}