2012-12-19 31 views
3

這裏是我創建管理倉庫的庫存基本應用。基本上五個線程或IT公司每個產生100個小部件,然後將其存儲在倉庫中。這工作得很好,但偶爾也越過倉庫上限是500所以我要五個獨立的公司能夠生產每100個零件,並把它們存儲在倉庫中,並在500個部件停止。但目前它有時但並非總是超過極限。所以如果我要運行它三次,它將工作2/3,它會繼續添加endles的部件到倉庫。所以我的問題是如何解決這個問題?螺紋庫存控制

下面是代碼

public class mainClass { 

public static void main(String[] args) { 

    warehouse acct1 = new warehouse(0); // create warehouse with nothing in it 
    System.out.print("Reciving widgets..."); 
    acct1.checkBal(); 
    manufacturer t1 = new manufacturer(acct1, "Calcutta"); // create 5 threads (manufacturers) 
    manufacturer t2 = new manufacturer(acct1, "New York"); 
    manufacturer t3 = new manufacturer(acct1, "Chicargo"); 
    manufacturer t4 = new manufacturer(acct1, "Liverpool"); 
    manufacturer t5 = new manufacturer(acct1, "Tokyo"); 

    t1.start();     
    t2.start(); 
    t3.start();     
    t4.start(); 
    t5.start();     


} 

}

import java.util.*; 
    public class manufacturer extends Thread { 
    warehouse myAcct;    //class 'warehouse' assigned to variable MyAcct 
    String name;   
     int time; 
     Random r = new Random();  // imported from java.util this can be used to create a random amount of time 
     int amount = 100;   // This variable is the manufacturing goal of each individual manufacture (thread)` 



    public manufacturer(warehouse acct, String x) { 
     myAcct = acct; 
     name = x; // name of the thread 
     time = r.nextInt(4000); // This creates the random time of anywhere between 0 and 9999 

    } 
    public void run() { 
     while (true) {    // run forever 
      try { 
       sleep (time);  // Create new widgets 
      } catch (InterruptedException e) { } 
       // 100 by each manufacturer 
       try{ 
        Thread.sleep(time); 
        System.out.printf("%s has successfully manufactured %d widgets \n", name, amount); 

        //how long do u want to sleep for? 
        //System.out.printf("%s is done\n", name); 


        myAcct.adjustBal(100); System.out.println("widgets have been stored at the central warehouse"); 
        System.out.println(); 
        Thread.sleep(time); 
       }catch(Exception e){} 

        if (myAcct.getBal() == 500) 
        { 
         System.out.println("The target goal of 500 widgets have been created and delivered to the central warehouse"); 
         System.exit(0); 
         //myAcct.adjustBal(100);// with 100 if necessary 

        } 





      } 
      } 
     } 


    public class warehouse { 
    int balance = 0; 
    public warehouse(int openingBal) {  // constructor method 
     balance = openingBal; 
    } 
    public synchronized void adjustBal(int amt) { 
     balance += amt;   // process a transaction 
     checkBal();    // then show the balance 
    } 
    public void checkBal() { 
     System.out.print (balance); 

     System.out.println(); 
    } 
    public int getBal() { 
     return balance; 
    } 
} 
+2

類名應以大寫字母開頭爲'MainClass' – Abubakkar

+0

這看起來像一個家庭作業的問題。另外它的芝加哥,而不是Chicargo。 – monksy

回答

0

有一個競爭條件在這裏:

 if (myAcct.getBal() == 500) 
     { 
      System.out.println("The target goal of 500 widgets have been created and delivered to the central warehouse"); 
      System.exit(0); 
      //myAcct.adjustBal(100);// with 100 if necessary 

     } 

檢查和System.exit(0)其它線程之間在系統退出之前,仍然可以添加到倉庫。

0

您需要同步讀取和寫入共享變量balance以確保更改可見=>使getBalance()也同步。

但問題是更可能是由於對方的回答中提到的競爭條件。

0

您有由以下情形引起的問題:

假設你得到了400個項目,目前線程X是加入另一個100的時候線程X到達借貸檢查if statement另一個線程ÿ可能得到cpu時間並增加100個(總共給你600個項目),然後餘額檢查將永遠不會通過。

你應該做你的極限檢查在adjustBalance方法,因爲它是syncrhonized並保證只有一個線程增加了在同一時間檢查限制。

一個非常重要的注意事項,但:使用System.exit(0)中止你的進程,中間是一個非常糟糕的編程。您應該閱讀關於生產者/消費者關於如何在單個數據結構上管理多個線程的內容。