On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of...

24
On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia

description

The School of Computer & Information Science3 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.

Transcript of On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of...

Page 1: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

On Implementing High Level Concurrency in Java

G Stewart von ItzsteinMark Jasiunas

University of South Australia

Page 2: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 2

Overview

• Motivation• Syntax• Chemical Abstract Machine• Compiler• Pattern Matching• Benchmarks• Conclusion and Further Research

Page 3: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 3

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.

Page 4: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 4

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.

Page 5: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 5

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)?

Page 6: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 6

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.

Page 7: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 7

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 blockint JoinFragment1() & JoinFragment2(int val) {

Return val;}

Page 8: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 8

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 {..}

Page 9: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 9

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

Page 10: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 10

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

Page 11: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 11

Pattern Matcher

• This means that Join method resolution is inherently a pattern matching problem.

• We express the chemical abstract machine as a pattern matcher.

Page 12: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 12

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.

Page 13: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 13

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.

Page 14: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 14

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

Page 15: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 15

Example of a Join Java Program

THREAD 1…System.out.println (“Calling a()”);a();System.out.println (“a finished”);Sleep(1);

Fragment Pool (CHAM)JOIN METHODvoid a() & b(int B) { System.out.println (“Both a and b have been called”);}

THREAD 2…System.out.println (“Calling b”);b(3);System.out.println (“b finished”);Sleep(1);…

Output

Pattern Matcher

Calling aa()

arrivedAdd a to Fragment PoolaCheck state of Fragment pool: No patternsBlock thread 1

Calling bB(3)

ba&b found

Thread 1 notified

Both a and bhave been called

a finished

b finished

Page 16: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 16

11 0 0

Pattern Matching AlgorithmsLinear Matching

• 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.

01 1 0

10 1 0

A()&D() {}

A()&B() {}

B()&D() {}

10 9 0

Call to D arrives 00 9 0

Page 17: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 17

Pattern Matching AlgorithmsTree Matching

Page 18: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 18

Pattern Matching AlgorithmsTree 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)

Page 19: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 19

Pattern Matching AlgorithmsPrecalculated Table Matcher

Page 20: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 20

Pattern Matching AlgorithmsPrecalculated 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.

Page 21: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 21

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.

Page 22: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 23

Benchmark Modified Café Benchmark on Precalculated Matcher

Standard Java vs Join Java

050

100150200250300350Ja

vaSyn

chro

nize

dM

etho

d

Java

Syn

chro

nize

dBlo

ck M

etho

d

Join

Jav

a<i

nt>

<int

>

Join

Jav

a<v

oid>

<in

t>

Join

Jav

a<v

oid>

<obj

ect>

Join

Jav

a<v

oid>

<vo

id>

Tim

e (1

0̂-6

)

InterpreterHot Spot

Java Join Java

Page 23: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 24

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).

Page 24: On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.

The School of Computer & Information Science 25

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