user3553237
user3553237

Reputation: 112

How to create Java Thread

I need to create a Point of Sale application.It has two class one is POS and other is Bill.

POS class creates an instance of Bill Class & Bill class handles all the logic to calculate the final bill.

There is one requirement that I don't understand

A single instance of the class POS should initiate bills.

  1. The interface should be Bill bill = pos.createBill(). Many clients could call pos.createBill() method simultaneously on the single POS instance, even from different threads.

  2. Each call to pos.createBill() should return a new instance of the Bill class.

  3. An instance of Bill will be used only in the one thread it was created in.

I don't understand which class should implement thread and how should I call it?

Upvotes: 0

Views: 122

Answers (3)

shalin
shalin

Reputation: 452

Why "Each call to pos.createBill() should return a new instance of the Bill class."???
Just use one Bill object

Bill bill = pos.createBill()

create this in Global location (In Main or in Form class,out side of any method within class) and use "Bill" in your method call. If and only your method taking too much of time to execute one task put those method calling in thread. Use asynchronously (Async) calling.

Upvotes: 0

Pramod S. Nikam
Pramod S. Nikam

Reputation: 4539

In Your POS class you should have thread declared as,

    Bill bill = new Bill();
    synchronized(bill)
    {
        Thread t = new Thread() { public void run() { 

               //  calls to bill.create bill.

        } };
        t.start();
        t.join();
    }

also As suggested above POS should be Singleton.

Upvotes: 1

asmalindi
asmalindi

Reputation: 49

POS should be use singleton prattern

Upvotes: 1

Related Questions