Results 1 to 19 of 19
  1. #1
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default Program for a store

    Hi ALL,

    I need to write a program for a store. The store has three lines and 3 clarks. The owner thinks one line is better. The program will make the comparison. It is not GUI.

    The input information will be in a text file, which will include how long each clark processes a customer and when a customer joins a line, and the store start and close times, etc.

    The rules are
    1 each line is a queue, which means once you are in the line, you cannot get out of it unless you are the first.

    2 A customer will join the shortest line. If 2 or 3 lines are of the same length, he will join in from left.

    A clark can wait a customer to come when the line is empty and the first customer in line has to wait for the clark when the clark has a customer. Do I need 6 threads (3 for the clarks and 3 for the lines)? Do I need to write the event to tell the clark when the line is empty?

    It is very complicated to me. Any suggestions will be greatly appreciated.

    Thank you very much.

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    You have rather detailed specs. I would say just start building, and keep it simple. The need of threads shows up while working, often some controlloop can do it all. Maybe the input (customers) needs some random timing, and the jobs some random throughputtime, but only introduce threads/timers when needed. You have obvious objects, just keep an eye on differentiating Model, View (printing in this case) and Controller-classes. Come back here when you are stuck.

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    I would suggest you forget about writing any code until you have worked out exactly how the thing is going to work on paper. You need to understand the issues involved before committing anything to code. When you think you know exactly how the program will work, then you can start writing code. The design looks tricky, and coding is tricky; trying to work out the design while writing the code is asking for trouble.

  4. #4
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    In run() method of the TimerTask class, I need to modify an array. But run() is void. How can I get the array after the change?

    This question is related this project "Program for a store"

    Thank you very much.

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I suppose you mean java.util.TimerTask? There are examples if you google, like this one: Java Practices -> Use Timer to schedule periodic tasks
    For the moment I think the contents of your run() should be something like:
    - modifyArray();
    - doSomethingWithIt();
    What are you using the timer for? In the end I guess the customers have to enter in a more random way then on the ticks of a timer (to start with you can take a random number of ticks), changing it later in a poisson-distribution. But you wrote the processing-times and the joiningtimes are in the inputfile?
    I have to leave now. Will look if you solved it this evening.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by yma16 View Post
    A clark can wait a customer to come when the line is empty and the first customer in line has to wait for the clark when the clark has a customer. Do I need 6 threads (3 for the clarks and 3 for the lines)? Do I need to write the event to tell the clark when the line is empty?
    For discrete event simulation you don't need Threads at all. Think of a time line. You place events on the line at a certain time in the future. You handle the event at time 'now'. When it is finished you simply handle the next event, i.e. you make a jump forward in time. You arbitrarily break ties (two events occurring at the same time). The possible events are:

    1) customer arrives;
    2) customer handled by clark;

    All you do is register the time it takes for each customer to enter and leave the shop again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default I read the link of the Timer example

    Use java.util.Timer to schedule a task to execute once 5 seconds have passed : Timer**Development Class**Java

    It used both Timer and TimerTask together. timer.schedule() calls run(). That is why I also used Timer. If there is a way not to use Timer to do the scheduled work, please let me know.

    As I said, the customers arrival times are in the input file. There is no randomness in the project.

    The only thing that is a little more general is the number of windows are in the file. It could be any positive integer.

    The array is in fact an array of queues with push and pop methods in the queue. The array is a property inside a subclass of the TimerTask.

    I am confused between when to using Timer and TimerTask vs Thread. Is Thread for handling random events?

    I am trying to not to use Thread as JosAH said.

    Thank you all very much.

  8. #8
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default TimerTask and Timer vs Thread from Jodokus's link

    ■if it may take a long time, or may block, use a Thread
    ■if it is to be done later, or periodically, use TimerTask and Timer (which itself uses a background thread)

    So for me, TimerTask and Timer are ok.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by yma16 View Post
    ■if it may take a long time, or may block, use a Thread
    ■if it is to be done later, or periodically, use TimerTask and Timer (which itself uses a background thread)

    So for me, TimerTask and Timer are ok.
    Have you read reply #6? For discrete event simulation you don't need Threads or Timers or TimerTasks or whatever, unless you want to make things difficult for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    Hi Jos,

    Thank you for the information. I've read all the posts. This project's events are time events. I have no idea on how to do it without using TimerTask and Timer. Assume there are 3 windows and one line. The clarks processing times are 3, 2, 1 minutes from left to right respectively. For the 1st customer, he spends 3 minutes. But for the 2nd one and on, there are more possibilities. If the 2nd comes after the 1st has left, he would spend 3 minutes. If not, he would spend 2 minutes. For the 3rd and 4th people, it is even harder for me to register their events. Whereas when I think about the real store situation related to time, the operation is clear. (Note: minutes are integers>0 and the number of queues are either 1 or 1 queue for each clark) Could share a small example with me? Say there 2 windows and 4 customers will come. Thanks a lot.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Is Google down again in your part of the world? Read this.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    Thank you again, Jos. I would try both ways if I have time. Based on your link, I can just use a int counter to represent the time in seconds (integers) and move events along.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by yma16 View Post
    Thank you again, Jos. I would try both ways if I have time. Based on your link, I can just use a int counter to represent the time in seconds (integers) and move events along.
    Yep, and it would be silly to increment that counter by steps of one, i.e if the first event happen at time 4 and the next event happens at time 7, the counter hops from 4 to 7 instantaneously because there's nothing to do in the mean time; simulated time isn't real time. The event generator can be an event by itself, just to keep the simulation going.

    The event 'dispatcher' simply walks the event queue and dispatches them. You don't need separate threads for that. When the queue becomes empty, the simulation is done. All the events register some statistics which are printed when the simulation is ready.

    kind regards,

    Jos
    Last edited by JosAH; 05-06-2011 at 07:07 PM.
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Hello yma,
    Back again. You are clearly in the ideaforming phase, and that's very important. At first I didn't take dlorde too serious: I had a clear idea from what you wrote so you probably had it too. That has changed. In the meantime several ideas have passed.
    Jos is clearly on the track that the goal of simulation is to answer your original question: is one line better then three. Then it is stupid to really simulate in real time (or "upspeeded" time): you just have to handle the consequences of events and you don't have to let the computer wait during times nothing happens (and threads are probably unnecessary).
    Because you started about threads and timers I had the idea that you wanted a simulation in real (or compressed) time. I even thought it is a pity that you abandoned the GUI. Then you probably need a thread "producing" customers.
    It is not that one or the other is better, but dlorde is right, these are things YOU have to decide before you start coding. But it is very good anyway to have considered timers and threads. Don't use them too soon: most programs don't need any.
    Last edited by Jodokus; 05-07-2011 at 11:54 AM. Reason: spelling

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Jodokus View Post
    Jos is clearly on the track that the goal of simulation is to answer your original question: is one line better then three.
    If I were the OP I wouldn't start programming at all but study the M/M/n queueing model first. I all depends on the probability distribution of the arrival time of the customers and the probability distribution of the handling time by the clarks but I guess it can be solved by just a bit of scribbling on paper ... The question would boil down to: which is better n == 1 or n == 3?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default Thank you Jos

    Your suggestion is very valuable. I am studying the things based on your opinion.

    As I mention before, the store has the closing time in the input file. So the program ending time is when

    1 The closing time is reached.

    Or

    2 all the Queues are empty and the time has passed (time is bigger than) the time that the last customer(s) joined the lines.

  17. #17
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default Thank you Jodokus

    I think I'd try Jos idea. If I can get it done, it will run much faster. If the time period is 8 hour, it would be a headache to test the timer stuff. Also, it seems hard to debug Timer stuff since it is supposed to be runing constantly.

  18. #18
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Let us know how many lines we should use!

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by yma16 View Post
    Your suggestion is very valuable. I am studying the things based on your opinion.

    As I mention before, the store has the closing time in the input file. So the program ending time is when

    1 The closing time is reached.

    Or

    2 all the Queues are empty and the time has passed (time is bigger than) the time that the last customer(s) joined the lines.
    You're welcome of course; you have to design and implement the following sub-tasks:

    1) a queue ordered by time. Two or more events with the same 'time' are ordered arbitrarily;
    2) an event handler; it grabs the earliest event from the queue and handles it;
    3) an event generator; it generates events; in your case it generates customers entering the shop and maybe even the shop closing its doors event.

    A small tip: make 3) also an event itself; when you're designing and implementing your stuff you'll know what I mean. By no means are all those 'events' related to Swing event.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. cant store images
    By wildheart25c in forum CLDC and MIDP
    Replies: 1
    Last Post: 01-11-2011, 10:38 AM
  2. problem with store
    By dj kourampies in forum New To Java
    Replies: 5
    Last Post: 08-23-2009, 07:55 PM
  3. Extract xml to store into db
    By palanikumark in forum Advanced Java
    Replies: 3
    Last Post: 06-06-2008, 04:09 PM
  4. store file
    By kazitula in forum Java Applets
    Replies: 0
    Last Post: 02-17-2008, 10:45 PM
  5. I would then store a string...
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •