d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help: Java : Timertasks/timer
Add Reply New Topic New Poll
Member
Posts: 27,007
Joined: Feb 15 2007
Gold: 0.77
Nov 15 2012 05:53am
Hello, for my assignment for my java I need to basically represent a supermarket checkout, with customers using a linkedlist queue

Each customer has a certain amount of groceries (randomized between 1-10)
When they are at the end of the queue they are getting served, I took each grocery as taking 1 second, so if a customer had 7 groceries and was at the top of the queue, after 7 seconds they will be removed from the queue, then the new top customer in the queue gets 'served'

I can get my timerTask working etc, however I can't work out how to once the TimerTask has ended (customer has finished) to return something so that I can remove it from the queue and start the next one.

Here's the code for my Timer/Task

Code
import java.util.Timer;
import java.util.TimerTask;

public class Schedule
{
   private Timer timer;
   private String customerName;
   
  public Schedule(Customer customer) {
       timer = new Timer();
       timer.schedule(new ScheduleTask(), customer.getgroceries()*1000);
       customerName = customer.getcustomer();
   }

   class ScheduleTask extends TimerTask {
       @Override
       public void run()
       {
           System.out.format(customerName + " Finished\n");
    timer.cancel(); //Terminate the timer thread
       }
   }
   
}


I have seperate methods for Customer and CustomerQueue
When testing it I do

Code
Customer customer1 = new Customer();
       Customer customer2 = new Customer();
       Customer customer3 = new Customer();
Schedule schedule1 = new Schedule(customer1);
Schedule schedule2 = new Schedule(customer2);
Schedule schedule3 = new Schedule(customer3);


But then all the Customers start at the same time instead of customer 1 waiting for customer 2 to finish :(

I hope you understand what I need!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 15 2012 09:07am
The whole point of the timer class is to be asynchronous. If you deliberately want it to wait a while, make it synchronous. Look into Threads. For your purposes, Thread.sleep(...) will prolly suffice as long as they're all on the same thread. alternatively you can design it so the scheduler picks off a customer from the queue when the timer fires

This post was edited by carteblanche on Nov 15 2012 09:35am
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 15 2012 03:57pm
Quote (OoOpeterOoO @ Nov 15 2012 04:53am)
Hello, for my assignment for my java I need to basically represent a supermarket checkout, with customers using a linkedlist queue


so. where's this linkedlist queue? it's certainly not anywhere in your code.
Member
Posts: 27,007
Joined: Feb 15 2007
Gold: 0.77
Nov 15 2012 06:28pm
Quote (irimi @ 15 Nov 2012 22:57)
so.  where's this linkedlist queue?  it's certainly not anywhere in your code.


It's in CustomerQueue class which is rrelevant for my issue

Quote (carteblanche @ 15 Nov 2012 16:07)
The whole point of the timer class is to be asynchronous. If you deliberately want it to wait a while, make it synchronous. Look into Threads. For your purposes, Thread.sleep(...) will prolly suffice as long as they're all on the same thread. alternatively you can design it so the scheduler picks off a customer from the queue when the timer fires


Thanks, I'll look up Thread.sleep tomorrow see how simple they are as I haven't actually learnt threads yet!

Thanks <3
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 15 2012 06:34pm
Quote (OoOpeterOoO @ Nov 15 2012 05:28pm)
It's in CustomerQueue class which is rrelevant for my issue


it's very relevant to your issue, actually. the reason your program isn't working is likely because there's a bug in the code you're not showing us.
Member
Posts: 18,954
Joined: Jul 5 2008
Gold: 4,784.51
Nov 15 2012 09:28pm
Here, hopefully this helps. You should be able to easily modify or take the principles from this and use it.

Code
import java.util.LinkedList;

// -------------------------------------------------------------------------
/**
* Simulates a LinkedList of Integers. Starts at the beginning of the LinkedList
* and then waits the number of seconds equal to each Integer's value.
*
* @author REM
* @version Nov 15, 2012
*/
public class IntegerWait
{
   // ----------------------------------------------------------
   /**
    * Place a description of your method here.
    *
    * @param args
    * @throws InterruptedException
    */
   public static void main(String[] args)
       throws InterruptedException
   {
       LinkedList<Integer> people = new LinkedList<Integer>();

       people.add(3);
       people.add(6);
       people.add(5);
       people.add(1);

       String customerList = "";

       for (Integer p : people)
       {
           customerList += "<" + p + "> ";
           System.out.println("\n>>>Now serving customer <" + p + ">");
           int currentTime = 1;
           synchronized (p)
           {
               while (currentTime < p + 1)
               {
                   p.wait(1000);
                   System.out.println(currentTime);
                   currentTime++;
               }
           }

           System.out.println(">>>Finished serving customer <" + p + ">");
       }

       System.out.println("\n>>>Finished serving all customers: "
           + customerList);
   }
}

Outputs:
Code
>>>Now serving customer <3>
1
2
3
>>>Finished serving customer <3>

>>>Now serving customer <6>
1
2
3
4
5
6
>>>Finished serving customer <6>

>>>Now serving customer <5>
1
2
3
4
5
>>>Finished serving customer <5>

>>>Now serving customer <1>
1
>>>Finished serving customer <1>

>>>Finished serving all customers: <3> <6> <5> <1>


This post was edited by Remembrance on Nov 15 2012 09:31pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 15 2012 09:54pm
yea no. if he can't figure out how to run a simple priority queue, there's no way that he's going to be able to figure out object.wait().

all that's really needed is

Code
Queue<Customer> foo = blah blah blah
while (foo.peek() != null) {
 new Schedule(foo.poll()).run();
}


dead simple.

This post was edited by irimi on Nov 15 2012 09:54pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll