On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia Overview • • • • • • • Motivation Syntax Chemical Abstract Machine Compiler Pattern Matching Benchmarks Conclusion and Further Research The School of Computer & Information Science 2 Motivation • Normal Java Thread Communication • Via shared variable. • Using synchronization. • Piped Reader Writer. • Not scalable. • No direct support of runtime decisions on channel formation. • Still via a shared variable hidden by API. The School of Computer & Information Science 3 Motivation • Why is there no message passing for Java threads? • Shared memory model defeats encapsulation. • Concurrent application are hard to write correctly and understand. • Visualise thread pools. • What wait goes with what notify? • People end up using notifyAll then select the correct thread putting all the others back to sleep. • We need a higher level of abstraction to represent more complex concurrent problems. • One just to has to teach an undergraduate class to see the creative interpretations of concurrency. The School of Computer & Information Science 4 Motivation • Can we integrate dynamic communication channels into Java rather than add them as a library? • Can we simplify thread creation? • Can we simplify parameterized thread creation? • Can we improve the code traceability (readability)? The School of Computer & Information Science 5 Motivation • Join Java goes someway to solving some of these problems. • • • • Gives explicit defined communications channels. Simplifies thread creation. Can create thread with parameters. Code is simplified not necessary to use wait/notify in a lot of cases. • Join Java is a superset language of Java. The School of Computer & Information Science 6 Join Java Syntax • Compound method signatures (Join methods) • Join fragments are individual method calls. • Join method is a number of Join fragments and a method body. • 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. • Example //JoinFragment1 blocks JoinFragment2 does not block int JoinFragment1() & JoinFragment2(int val) { Return val; } The School of Computer & Information Science 7 Join Java Syntax Continued • “signal” return representing asynchronous operation. • A method of return type signal will not wait for completion of the method before returning. • Convenient way of creating threads. • Eg. signal thread1() {..} • “ordered” modifier for class. • Changes the rules in which Join methods are evaluated. • How to handle ambiguous situations with respect to Join methods. • Eg. ordered class JoinClass {..} The School of Computer & Information Science 8 Chemical Abstract Machine • Need to represent the execution environment of Join Java. This is done by representing it as a chemical abstract machine. • An individual method call (Join fragment) is an “atom”. • A() • All Join methods represent possible “molecules” • A() & B() {…} • A particular type of atom may be part of different molecules. • A() & B() {…} • C() & B() {…} • A() & Z() {…} • A() is in patterns 1 and 3 • B() is in patterns 1 and 2 The School of Computer & Information Science 9 Chemical Abstract Machine • When you make a call to a Join fragment the atom representing the Join fragment 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. • Completed molecules are removed from the pool along with their component atoms The School of Computer & Information Science 10 Pattern Matcher • This means that Join method resolution is inherently a pattern matching problem. • We express the chemical abstract machine as a pattern matcher. The School of Computer & Information Science 11 Architecture of The Compiler • Fairly standard Java compiler. • Join Java adds; • • • • Addition of extra language features to parser. Addition of an extra semantic checking phase. Pretty printer. Translator phase that converts from Join Java AST to Java AST. • Compiles to standard Java byte-code or standard Java source code from Join Java source code. The School of Computer & Information Science 12 Architecture of The Compiler • Language extension generates standard Java byte code • In the language prototype. • Pattern matching code available as a compiled library • Uses a library for matching rather than compiling all the code. • Disadvantage : • Have to supply runtime files. • Advantage: • Can rapidly prototype different designs. • A final language version will generate all the code. The School of Computer & Information Science 13 Class Modifiers and Prototype 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 The School of Computer & Information Science 14 Example of a Join Java Program JOIN METHOD Fragment Pool (CHAM) void a() & b(int B) { System.out.println (“Both Both a and b called have been called”); } THREAD 1 Pattern Matcher … System.out.println (“Calling Calling a()”); a a(); a() System.out.println (“a a finished”); finished Sleep(1); arrived Add a to Fragment Poola b patterns Check state of Fragment pool: Noa&b found Block thread 1 Thread 1 notified THREAD 2 … System.out.println (“Calling Calling b”); b b(3); B(3) System.out.println (“b b finished”); finished Sleep(1); … Output The School of Computer & Information Science 15 Pattern Matching Algorithms Linear Matching Call to D arrives 0 9 0 0 0 9 0 1 1 0 0 1 A()&D() {} 1 1 0 0 A()&B() {} 0 1 0 1 B()&D() {} • Just take the present state and compare it to all completion states one at a time. • Is slow in the O(f*p). • Where • f = number of fragments. • p = number of patterns. The School of Computer & Information Science 16 Pattern Matching Algorithms Tree Matching The School of Computer & Information Science 17 Pattern Matching Algorithms Tree Matching • Advantages • Prunes Search Space (Quicker) • Average time is short (assuming some locality of Join methods). • Disadvantages • Pathological example of one Join fragment in all Join patterns becomes something like quadratic (viz linear matching) The School of Computer & Information Science 18 Pattern Matching Algorithms Precalculated Table Matcher The School of Computer & Information Science 19 Pattern Matching Algorithms Precalculated Table Matcher • Advantages • Can be precalculated at compile time in a final production version. • Disadvantages • Requires more memory. • Memory footprint can be reduced by using symmetry. • However, design of Join Java is one pattern matcher per class so the size of the pattern matcher does not get large. The School of Computer & Information Science 20 Benchmarking • How fast is Join Java? • Majority of cases not much difference to that of standard Java synchronized methods. • Raw method call overhead is high in some cases. Usually due to prototype rather than any limiting factor. • Not recommended to make every method a Join method • Standard Java call != Join Java method call • However, Synchronized Java call != Standard Java call • Synchronized blocks are even slower. The School of Computer & Information Science 21 Benchmark Modified Café Benchmark on Precalculated Matcher Java The School of Computer & Information Science Join Java <void> <void> Join Java <void> <object> Join Java <void> <int> Hot Spot Join Java <int> <int> Interpreter Join Java Java Synchronized Block Method 350 300 250 200 150 100 50 0 Java Synchronized Method Time (10^-6) Standard Java vs Join Java 23 Problems • Hotspot Optimizations • Adaptive inlining/Dynamic deoptimization. Fine for standard Java programs possibly not for Join Java translations. • Hot spotting may be disrupted by the way the pattern matcher runs. • If the code doesn’t appear the same to the hot spotting code it won’t optimize it. • Unboxing • Expensive (see <int> -> <int> translation on the previous page. • No JVM support for boxing and unboxing (unlike .NET runtime). The School of Computer & Information Science 24 Conclusions & Future work • Hotspot problems may fade in the next hotspot JVM which has on stack replacement. • JVM hotspots code and replaces interpreted code as its running rather the next time its needed. • Overheads in Java (boxing) • .Net has a VM instruction. • May be able to optimize an open source JVM for Join patterns without changing the instruction set. • Further Pattern Matching Algorithms • Symmetric pattern matching The School of Computer & Information Science 25
© Copyright 2025 Paperzz