Using Threads


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Using Threads

  1. #1
    Join Date
    Oct 2004
    Posts
    34

    Question Using Threads

    See module attached:
    The module simulates a museum's walkman rentals at 5.00 a piece and a total of 50 walkmans, and groups between 1 and 7 visitors. I want to have counter threads(3) so that the results would be:
    Counter 0 wants 1
    Counter 0 acquires 1
    Status: Deposits 5 Walkman 49
    Counter 1 wants 4
    Counter 1 acquires 4
    Status: Deposits 25 Walkman 45
    ...
    then when stock runs out counters must wait till the stock is replenished, via deposit()...

    i.e if there were only 2 tape players left and all groups at the counter were larger than 2 visitors, then no group could proceed.
    and message should say:

    Counter 1 wants 4
    Counter 2 wants 2
    Replacing 2
    Counter 2 acquires 2.....
    Status:.................

    Thanks hope that is clear....
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    This is not quite right.

    I've checked your code and there is no connection between the rentals
    and the receivals, e.g. the getStockBalance() method in Walkman (is a
    hack that) handles returns that exceed the original Walkman storage.

    As I see it you should have another class (say Entry) that spawned
    visitor groups that hired and returned the same number of walkmen, and
    then exited/disappeared/died.....

  3. #3
    Join Date
    Oct 2004
    Posts
    34

    Question Using Threads

    Sounds good can you give me an example of what you mean, Thanks?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Here is some pseudo code.

    If you establish a code sceleton I may can have a look at it.

    App creates Entry.
    App Creates Countes and starts the Counter threads that go to sleep immediately.
    App starts Entry
    Entry spawns (creates) one VisitorGroup each 4 sec.
    Entry assigns the VisitorGroup a headcount of between 3-10 visitors.
    Entry directs the VisitorGroup to one of the available Counters
    This is done by interrupting the Counter thread (e.g. waking it up)
    The Counter rents walkmen to the VisitorGroup if it has enough in stock.
    -if not, the Counter waits for a little while.
    (the Counter operates an internal stack of VisitorGroups that serves as the Counter que)
    The counter starts the VisitorGroup after having rented out the walkmen.
    The Counter then checks its queue, if any VisitorGroup found the Counter attempts a new rental/start.
    -if queue is empty it sleeps for a long time, as in the start.
    The visitor group goes for a tour of, say, 7-10 sec, - e.g. sleeps.
    On wakeup the VisitorGroup returns the walkmen to the Counter and exits. - this
    implies that a VisitorGroup must 'know' whicj Counter it rents from.
    The Counter interrupts its own sleep (if sleeping) when the VisitorGroup-returnal method is called.
    Last edited by sjalle; 04-21-2005 at 08:57 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center