Multiple threads, require a synchronized list, which can hold multiple variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonjon0688
    New Member
    • Mar 2008
    • 14

    #1

    Multiple threads, require a synchronized list, which can hold multiple variables

    I have the task of creating 3 classes, Cashier, OrderList and cook.

    The cashier adds an order to the OrderList. The order info is the cashiers name, the order number (unique) and the current time.

    I have written code to get the relevant information for the order (see below), however I do not know how to put it in the OrderList, I thought an arrayList could only hold one variable. Also should the list be declared in OrderList or in the Cashier class?
    Also, the classes have to be run concurrently.

    public class CashierThread2 implements Runnable{



    public String theCashier;
    private static int theNextNum = 1;
    public int orderNo;
    public String timeNow;
    private String DATE_FORMAT_NOW = "HH:mm:ss";


    public void setCashier(Stri ng aCashier) // set Cashier name
    {
    theCashier = aCashier;
    }

    public String getCashier()
    {
    return theCashier;
    }

    public synchronized void uniqueNumber() // next Order number
    {
    theNextNum ++;
    orderNo = theNextNum;
    }

    public int getOrderNo()
    {
    return orderNo;

    }

    public String getTimeOfOrder( )
    {

    return timeNow;
    }

    public void now() // get the current time
    {
    Calendar cal = Calendar.getIns tance();
    SimpleDateForma t sdf = new SimpleDateForma t(DATE_FORMAT_N OW);
    String time = sdf.format(cal. getTime());
    timeNow = time;
    }


    public void run()
    {
    try {

    Thread.currentT hread().sleep(3 );
    } catch (InterruptedExc eption e) {
    }

    out.println();
    out.println("Ca shier's name is " + theCashier);
    out.println("Or der Number is " + orderNo);
    out.println("Th e current time is " + timeNow);
    }

    public static void main (String args[])
    {

    CashierThread2 c = new CashierThread2( );
    c.setCashier("A mie");
    c.now();
    c.uniqueNumber( );
    new Thread(c).start ();

    CashierThread2 d = new CashierThread2( );
    d.setCashier("J ohn");
    d.now();
    d.uniqueNumber( );
    new Thread(d).start ();

    CashierThread2 e = new CashierThread2( );
    e.setCashier("B ill");
    e.now();
    e.uniqueNumber( );
    new Thread(e).start ();

    }
    }

    Thanks in advance!
  • jonjon0688
    New Member
    • Mar 2008
    • 14

    #2
    Ok, I now realise i need a LinkedList and I think this should be declared in the OrderList class, how do I get the information from the Cashier Thread into the LinkedList?

    Thanks.

    Comment

    Working...