簡體   English   中英

LinkedList隊列實現

[英]LinkedList queue implementation

我有LinkedList隊列,並且我正在嘗試讀取一個文件,該隊列中有等待幫助的人數以及當時可以提供幫助的代理人數。 我不知道要檢查他們是否忙,或者如何首先添加隊列中等待的人。 誰能幫我? 這是我到目前為止的代碼。

public class WaitingQueue 
{
 public int [] windows = 0; // every time we add some one  check if location occupied
 public int time = 0;
  public int waitTime = 0;

 public static void main(String args[])
  {
   Queue newQueue = new Queue();
   try{

     FileInputStream fn = new FileInputStream(args[0]); 
     BufferedReader br = new BufferedReader(new InputStreamReader(fn));
     String line;

     while((line = br.readLine()) != null)
     {
         time++; // happens every time window i busy
         waitTime++  // increment waiTime
           if ( time for people to arrive)
         {
           add people to the queue // have to have a queue for people waiting.
             //use enque to add people.
         }
         if(window is open)
         {
           // move people from queue to window
           // use dequeue
         }

         if(time = x;)
         {
           // add some people to list
         }
       }

  //Close the input stream
       outFile.close();
       fn.close();
      }
     }catch (Exception e)
     {/*Catches exception*/
      System.err.println("An error has occured : " + e.getMessage());
      }
     }

- 編輯 -

我看到您的代碼現在已經用Java標記了; 我的代碼更多是ac#/ pseudo,因此您可能需要將其轉換為Java。

- 編輯 -

雖然這可能無濟於事。 但是我建議采用一種更面向實體的方法。 就像是:

  • 代理,代理列表:應列出可用的代理
  • 客戶,客戶隊列:應保持需要幫助的客戶隊列
  • 客戶支持經理:
    • 將查看代理是否可用(不忙)
    • Dequeue客戶
    • 將其分配給可用的代理之一

在我頭頂上方,請參閱以下內容:

顧客:

public class Customer
{
    string _strName;
    public Customer(string strName) { _strName = strName; }
}

代理商:

public class Agent
{
    string _strName;
    bool _bIsBusy = false;//
    public bool IsBusy { get { return _bIsBusy; } }

    Customer _Customer;
    public Agent(string strName)
    {
        _strName = strName;
    }

    public void HandleCustomer(Customer theCustomer)
    {
        _Customer = theCustomer;
        _bIsBusy = true;//Busy as long as the window is open.

        //You might need something that doesnt block;
        Thread.Sleep(5 * 1000); //Wait for time to simulate that agent is talking to customer

        RemoveCustomer();//Done with the customer.
    }

    private void RemoveCustomer()
    {
        _Customer = null;
        _bIsBusy = false;
    }
}

經理:

根據可用性管理客戶和代理商的類

public class CustomerServiceBench
{
    Queue<Customer> queCustomers = new Queue<Customer>();
    List<Agent> lstAgents = new List<Agent>();
    Thread thdService;

    public CustomerServiceBench()
    {
        //Something along these lines.
        thdService = new Thread(delegate() { WaitAndAddCustomerIfAgentIsAvailable(); });

    }

    private void AddCustomer()
    {
        //Add a dummy customer.
        Random r = new Random(1231);
        queCustomers.Enqueue(new Customer("Customer" + r.Next().ToString()));

        Thread.Sleep(5 * 1000); //SpinWait.Once()...

    }

    private void WaitAndAddCustomerIfAgentIsAvailable()
    {
        //Thread1 to manage the 

    }
}

這不是一件容易的事,因此我建議您花一點時間搜索包含大量示例代碼的教程,然后對其進行更改以適合您的需求。

8.3生產者/消費者模式-Java線程,第三版

Java 5中的生產者-消費者模式:優先使用阻塞隊列,而不是wait()/ notify()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM