2016-07-13 4 views
0

私は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(); 
     } 
     } 
    } 
} 

答えて

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(); 
     } 
     } 
    } 
} 
0

ランダムにお金を振り替えると、お金がなくなるアカウントが発生します。実行する時間が長いほど、ランダムにマイナスになります。

これが問題であることを防ぐ唯一の方法は、最悪の場合には常に1つのアカウントがあるため、アカウントと同じ数のスレッドを持つことです。

+1

あなたはそうです。また、スレッドが既に待機しているアカウントから金銭を移転していないという条件を追加する必要がありました。ありがとう。 –

関連する問題