繁体   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