Java concurrency

32
Java Concurrency By: Hithem Aly

description

The presentation intended to give a basic introduction for Concurrency in Java

Transcript of Java concurrency

Page 1: Java concurrency

Java ConcurrencyBy: Hithem Aly

Page 2: Java concurrency

Agenda Objective Facts Thread Safety Java Concurrency Problems (Symptoms) Java Concurrency methods

Page 3: Java concurrency

Objective Managing shared state

Page 4: Java concurrency

Controller

Objective Shared State

S S S Heap

T T T

Method Area

Page 5: Java concurrency

Is That Thread Safe?package com.thread;

class ApplicationThread implements Runnable{

private int i = 0;

@Overridepublic void run() {System.out.println(++i);}

}

public class Main {

public static void main(String[] args) {

for (int i = 0; i < 1000; i++) {Thread thread1 = new Thread(new ApplicationThread());thread1.start();}}

}

Page 6: Java concurrency

Is That Thread Safe?package com.thread;

class ApplicationThread implements Runnable{

private int i = 0;

@Overridepublic void run() {System.out.println(++i);}

}public class Main {

public static void main(String[] args) {

Runnable runnable = new ApplicationThread();

for (int i = 0; i < 100000; i++) {Thread thread1 = new Thread(runnable);thread1.start();}}

}

Page 7: Java concurrency

Agenda Objective Facts Thread Safety Java Concurrency Problems (Symptoms) Java Concurrency methods

Page 8: Java concurrency

Facts Processors, cache, RAM interactions Compilers optimization (Instructions Re-

ordering)

Page 9: Java concurrency

Processors, cache, RAM interactions

P1 P2

C1 C2

RAM

R1 R2

Stack 1

HeapStack

2

Thread 1

Thread 2

text (code)

segment

Page 10: Java concurrency

Compilers optimization

For more Reference, Cglib (Code generation Library)

• Drop memory write for non volatile variables• Instructions Reordering

• int A, B;   • void foo() • { A = B + 1; • B = 0; }

Ex:

static final int length = 25; static final int width = 10; int res = length * width; //int res = 250;

• Constant folding

• int A, B;   • void foo() • {B = 0; • A = B + 1; }

Page 11: Java concurrency

Illustrative Examplepublic class NoVisibility {

private static boolean ready; private static int number;

private static class ReaderThread extends Thread {

public void run() {

while (!ready) Thread.yield(); System.out.println(number);

} }

public static void main(String[] args) {

new ReaderThread().start(); number = 42; ready = true;

}

}

Page 12: Java concurrency

Solutionpublic class NoVisibility {

private static volatile boolean ready; private static volatile int number;

private static class ReaderThread extends Thread {

public void run() {

while (!ready) Thread.yield(); System.out.println(number);

} }

public static void main(String[] args) {

new ReaderThread().start(); number = 42; ready = true;

}

}

Page 13: Java concurrency

Agenda Objective Facts Thread Safety Java Concurrency Problems (Symptoms) Java Concurrency methods

Page 14: Java concurrency

SingletonIs That Thread Safe?

class Demo {

private static Collaborator collaborator;

public Collaborator getCollaborator() { if (collaborator == null) { collaborator = new Collaborator(); } return collaborator; }}

Page 15: Java concurrency

Thread Safety

Concurrency

Atomicity

Visibility

Volatile Patterns• One thread writes Other

Threads reads• Checking Status flags• Independent Observations

Volatile

synchronized

Page 16: Java concurrency

Thread Safeclass Demo {

private static Collaborator collaborator;

public synchronized Collaborator getCollaborator() { if (collaborator == null) { collaborator = new Collaborator(); } return collaborator; }}

Page 17: Java concurrency

Optimized Solutionclass Demo {

private static volatile Collaborator collaborator;

public Collaborator getCollaborator() { if (collaborator == null) {// double checked locking code synchronized(this) { if (collaborator == null) { collaborator = new Collaborator(); } } } return collaborator; }}

Page 18: Java concurrency

Even more Optimized

class Demo {

private static volatile Collaborator collaborator;

public Collaborator getCollaborator() { Collaborator tmp = collaborator; if (tmp == null) { synchronized(this) {

if (tmp == null) { tmp = new Collaborator(); collaborator = tmp; } } } return tmp; }}

Page 19: Java concurrency

Agenda Objective Facts Thread Safety Java Concurrency Problems (Symptoms) Java Concurrency methods

Page 20: Java concurrency

Java Concurrency Problems and Symptoms Invalid States

Stale values (old values, other threads values) Null pointer exceptions Array out of bound exceptions

Symptom Code works in Debugging while not working in the

running mode or production mode Working with JVM vendor while not working with other Working with certain usage load and doesn’t work

when the usage is increased (JIT Compiler)

Page 21: Java concurrency

Agenda Objective Facts Thread Safety Java Concurrency Problems

(Symptoms) Java Concurrency methods

Page 22: Java concurrency

Java Concurrency methods

Thread Confinement Stack Confinement Immutable Visibility Atomicity

Page 23: Java concurrency

Thread confinement (Not to share) If data is only accessed from a single thread,

no synchronization is needed.

JDBC Connections , confines the connection for a single thread

Implementation using ThreadLocal

Page 24: Java concurrency

Thread Confinementpublic class ConnectionDispenser { private static class ThreadLocalConnection extends ThreadLocal { public Object initialValue() { return DriverManager.

getConnection(ConfigurationSingleton.getDbUrl()); } }

private static ThreadLocalConnection conn = new ThreadLocalConnection();

public static Connection getConnection() { return (Connection) conn.get(); }}

Page 25: Java concurrency

Stack ConfinementNot to share Object can only reached using local

variables.

Widely used in our projects

public class Income extends HttpServlet {

@overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) {

try {

IncomeForm income = Util.jsonToObject(input, IncomeForm.class);

Page 26: Java concurrency

Immutable Fields are Final , values are set only from

the constructors, (this reference does not escaped)

Immutable V effectively Immutable (Fields are not declared final, but there are not setters methods )

Volatile Reference to Immutable Objects

Page 27: Java concurrency

Illustrative example@Immutableclass OneValueCache { private final BigInteger lastNumber; private final BigInteger[] lastFactors;

public OneValueCache(BigInteger i, BigInteger[] factors) { lastNumber = i; lastFactors = Arrays.copyOf(factors, factors.length); }

public BigInteger[] getFactors(BigInteger i) { if (lastNumber == null !lastNumber.equals(i)) return null; else return Arrays.copyOf(lastFactors, lastFactors.length); }}

Page 28: Java concurrency

Using volatile to publish immutable objects

@ThreadSafepublic class VolatileCachedFactorizer implements Servlet { private volatile OneValueCache cache = new OneValueCache(null, null);

public void service(ServletRequest req, ServletResponse resp) { BigInteger i = extractFromRequest(req); BigInteger[] factors = cache.getFactors(i); //Tip !!! if (factors == null) { factors = factor(i); cache = new OneValueCache(i, factors); } encodeIntoResponse(resp, factors); }}

Page 29: Java concurrency

Volatilevolatile boolean shutdownRequested;

...

public void shutdown() { shutdownRequested = true; }

public void doWork() { while (!shutdownRequested) { // do stuff }}

Page 30: Java concurrency

Synchronizedclass SynchronizedCounter { private int c = 0;

public synchronized void increment() { c++; }

public synchronized void decrement() { c--; }

public synchronized int value() { return c; }

}

Page 31: Java concurrency

Agenda Objective Facts Thread Safety Java Concurrency Problems

(Symptoms) Java Concurrency methods