Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 4 123 ... LastLast
Results 1 to 25 of 79

Thread: Queue Simulation

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Queue Simulation

    Hi im trying to simulate a waiting queue by using Java. What my program must consist of:

    • The user input nth number of cashiers

    • There minimum of 10 customers will arrive in the queue in random intervals.

    • When a cashier is free the the next customer is line will be processed.

    • The program must output each stage of the queue as well as the time each customer spent in the queue.

    • Oki so what I have so is a empty queue object, a random string list generator which sends the strings to the queue.


    However what problems im having is the random string generator is picking duplicates in the loop, how do I fix this ? Also how do I make it send the customers to the queue in intervals of 0.5 sec and I need to record the time they enter the queue and leave the queue so then I can output the time spent in the queue. Im stuck dont know what to do now ?

    public static Queue<String> line = new  LinkedList<String> ();
     
    public static void main(String[] args) 
    {
        String[] list = {"a", "b", "c", "e", "f", "g", "h", "i", "j", "k", };
        int customer = list.length;
     
        for (int x = 0; x < customer; x++ )
        {
          int cus = (int) (Math.random() * customer);
          line.add(list[cus]);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    Also posted at: Waiting Queue Simulation - Dev Shed

    As I said on the other forum, the customers names are not important. If there are 500 customers, you won't want to pick names for all of them from a list. Just assign them a number in the order that they arrive at the facility.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Yep really stuck on this

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    What kind of class are you taking? What has the instructor taught about this kind of program and process?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Honestly the instructor has just taught us about loops, Recursion and we haven't even done random generators yet but ive done it for my self.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    What kind of course is it? Why the queue wait problem?

    Is the program supposed to run in "clock time" or can it run faster: say 30ms for 30sec?

    or can it just jump from the time of one event to the time of the next event without having to wait for real clock time to elapse? For example if a customer is at the cashier at time XX, can the next event be at time XX+processtime without the program waiting processtime. The program moves from one event to the next event as fast as it can. The time between events is computed by subtracting the start time from the current time. The time coming from the event not from the clock.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    I believe the whole thing can run faster like you said 30ms

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    SO you are thinking you want to use clock time (but faster) to do the simulation
    instead of using what I'll call "event time" where the "current time" is set from the time in the event object.

    Using fast "clock time" (1ms for 1 sec) here is an idea:

    You need to generate new a Customer every 0 to 30sec and add that Customer to the wait queue if there are not any cashiers available.
    Next you'll need code to remove a customer from the queue and move it to one of the cashiers when one is available.

    These techniques will require using Threads.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Oki I understand that but what recording the time you have to record the time the customer went in the queue and then went out the queue to able to know the time spent in the queue. Is it possible to process this in the cashier ?

    Can you start me off with the coding.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    Sounds like you need a Customer class where you can record the time when the customer entered the queue and also a place to record the total wait time in the queue.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Queue Simulation

    Just accumulate values you allocate and call them the time spent? Why do you need a clock if you know what the time intervals are?

  12. #12
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Starstreak can you explain abit more how would that me done.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    The tricky bit is knowing when one of the cashiers will be free so a customer can be removed from the waiting queue and his waiting time recorded.
    The processing time at a cashier for each customer is a random value in a range, say 60 to 160.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    remember the user will input how many cashiers exist. To know of the cashier is free cant the been done with a if statement in thread. Also how did u work out the random time vales of 60 - 160 sec.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    random time vales of 60 - 160 sec.
    I made them up. The values depend on what is being simulated.

    the user will input how many cashiers exist
    Always try to parametize code so that the controlling values can be changed.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    remember the user will input how many cashiers exist. To know of the cashier is free cant the been done with a if statement in thread. Also how did u work out the random time vales if 60 - 160 sec.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    how did u work out the random time vales if 60 - 160 sec.
    Are you asking how to randomly generate a number in the range 60 to 160?
    Generate a number in the range 0 to (160-60) and add 60 to it.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    can u start me of with threads coding plz

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    If you don't know anything about threads, there can be a bit to learn.
    Start by reading the tutorial:
    Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    oki ill read up about it and start testing ill get back to when I have a problem lol

  21. #21
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Ive read up on threads and so far Ive managed to get loop working with outputting the customer name and the time they were served by using methods in another class. However Im still stuck on if the user inputs nth number of cashier which basically mean the whole loop will run x(nth cashier inputted).

    while (!line.isEmpty())
    		{
    			System.out.println(line  + "\n");
    			System.out.println("The queue has " + line.size() + " customers left");
    			Customer cus = line.remove();
    			System.out.println(cus.name + " queued at " + cus.getTime() + " <=== SERVED" + "\n");
    			// you will have to sleep a random number of seconds here
    			int wait = ran.nextInt(2) + 1;  // will generate 1 or 2
    			try 
    			{
    			     Thread.sleep(wait * 1000);
    			}
    			catch(Exception e) 
    			{
    				System.out.println("Sleep error: " + e);
    			}

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    The thread with the loop generates a customer arrival every x to y seconds. the values of x and y are determined by what type of facility you are modelling. The number of customers to generate is a parameter for the program.
    The generated customer either goes to a cashier or goes into the wait queue to wait for a cashier. The number of cashiers is a parameter for the program.

    For the first version of the program just make the loop as described above. After the loop ends, print out what customers are at a cashier and the contents of the waiting queue. The cashiers could be an array of customers. The cashier is free if null and busy if there is a customer there.
    Last edited by Norm; April 2nd, 2013 at 04:50 PM. Reason: Added cashier array idea
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Queue Simulation

    Quote Originally Posted by spiderd View Post
    Starstreak can you explain abit more how would that me done.
    Well, Norm seems to advocate a real time simulation, whereas I propose just calculating and accumulating known (random) time intervals. You then pump them out in one go during runtime.

    For the actual queue, how about a Java List, where data can be appended (FIFO)?

    {Throwing ideas about here )

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    @Starstreak This has some good ideas: Multi-queue simulation theory

    I've proposed two solutions: a real time and an event time.

    A question: What kind of solution does the instructor want?
    The real time solution involves threads, timers and synchronizing of queue accesses
    The event time is some looping with some logic to control event sequences
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    The instructor hasn't specifed whether real time or event time is there any existing examples you can refer me to so i can get a better understanding and get some practice.

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Multi-queue simulation theory
    By Herah in forum Object Oriented Programming
    Replies: 4
    Last Post: April 4th, 2013, 10:38 PM
  2. Cash Register Simulation Help
    By Gont in forum Object Oriented Programming
    Replies: 1
    Last Post: September 22nd, 2012, 06:58 PM
  3. create a simulation
    By aecosis in forum Java IDEs
    Replies: 3
    Last Post: June 12th, 2012, 08:55 AM
  4. Blackjack simulation program help
    By senorfletch in forum Java Theory & Questions
    Replies: 2
    Last Post: April 5th, 2011, 09:22 AM
  5. [SOLVED] Dice Rolling Simulation
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2011, 05:51 PM