vedran
vedran

Reputation: 2188

Java, working with threads

I'm working on a large homework which implements the use of Threads and synchronized methods. I never worked with Threads before, so it's a bit confusing. Since the homework is too large, I decided to first try with a simple example. So, in this example I have 4 classes:

So by definition, Worker must be a Thread. His run() method contains a loop which will make worker gather food (create new instances of Food) and store it in a stack in Storage. Each successful gathering decreases working hours. This loop will repeat itself until working hours are equal to 0. Now this is where I don't understand how to make a thread wait. Say, a worker has 15 hours and capacity of storage is 10. So, worker should add 10 new Food items in Storage, maxing its capacity, and wait for some (external) event to either increase the capacity or remove Food items from the storage so he can continue to "gather" the food and add it to the storage. Here's my current code:

import java.util.*;

class testSync {

    public static void main(String[] args) {
        /** Create a storage **/
        Storage storage = new Storage();
        /** Assign a worker to this storage **/
        Worker worker = new Worker(storage);
        /** Create a trash can **/
        Trash trash = new Trash(storage);

        /** Start new thread **/
        new Thread(worker).start();

        /** The thread should work until the maximum capacity of the storage has been reached **/

        /** Throw item so that further items can be added **/
        trash.throwItem();

    }
}

/** WORKER CLASS **/
class Worker implements Runnable {
    int work = 15;
    Storage storage;
    public Worker(Storage s) {
        storage = s;
    }
    /** Run this method until working hours equal to zero **/
    public void run() {
        while ( work > 0 ) {
            System.out.println(work);
            storage.store(new Food());
            work--;
            /** In case the capacity has been maxed out, wait for some event which will remove food items from the storage **/
            if ( !storage.hasSpace()) {
                // WAIT FOR THE STORAGE TO BE EMPTIED AND THEN CONTINUE ADDING
            }
        }
    }
}
/** TRASH CLASS **/
class Trash {

    Storage storage;

    public Trash(Storage s) {
        storage = s;
    }
    /** Remove one item from the storage **/
    public void throwItem() {
        storage.load();
    }
}

/** FOOD CLASS **/
class Food {
    public Food() {}
}

/** STORAGE CLASS **/
class Storage {

    private int cap = 10;
    private Stack<Food> container = new Stack<Food>();

    public Storage() {}
    /** Check to see if there's any free space **/
    public boolean hasSpace() {
        if (container.size() < cap)
            return true;
        else
            return false;
    }
    /** If capacity allows, add one an item to the storage **/
    public void store(Food food) {
        if (hasSpace()) {
            container.push(food);
        }
    }
    /** Remove one item from the fridge **/
    public Food load() {
        return container.pop();
    }
}

Upvotes: 1

Views: 932

Answers (2)

Sid Malani
Sid Malani

Reputation: 2116

Create a synchronised method on storage which returns true on accepting storage. Something like this...

public synchronized boolean store ( int num) {    
   if (  items  < capacity ) {
       items ++;
       return true;
    }
    return false;
}

Upvotes: 3

Kylar
Kylar

Reputation: 9344

Take a look at the BlockingQueue class - if you implement it right, you can use something like that that the worker can call, but it won't return until the queue(Storage) has room for the object.

Upvotes: 3

Related Questions