22
Using a simple Rendez- Vous mechanism in Java João Duarte [email protected]

Using a simple Rendez-Vous mechanism in Java João Duarte [email protected]

Embed Size (px)

Citation preview

Page 1: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Using a simple Rendez-Vous mechanism in Java

João [email protected]

Page 2: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Motivation

Real time programs Cooperating threads sharing the

same context (data structures, IO devices, etc.)

Need for interaction

Page 3: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Interaction

Interaction primitives: Locks Semaphores

Structured interaction mechanisms:

Rendez-Vous (ADA) Monitors (Java)

Page 4: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Java Monitors Monitor: passive object providing

synchronized methods Dynamic model of Java multithreading

computation: Dynamic set of monitors Dynamic set of threads Threads interact indirectly through monitor

services calls. Direct interaction of threads was

suppressed for safety reasons (e.g. functions resume() and suspend() not supported after JDK 1.3)

Page 5: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

ADA’s Rendez-Vous Common execution of a code section by

two threads (tasks) Asymmetric, “roles” of both tasks are

different: The client calls rendezvous (and needs “to

know” the partner) The server accepts rendezvous (and does

not need to know the client) Synchronous, the first task reaching the

rendezvous point waits for its partner

Page 6: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

ADA’s Rendez-Vous Data of both “actors” of rendezvous can

be used within the execution Every transformation of tasks data can

be programmed (and atomically executed) as a rendezvous

Dynamic model of Ada multithreading computation: Set of tasks that interact directly using the

rendezvous mechanism

Page 7: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

ADA’s Rendez-Vous - Server Example

Initialization of Entries

Beginning of task

Store first Item

Page 8: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

ADA’s Rendez-Vous - Server Example

Select statement with two alternatives

Page 9: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

ADA’s Rendez-Vous – Client Example

Create the Task

Acess as a normal variable

Page 10: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Using ADA’s Rendez-Vous in Java – Why? Allows “pure monitor” (i.e. server)

thread. More general model of parallel

(concurrent) computation. An easy way how to synchronize

two threads in a specified point of their execution and join data exchange/transformation.

Page 11: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Using ADA’s Rendez-Vous in Java – Why?

A way how to construct threads with “two faces”, i.e. part of their life they can act as clients and another part as servers

A way how to construct multiple-mode monitors, e.g. monitors that provides various sets of services within various stages of their life.

Page 12: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Implementing Rendez-Vous in Java - Keypoints

As simple as possible,no need to include all the facilities of the Ada rendezvous implementation

No need for any special JVM or pre-processor

Construct an object type whose instance can manage an entry synchronization (i.e. to every single entry should be assigned one synchronization object).

Page 13: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Implementing Rendez-Vous in Java - Keypoints Methods of a dedicated

synchronization object should be called only from the server side of rendezvous.

To call an entry from the client side should be as simple as the Java conventional public method call

It is necessary to implement a simple form of the Ada select statement

Page 14: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Implementing Rendez-Vous in Java – State Machine

Page 15: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Implementing Rendez-Vous in Java – Entry class

Page 16: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Implementing Rendez-Vous in Java – Entry class

Page 17: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Implementing Rendez-Vous in Java – Entry class

Page 18: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Using the Entry class

Uses Entry e1

Both use Entry e2

Code executed inside the Rendez Vous

Page 19: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Using the Entry class

here it accepts only entry_1() call

it accepts either entry_21() or entry_22() call

it can call entry of another server as well

Page 20: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Conclusion

Simple and easy to use Just one class needed Extends possibilities on how to

design Java multithreaded program architecture

Page 21: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Future work

Add EntrySet class Include all the possibilities of the

select statement: Boolean guards Delays else alternative

Page 22: Using a simple Rendez-Vous mechanism in Java João Duarte duarte@kiv.zcu.cz

Web

Source code and examples: http://www.kiv.zcu.cz/~stracek/ppr/java/randez/

Email: [email protected]