Skip to content
rfqu edited this page Jun 20, 2015 · 2 revisions

Shared Places

Shared places are represented by standalone class org.df4j.core.SharedPlace which extends Actor1<Port>. That is, to request a token of type T from a shared place shp, user needs to pass a request of type Port: shp.post(port). When a token is available, the shared place sends the token to the port. To embed sending and receiving requests to an Actor, class Actor has inner parameter class RequestingInput. It must be instantiated with a parameter - reference to a shared place:

class Token {
}

class MySharedPlace extends SharedPlace {
// define storage for tokens
}
...
SharedPlace shp=new MySharedPlace();
...
class MyActor extends Actor {
   Semafor sema=new Semafor();
   RequestingInput inp=new RequestingInput<>(shp);
    
   protected void act() {
     Token t=inp.get();
     ...
   }
}

An instance of RequestingInput automatically requests token from the referenced shared place after all private places are filled. An actor without private places cannot start. In the example above, the only private place is Semafor sema. It was declared solely to initate requesting from the shared place by invoking sema.up().

In special case when some shared place can contain maximally one token, the place and token can be combined in single instance of class SharedToken. See example in DiningPhilosophers.java.

Choosing Executor for an Actor

By default, actors are submitted to common Fork-Join pool java.util.concurrent.ForkJoinPool.commonPool(). To use another executor, override method Actor.fire():

    // default implementation:
    protected void fire() {
        commonPool.execute(this);
    }    

    // SwingActor implementation, uses Event Dispatch Thread
    // and allows the act() method to modify GUI
    protected void fire() {
        EventQueue.invokeLater(this);
    }

    // fast implementation, uses caller's thread
    protected void fire() {
        this.run();
    }

Use fast implementation with caution: actor's method run() should be quick, chain of fast actors should be short and may not compose cycles in the dataflow graph.

Clone this wiki locally