ACSW2002 - Join Java - University of South Australia

Download Report

Transcript ACSW2002 - Join Java - University of South Australia

Applications of Join Java
G Stewart von Itzstein
Professor Martin Odersky
David Kearney
University of South Australia
Overview
Motivation
 Syntax
 Chemical Abstract Machine
 Compiler
 Example Code
 State Diagrams
 Petri Nets

Motivation
Despite the relative simplicity of Java’s
threading model, writing concurrent
programs in Java can still be strange and
confusing.
Tim Lindholm, Javasoft
Motivation

Normal Java Thread Communication
– Via shared variable.
 With
–
synchronized keyword
Piped Reader Writer
 Not
scalable.
 No idea of runtime decision on which channel to
form.
 Still via a shared variable hidden by API.
Motivation
Why is there no message passing in Java
threads?
 Shared memory model defeats
encapsulation.

Motivation Continued

Concurrent application are hard to write
correctly and understand.
–
–
–
Visualise, barrier synchronisation (threads
waiting at a certain point), thread pools (reusing
threads)
What wait goes with what notify?
We need a higher level of abstraction to
represent more complex concurrent problems.
Join Java
Can we add dynamic communication
channels to Java?
 Can we simplify thread creation
 Can we improve the code traceability of the
source code

Syntax

Compound method signatures (Join
methods)
–
–
–
–
A method body is not executed until all Join
fragments of the Join method have been called.
The first method can be synchronous (normal
Java return type) or asynchronous.
The second and subsequent methods are
asynchronous.
Eg. void fragment1() & fragment2() {..}
Syntax Continued

“signal” return representing asynchronous
operation.
–
–

A method of return type signal will not wait for
completion of the method before returning.
Eg. signal thread1() {..}
“ordered” modifier for class.
–
–
Changes the rules in which Join methods are evaluated.
Eg. ordered class JoinClass {..}
Chemical Abstract Machine
All Join methods represent possible
“molecules”
 An individual method call is an “atom”.
 When you make a call to a method the atom
representing the call is placed into the
chemical machine.
 When all the required atoms to complete a
molecule are in the pool, the atoms reacts
changing the composition of the pool.

Chemical Abstract Machine
Example

Join method A&B -> C, D
B
A
F
S
C
R
Before Addition of B
S
C
D*
F
R C*
After Addition of B
Implementation of ‘Chemical
Abstract Machine’
Join methods are inherently a pattern
matching problem.
 Express the chemical abstract machine as a
pattern matcher and the Join methods as the
patterns to match against.

Pattern Matcher
Pattern matcher acts as the Chemical
Abstract Machine.
 Reaction rules are the Join methods.
 Atoms are Join fragment calls

Architecture of The Compiler






Fairly standard Java compiler
Addition of a extra language features to parser
Addition of an extra semantic checking phase
Compile to byte code or standard Java code from
Join Java
Pattern matching code available as a compiled
library
Language extension generates standard Java byte
code
Class Modifiers and Restrictions

Additional class modifier ordered
–
–

When used each Join method will be tested in
the order in which they are declared.
When no modifier is used non-deterministic
choice is made.
No subclassing of a Join enabled class
–
All Join classes are declared final
Asynchronous Methods
class threader {
signal thread1() {
//code
System.out.println(“Hello World”);
//code
}
public static void main(String[] argv) {
threader x = new threader();
x.thread1();
x.thread1();
}
}
Message Passing
class Message {
public Message() {thread1();thread2();}
String receiver() & sender(String value) {
return value;
}
signal thread1() {
//do some work that produces a string value
sender(value);
}
signal thread2() {
String value = receiver();
//use value
}
}
State Diagrams

Conversion of State Diagrams to Code
x
A
C
y
a
B
b
State Diagram
State Diagram Code
ordered class statediagram {
public statediagram() { A(); }
void x() & A() { C(); }
void b() & A() { B(); }
void a() & B() { A(); }
void y() & C() { B(); }
void x(){}
void b(){}
void a(){}
void y(){}
}
x
A
C
y
a
B
b
State Diagram
Petri Nets
P1
P4
T2
P3
P2
P5
T1
T3
Petri Net Code
final class PetriNet {
signal P1() & P2() {
P3(); //P3 has now got a token
}
signal P3() {
P4(); //P4 has now got a token
P5(); //P5 has now got a token
}
signal P4() {
//No transitions out of this
}
signal P5() {
//No transitions out of this
}
}
When to Use/Not Use Join Java



Whenever there is a number of threads that need
to communicate dynamically.
Can use original synchronization method on
simple examples.
Join Java threads do not allow access to the low
level thread methods for example setDeamon()
–
–
Java threads are interchangeable with Join Java threads.
Possible to create a normal Java thread and then use
Join Java synchronization with it.
Next Steps

Add synchronous trailing methods.
–

Allow bi-directional channels to be used.
Benchmarking.
–
Is it fast enough?