我有LinkedList Queue,我正在尝试读取一个文件,其中包含等待帮助的队列中的人数以及当时可用于帮助的代理数量。我不知道要检查他们是否正忙,或者首先如何添加排队等候的人。谁能帮我?这是我到目前为止的代码。
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());
}
}
答案 0 :(得分:1)
<强> - 编辑 - 强>
我看到您的代码现在已经用Java标记了;我的代码更像是一个c#/ 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
}
}
答案 1 :(得分:0)
这不是微不足道的,所以我建议您搜索一下有大量示例代码的教程,然后根据您的需要进行修改。
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()