简体   繁体   中英

LinkedList queue implementation

I have LinkedList Queue, and I'm trying to read a file with numbers of people in queue waiting to be helped and the number of agents available to help at the time. I do not know to check if they are busy or how to add the people waiting in the queue in the first place. Can anyone help me? This is the code I have so far.

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

--EDIT--

I see your code has now been tagged in Java; my code is more of ac#/pseudo, so you might need to convert it into Java.

--EDIT--

Though this may not help. But I'd suggest a more entity orient approach; something like:

  • Agents, Agent List: Shall list the available agents
  • Customers, Customer Queue: Shall maintain a queue of customers wanting help
  • CustomerSupport Manager:
    • Shall see if an agent is available(not busy)
    • Dequeue the customer
    • Assign it to one of the available agents

Above the top of my head, see following:

Customer:

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

Agent:

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;
    }
}

Manager:

A class that manages the customers and agents, according to availability

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 

    }
}

It's not trivial so I suggest you search a bit for a tutorial with lots of example code and then alter it to suit your needs.

8.3 The Producer/Consumer Pattern - Java Threads, Third Edition

The producer-consumer pattern in Java 5: using blocking queues in preference to wait()/notify()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM