SlideShare una empresa de Scribd logo
1 de 62
JavaJava
ConcurrencyConcurrency
●
Carol McDonaldCarol McDonald
–AvailityAvaility
. 4
Why Concurrency:
• Benefits of threads:
> Multitasking, Exploit multiple cores or CPUs
> Multi-threading good for blocking for I/O or other blocking ops
• Java < 1.5 concurrency primitives:
> wait(), notify(), sleep(), interrupt(),
synchronized
> Easy to use incorrectly
> Incorrect use can produce poor performance
. 5
Risks of Threads
• Safety hazards
> Syncronization, contention
• Liveness hazards
> Race conditions, deadlock
• Performance hazards
> Context switching overhead
. 6
Concurrency Utilities Goals
• set of concurrency building blocks
> library for concurrency like Collections for data structures
• Enhance scalability, performance, and thread safety
• Java Memory Model for Multi tasking
. 7
Do you see the error?
01 class PingPong {
02 public static synchronized void main(String[] a) {
03 Thread t = new Thread() {
04 public void run() {
05 pong();
06 }
07 };
08
09 t.run();
10 System.out.print("Ping");
11 }
12
13 static synchronized void pong() {
14 System.out.print("Pong");
15 }
16 }
. 9
What Does It Print?
(a) PingPong
(b) PongPing
(c) It varies
Not a multithreaded program!
. 10
Example How to start a thread
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
. 11
Another Look
01 class PingPong {
02 public static synchronized void main(String[] a) {
03 Thread t = new Thread() {
04 public void run() {
05 pong();
06 }
07 };
08
09 t.run(); // Common typo!
10 System.out.print("Ping");
11 }
12
13 static synchronized void pong() {
14 System.out.print("Pong");
15 }
16 }
. 12
How Do You Fix It?
01 class PingPong {
02 public static synchronized void main(String[] a) {
03 Thread t = new Thread() {
04 public void run() {
05 pong();
06 }
07 };
08
09 t.start();
10 System.out.print("Ping");
11 }
12
13 static synchronized void pong() {
14 System.out.print("Pong");
15 }
16 }
. 14
Concurrency Utilities:
• Task Scheduling Framework: Executor
interface replaces direct use of Thread
• Callable and Future
• Synchronisers
> Semaphore, CyclicBarrier, CountDownLatch
• Concurrent collections
• Lock
• Atomic, Visible
. 15
Concurrency Utilities:
• Task Scheduling Framework: Executor
• Separate task submission from
execution policy
• No more direct Thread invocation
> Use myExecutor.execute(aRunnable);
> Not new Thread(aRunnable).start();
public interface Executor {
void execute (Runnable command);
}
. 16
Executor
an object whose job it is to run Runnables:
public interface Executor {
void execute (Runnable command);
}
public interface ExecutorService extends Executor {
..
}
public class Executors {
static ExecutorService newFixedThreadPool(int poolSize);
...
}
Executor pool = Executors.newFixedThreadPool(5);
pool.execute (runnable);
manages the lifecycle of the execution service
static factory methods for Executor implementations
. 17
Creating Executors
• Factory methods in the Executors class
public class Executors {
static ExecutorService
newSingleThreadedExecutor();
static ExecutorService
newFixedThreadPool(int poolSize);
static ExecutorService
newCachedThreadPool();
static ScheduledExecutorService
newScheduledThreadPool();
// Other methods not listed
}
. 18
How not to manage tasks
class UnreliableWebServer {
public static void main(String[] args) {
ServerSocket socket = new ServerSocket(80);
while (true) {
final Socket connection = socket.accept();
Runnable r = new Runnable() {
public void run() {
handleRequest(connection);
}
};
// Don't do this!
new Thread(r).start();
}
}
}
Could create too many threads !
. 19
Thread Pool Example
class WebService {
public static void main(String[] args) {
Executor pool = Executors.newFixedThreadPool(5);
ServerSocket socket = new ServerSocket(999);
for (;;) {
final Socket connection = socket.accept();
Runnable task = new Runnable() {
public void run() {
new Handler().process(connection);
}
}
pool.execute (task);
}
}
}
class Handler { void process(Socket s); }
myExecutor.execute(aRunnable)
. 20
ExecutorService for Lifecycle Support
• ExecutorService supports graceful and
immediate shutdown
public interface ExecutorService extends
Executor {
void shutdown();
List<Runnable> shutdownNow();
boolean isShutdown();
boolean isTerminated();
boolean awaitTermination(long timeout,
TimeUnit unit);
// additional methods not listed
}
. 21
Callables and Futures
•Callable interface provides way to get a result
from a thread
●Implement call() method rather than run()
•Callable is submitted to ExecutorService
●Call submit() not execute()
●submit() returns a Future object
•Retrieve result using get() method of Future
object
●If result is ready it is returned
●If not ready calling thread will block
. 22
Callable Example
class CallableExample implements
Callable<String> {
public String call() {
String result = null;
/* Do some work and create a result */
return result;
}
}
. 23
Future Example
ExecutorService es =
Executors.newSingleThreadedExecutor();
Future<String> f =
es.submit (new CallableExample());
/* Do some work in parallel */
try {
String callableResult = f.get();
} catch (InterruptedException ie) {
/* Handle */
} catch (ExecutionException ee) {
/* Handle */
}
. 24
ScheduledExecutorService
• Deferred and recurring tasks
> Schedule execution of Callable or Runnable to run
once after a fixed delay
> schedule()
> Schedule a Runnable to run periodically at a fixed rate
> scheduleAtFixedRate()
> Schedule a Runnable to run periodically with a fixed delay
between executions
> scheduleWithFixedDelay()
• Submission returns a ScheduledFuture
> Can be used to cancel task
. 26
Recommendation
• Hold Locks for as short a time as possible
• Do not perform CPU intensive or I/O ops inside a
synchronized method or while locking
. 27
Synchronize Critical Section
• E.g., shared resource is an customer account. Certain methods called
by multiple threads.
• Hold monitor lock for as short a time as possible.
synchronized double getBalance() {
Account acct = verify(name, password);
return acct.balance;
} Lock held for long time
double getBalance() {
synchronized (this) {
Account acct = verify(name, password);
return acct.balance;
}
}
Current object is locked
Equivalent to above
double getBalance() {
Account acct = verify(name, password);
synchronized (acct) { return acct.balance};
}
Better
Only acct object is locked – for shorter tim
. 28
Locks
• Java provides basic locking via synchronized
• Good for many situations, but some issues
> Single monitor per object
> Not possible to interrupt thread waiting for lock
> Not possible to time-out when waiting for a lock
> Block structured approach
> Aquiring multiple locks is complex
> Advanced techniques not possible
• New Lock interface addresses these issues
. 29
Lock Interface
• No automatic unlocking
Interface Lock {
void lock();
void lockInterruptibly() throws IE;
boolean tryLock();
boolean tryLock(long t, TimeUnit u) throws IE;
//returns true if lock is aquired
void unlock();
Condition newCondition() throws
UnsupportedOperationException;
}
IE = InterruptedException
. 31
Lock Example
Lock lock = new RentrantLock();
public void accessProtectedResource()
throws IllegalMonitorStateException {
lock.lock();
try {
// Access lock protected resource
} finally {
// Ensure lock is always released
lock.unlock();
}
}
. 32
ReadWriteLock Interface
• Has two locks controlling read and write access
> Multiple threads can aquire the read lock if no threads have a
write lock
> Only one thread can aquire the write lock
> Methods to access locks
rwl.readLock().lock();
rwl.writeLock().lock();
> Better performance for read-mostly data access
. 33
ReadWriteLock Example
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
Lock r = rwl.readLock();
Lock w = rwl.writeLock();
ArrayList<String> data = new ArrayList<String>();
public String getData(int pos) {
r.lock();
try { return data.get(pos); }
finally { r.unlock(); }
}
public void addData(int pos, String value) {
w.lock();
try { data.add(pos, value); }
finally { w.unlock(); }
}
. 34
Synchronizers
• Co-ordinate access and control -can eliminate most
uses of wait or notify
• Semaphore
> Manages a fixed sized pool of resources
• CountDownLatch
> One or more threads wait for a set of threads to complete an
action
• CyclicBarrier
> Set of threads wait until they all reach a specified point
• Exchanger
> Two threads reach a fixed point and exchange data
. 35
Semaphore Example
private Semaphore available;
private Resource[] resources;
private boolean[] used;
public Resource(int poolSize) {
available = new Semaphore(poolSize);
/* Initialise resource pool */
}
public Resource getResource() {
try { available.aquire() } catch (IE) {}
}
public void returnResource(Resource r) {
/* Return resource to pool */
available.release();
}
. 36
Recommendation
• Prefer Concurrency utilities to wait and notify
. 37
BlockingQueue Interface
• thread safe Producer Consumer Pattern
Interface BlockingQueue<E> {
void put(E o) throws IE;
boolean offer(E o) throws IE;
boolean offer(E o, long t, TimeUnit u) throws IE;
E take() throws IE;
E poll() throws IE;
E poll(long t, TimeUnit u) throws IE;
int drainTo(Collection<? super E> c);
int drainTo(Collection<? super E> c, int max);
// Other methods not listed
}
. 39
Consumer Blocking Queue Example:
Logger thread
private BlockingQueue<String> msgQueue;
public Logger(BlockingQueue<String> mq) {
msgQueue = mq;
}
public void run() {
try {
while (true) {
String message = msgQueue.take();
/* Log message */
}
} catch (InterruptedException ie) { }
}
. 40
Producer Blocking Queue Example:
using the Logger thread
private ArrayBlockingQueue messageQueue =
new ArrayBlockingQueue<String>(10);
Logger logger = new Logger(messageQueue);
// runnables :
public void run() {
String someMessage;
try {
while (true) {
/* Do some processing */
/* Blocks if no space available */
messageQueue.put(someMessage);
}
} catch (InterruptedException ie) { }
}
. 41
Concurrent Collections
• ConcurrentMap (interface)
> Extends Map interface with atomic operations
• ConcurrentHashMap
> Fully concurrent retrieval
> Tunable concurrency for updates
> Constructor takes number of expected concurrent threads
• ConcurrentLinkedQueue
> Unbounded, thread safe queue, FIFO
• CopyOnWriteArrayList
> Optimised for frequent iteration, infrequent modifications
. 42
Java SE 6:
Concurrency Features
•Deque interface for double ended queues
>ArrayDeque, LinkedBlockingDeque,
ConcurrentLinkedDeque
•Concurrent skiplists
>ConcurrentSkipListMap,
ConcurrentSkipListSet
•AbstractQueuedLongSynchronizer
>Version of AbstractQueuedSynchronizer that uses a
long for internal state information
Concurrency:Concurrency:
Atomic VariablesAtomic Variables
. 44
Java Memory Model
• key ideas:
• all threads share the main
memory
• each thread uses a local
working memory
• refreshing local memory
to/from main memory must
comply to JMM rules
. 45
Java Memory Model
•
• Volatile
writing to a volatile forces a flush to
shared memory
• a read of volatile variable always
returns the most recent write by any
thread
. 46
Atomics
• java.util.concurrent.atomic
> classes that support lock-free thread-safe programming
on single variables
> use efficient machine level atomic processor instructions
> AtomicBoolean, AtomicInteger, AtomicLong,
AtomicLongArray,AtomicReference, AtomicReferenceArray
AtomicInteger balance = new AtomicInteger(0);
public int deposit(integer amount) {
return balance.addAndGet(amount);
}
. 47
Prefer immutable objects/data
•An immutable object is one whose
> State cannot be changed after construction
> All fields are private final , no setters
•Immutable objects are automatically thread-safe!
•Simpler
•Safer
> Can be freely shared
•More scalable
> No synchronization required
. 48
Multithreaded Lazy Initialization is tricky
public class Singleton {
private static Singleton instance;
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
private Singleton() {}
}
Shared
mutable
Non deterministic type bug can result
. 49
Eager Initialization
public class Singleton {
private static final Singleton instance=
new Singleton();
public static Singleton getInstance() {
return instance;
}
private Singleton() {}
}
immutable
. 50
Servlet Counter
public class HelloWithHits extends HttpServlet {
int hits; // Shared mutable variable (danger)
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
++hits; // Risks atomicity + visibility failure
...
}
}
. 51
Servlet Counter
public class HelloWithHits extends HttpServlet {
final AtomicInteger hits = new AtomicInteger();
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
hits.incrementAndGet();
...
}
}
. 52
Audit Web
• Find the shared variables
> Search all servlet classes for instance fields
> Search for all instances of getAttribute() on HttpSession or
ServletContext
> Search JSPs for session- or application-scoped beans
• For each, determine if it is mutable
> If so, can you replace it with an immutable object?
• Identify variables that will change at the same time
> Ensure that read/multiple write accesses are atomic
> One way is to encapsulate in an immutable holder
http://www.ibm.com/developerworks/library/j-jtp09238.html
. 53
Reduce locking
• Do As little work as possible inside
synchronized blocks
• guard different state with different locks
> reduces likelihood of lock contention
> (but disciplined order or deadlock)
. 54
Reduce locking
• reduce locking
> replace mutable objects with immutable ones
> replace shared objects with thread-local ones
> java.util.concurrent.atomic, and
> Concurrent collections in java.util.concurrent
. 55
VisualVM Monitoring threads
. 56
VisualVM
. 57
VisualVM
. 58
Trends
trend towards concurrent, asynchronous
computing
. 59
Actors
• message passing concurrency
• Share Nothing
• Communicate through messages
• Asynchronous non-blocking
60
EBay: Async Everywhere
Pattern: Event Queue
> Primary application writes data and queues event
> Consumers subscribe to event
62
Ebay: Partition Everything
Pattern: Functional Segmentation
> Segment processing into pools, services, and stages
> Segment data by entity and usage pattern
63
LinkedIn Best Practices
Storage and architecture
> Keep the system stateless

eBay, Google, etc.
> Partition data and services

Facebook, eBay
> Cache data
> Replicate your data
. 64
MySQL replication
• Writes replicated to In memory memcache for fast reads
master
slave
Resources
. 68
More Info/References
•Books
> Java Concurrency in Practice (Goetz, et al)
> See http://www.jcip.net
> Concurrent Programming in Java (Lea)
> Effective Java (Bloch)
• Articles
> http://www.ibm.com/developerworks/java/tutorials/j-
concur/section4.html
> http://blogs.sun.com/carolmcdonald/entry/some_concurr
ency_tips
. 69
References
• http://www.ibm.com/developerworks/library/j-jtp09238.html
• http://www.angelikalanger.com/
• Best Practices for Large-Scale Web Sites -- Lessons from eBay
http://java.sun.com/javaone/2009/articles/gen_ebay.jsp
• Lessons from Linked In
> http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS
• http://www.ibm.com/developerworks/library/j-jtp03304/
. 70
For More Information
• Memory management white paper
> http://java.sun.com/j2se/reference/whitepapers/
• Destructors, Finalizers, and Synchronization
> http://portal.acm.org/citation.cfm?id=604153
• Finalization, Threads, and the Java Technology
Memory Model
> http://developers.sun.com/learning/javaoneonline/2005/corep
latform/TS-3281.html
• Memory-retention due to finalization article
> http://www.devx.com/Java/Article/30192
. 71
For More Information
• FindBugs
> http://findbugs.sourceforge.net
• Heap analysis tools
> Monitoring and Management in 6.0
> http://java.sun.com/developer/technicalArticles/J2SE/monitoring/
> Troubleshooting guide
> http://java.sun.com/javase/6/webnotes/trouble/
> VisualVM
> http://download-
llnw.oracle.com/javase/6/docs/technotes/guides/visualvm/threads.html
> JConsole
> http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
Thank You!Thank You!
●
Carol McDonaldCarol McDonald
–

Más contenido relacionado

La actualidad más candente

Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in JavaAllan Huang
 
Java concurrency
Java concurrencyJava concurrency
Java concurrencyducquoc_vn
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningCarol McDonald
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkArun Mehra
 
Java New Evolution
Java New EvolutionJava New Evolution
Java New EvolutionAllan Huang
 
Java multi threading
Java multi threadingJava multi threading
Java multi threadingRaja Sekhar
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyAnton Keks
 
Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)choksheak
 
Java 8 - Stamped Lock
Java 8 - Stamped LockJava 8 - Stamped Lock
Java 8 - Stamped LockHaim Yadid
 
Thread syncronization
Thread syncronizationThread syncronization
Thread syncronizationpriyabogra1
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Inter threadcommunication.38
Inter threadcommunication.38Inter threadcommunication.38
Inter threadcommunication.38myrajendra
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 

La actualidad más candente (20)

The Java memory model made easy
The Java memory model made easyThe Java memory model made easy
The Java memory model made easy
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
 
Java New Evolution
Java New EvolutionJava New Evolution
Java New Evolution
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and Concurrency
 
Tech talk
Tech talkTech talk
Tech talk
 
Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)
 
Java 8 - Stamped Lock
Java 8 - Stamped LockJava 8 - Stamped Lock
Java 8 - Stamped Lock
 
Thread syncronization
Thread syncronizationThread syncronization
Thread syncronization
 
Java Concurrency
Java ConcurrencyJava Concurrency
Java Concurrency
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Inter threadcommunication.38
Inter threadcommunication.38Inter threadcommunication.38
Inter threadcommunication.38
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
 

Similar a Java Concurrency, Memory Model, and Trends

Fork and join framework
Fork and join frameworkFork and join framework
Fork and join frameworkMinh Tran
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in JavaRuben Inoto Soto
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrentRoger Xia
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/MultitaskingSasha Kravchuk
 
13multithreaded Programming
13multithreaded Programming13multithreaded Programming
13multithreaded ProgrammingAdil Jafri
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot NetNeeraj Kaushik
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading PresentationNeeraj Kaushik
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threadsYnon Perek
 
Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrencypriyank09
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent codeDror Helper
 
Core Java Programming Language (JSE) : Chapter XII - Threads
Core Java Programming Language (JSE) : Chapter XII -  ThreadsCore Java Programming Language (JSE) : Chapter XII -  Threads
Core Java Programming Language (JSE) : Chapter XII - ThreadsWebStackAcademy
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsAzul Systems, Inc.
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 

Similar a Java Concurrency, Memory Model, and Trends (20)

Fork and join framework
Fork and join frameworkFork and join framework
Fork and join framework
 
Concurrency-5.pdf
Concurrency-5.pdfConcurrency-5.pdf
Concurrency-5.pdf
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
 
Java Programming - 08 java threading
Java Programming - 08 java threadingJava Programming - 08 java threading
Java Programming - 08 java threading
 
Threads
ThreadsThreads
Threads
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrent
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
13multithreaded Programming
13multithreaded Programming13multithreaded Programming
13multithreaded Programming
 
Curator intro
Curator introCurator intro
Curator intro
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Java adv
Java advJava adv
Java adv
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
 
Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrency
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
Core Java Programming Language (JSE) : Chapter XII - Threads
Core Java Programming Language (JSE) : Chapter XII -  ThreadsCore Java Programming Language (JSE) : Chapter XII -  Threads
Core Java Programming Language (JSE) : Chapter XII - Threads
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM Mechanics
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Thread
ThreadThread
Thread
 

Más de Carol McDonald

Introduction to machine learning with GPUs
Introduction to machine learning with GPUsIntroduction to machine learning with GPUs
Introduction to machine learning with GPUsCarol McDonald
 
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...Carol McDonald
 
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DB
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DBAnalyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DB
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DBCarol McDonald
 
Analysis of Popular Uber Locations using Apache APIs: Spark Machine Learning...
Analysis of Popular Uber Locations using Apache APIs:  Spark Machine Learning...Analysis of Popular Uber Locations using Apache APIs:  Spark Machine Learning...
Analysis of Popular Uber Locations using Apache APIs: Spark Machine Learning...Carol McDonald
 
Predicting Flight Delays with Spark Machine Learning
Predicting Flight Delays with Spark Machine LearningPredicting Flight Delays with Spark Machine Learning
Predicting Flight Delays with Spark Machine LearningCarol McDonald
 
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DB
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DBStructured Streaming Data Pipeline Using Kafka, Spark, and MapR-DB
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DBCarol McDonald
 
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...Carol McDonald
 
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...Carol McDonald
 
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...Carol McDonald
 
How Big Data is Reducing Costs and Improving Outcomes in Health Care
How Big Data is Reducing Costs and Improving Outcomes in Health CareHow Big Data is Reducing Costs and Improving Outcomes in Health Care
How Big Data is Reducing Costs and Improving Outcomes in Health CareCarol McDonald
 
Demystifying AI, Machine Learning and Deep Learning
Demystifying AI, Machine Learning and Deep LearningDemystifying AI, Machine Learning and Deep Learning
Demystifying AI, Machine Learning and Deep LearningCarol McDonald
 
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...Carol McDonald
 
Streaming patterns revolutionary architectures
Streaming patterns revolutionary architectures Streaming patterns revolutionary architectures
Streaming patterns revolutionary architectures Carol McDonald
 
Spark machine learning predicting customer churn
Spark machine learning predicting customer churnSpark machine learning predicting customer churn
Spark machine learning predicting customer churnCarol McDonald
 
Fast Cars, Big Data How Streaming can help Formula 1
Fast Cars, Big Data How Streaming can help Formula 1Fast Cars, Big Data How Streaming can help Formula 1
Fast Cars, Big Data How Streaming can help Formula 1Carol McDonald
 
Applying Machine Learning to Live Patient Data
Applying Machine Learning to  Live Patient DataApplying Machine Learning to  Live Patient Data
Applying Machine Learning to Live Patient DataCarol McDonald
 
Streaming Patterns Revolutionary Architectures with the Kafka API
Streaming Patterns Revolutionary Architectures with the Kafka APIStreaming Patterns Revolutionary Architectures with the Kafka API
Streaming Patterns Revolutionary Architectures with the Kafka APICarol McDonald
 
Apache Spark Machine Learning Decision Trees
Apache Spark Machine Learning Decision TreesApache Spark Machine Learning Decision Trees
Apache Spark Machine Learning Decision TreesCarol McDonald
 
Advanced Threat Detection on Streaming Data
Advanced Threat Detection on Streaming DataAdvanced Threat Detection on Streaming Data
Advanced Threat Detection on Streaming DataCarol McDonald
 

Más de Carol McDonald (20)

Introduction to machine learning with GPUs
Introduction to machine learning with GPUsIntroduction to machine learning with GPUs
Introduction to machine learning with GPUs
 
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...
 
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DB
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DBAnalyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DB
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DB
 
Analysis of Popular Uber Locations using Apache APIs: Spark Machine Learning...
Analysis of Popular Uber Locations using Apache APIs:  Spark Machine Learning...Analysis of Popular Uber Locations using Apache APIs:  Spark Machine Learning...
Analysis of Popular Uber Locations using Apache APIs: Spark Machine Learning...
 
Predicting Flight Delays with Spark Machine Learning
Predicting Flight Delays with Spark Machine LearningPredicting Flight Delays with Spark Machine Learning
Predicting Flight Delays with Spark Machine Learning
 
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DB
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DBStructured Streaming Data Pipeline Using Kafka, Spark, and MapR-DB
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DB
 
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...
 
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...
 
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...
 
How Big Data is Reducing Costs and Improving Outcomes in Health Care
How Big Data is Reducing Costs and Improving Outcomes in Health CareHow Big Data is Reducing Costs and Improving Outcomes in Health Care
How Big Data is Reducing Costs and Improving Outcomes in Health Care
 
Demystifying AI, Machine Learning and Deep Learning
Demystifying AI, Machine Learning and Deep LearningDemystifying AI, Machine Learning and Deep Learning
Demystifying AI, Machine Learning and Deep Learning
 
Spark graphx
Spark graphxSpark graphx
Spark graphx
 
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...
 
Streaming patterns revolutionary architectures
Streaming patterns revolutionary architectures Streaming patterns revolutionary architectures
Streaming patterns revolutionary architectures
 
Spark machine learning predicting customer churn
Spark machine learning predicting customer churnSpark machine learning predicting customer churn
Spark machine learning predicting customer churn
 
Fast Cars, Big Data How Streaming can help Formula 1
Fast Cars, Big Data How Streaming can help Formula 1Fast Cars, Big Data How Streaming can help Formula 1
Fast Cars, Big Data How Streaming can help Formula 1
 
Applying Machine Learning to Live Patient Data
Applying Machine Learning to  Live Patient DataApplying Machine Learning to  Live Patient Data
Applying Machine Learning to Live Patient Data
 
Streaming Patterns Revolutionary Architectures with the Kafka API
Streaming Patterns Revolutionary Architectures with the Kafka APIStreaming Patterns Revolutionary Architectures with the Kafka API
Streaming Patterns Revolutionary Architectures with the Kafka API
 
Apache Spark Machine Learning Decision Trees
Apache Spark Machine Learning Decision TreesApache Spark Machine Learning Decision Trees
Apache Spark Machine Learning Decision Trees
 
Advanced Threat Detection on Streaming Data
Advanced Threat Detection on Streaming DataAdvanced Threat Detection on Streaming Data
Advanced Threat Detection on Streaming Data
 

Último

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Último (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Java Concurrency, Memory Model, and Trends

  • 2. . 4 Why Concurrency: • Benefits of threads: > Multitasking, Exploit multiple cores or CPUs > Multi-threading good for blocking for I/O or other blocking ops • Java < 1.5 concurrency primitives: > wait(), notify(), sleep(), interrupt(), synchronized > Easy to use incorrectly > Incorrect use can produce poor performance
  • 3. . 5 Risks of Threads • Safety hazards > Syncronization, contention • Liveness hazards > Race conditions, deadlock • Performance hazards > Context switching overhead
  • 4. . 6 Concurrency Utilities Goals • set of concurrency building blocks > library for concurrency like Collections for data structures • Enhance scalability, performance, and thread safety • Java Memory Model for Multi tasking
  • 5. . 7 Do you see the error? 01 class PingPong { 02 public static synchronized void main(String[] a) { 03 Thread t = new Thread() { 04 public void run() { 05 pong(); 06 } 07 }; 08 09 t.run(); 10 System.out.print("Ping"); 11 } 12 13 static synchronized void pong() { 14 System.out.print("Pong"); 15 } 16 }
  • 6. . 9 What Does It Print? (a) PingPong (b) PongPing (c) It varies Not a multithreaded program!
  • 7. . 10 Example How to start a thread public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); } }
  • 8. . 11 Another Look 01 class PingPong { 02 public static synchronized void main(String[] a) { 03 Thread t = new Thread() { 04 public void run() { 05 pong(); 06 } 07 }; 08 09 t.run(); // Common typo! 10 System.out.print("Ping"); 11 } 12 13 static synchronized void pong() { 14 System.out.print("Pong"); 15 } 16 }
  • 9. . 12 How Do You Fix It? 01 class PingPong { 02 public static synchronized void main(String[] a) { 03 Thread t = new Thread() { 04 public void run() { 05 pong(); 06 } 07 }; 08 09 t.start(); 10 System.out.print("Ping"); 11 } 12 13 static synchronized void pong() { 14 System.out.print("Pong"); 15 } 16 }
  • 10. . 14 Concurrency Utilities: • Task Scheduling Framework: Executor interface replaces direct use of Thread • Callable and Future • Synchronisers > Semaphore, CyclicBarrier, CountDownLatch • Concurrent collections • Lock • Atomic, Visible
  • 11. . 15 Concurrency Utilities: • Task Scheduling Framework: Executor • Separate task submission from execution policy • No more direct Thread invocation > Use myExecutor.execute(aRunnable); > Not new Thread(aRunnable).start(); public interface Executor { void execute (Runnable command); }
  • 12. . 16 Executor an object whose job it is to run Runnables: public interface Executor { void execute (Runnable command); } public interface ExecutorService extends Executor { .. } public class Executors { static ExecutorService newFixedThreadPool(int poolSize); ... } Executor pool = Executors.newFixedThreadPool(5); pool.execute (runnable); manages the lifecycle of the execution service static factory methods for Executor implementations
  • 13. . 17 Creating Executors • Factory methods in the Executors class public class Executors { static ExecutorService newSingleThreadedExecutor(); static ExecutorService newFixedThreadPool(int poolSize); static ExecutorService newCachedThreadPool(); static ScheduledExecutorService newScheduledThreadPool(); // Other methods not listed }
  • 14. . 18 How not to manage tasks class UnreliableWebServer { public static void main(String[] args) { ServerSocket socket = new ServerSocket(80); while (true) { final Socket connection = socket.accept(); Runnable r = new Runnable() { public void run() { handleRequest(connection); } }; // Don't do this! new Thread(r).start(); } } } Could create too many threads !
  • 15. . 19 Thread Pool Example class WebService { public static void main(String[] args) { Executor pool = Executors.newFixedThreadPool(5); ServerSocket socket = new ServerSocket(999); for (;;) { final Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { new Handler().process(connection); } } pool.execute (task); } } } class Handler { void process(Socket s); } myExecutor.execute(aRunnable)
  • 16. . 20 ExecutorService for Lifecycle Support • ExecutorService supports graceful and immediate shutdown public interface ExecutorService extends Executor { void shutdown(); List<Runnable> shutdownNow(); boolean isShutdown(); boolean isTerminated(); boolean awaitTermination(long timeout, TimeUnit unit); // additional methods not listed }
  • 17. . 21 Callables and Futures •Callable interface provides way to get a result from a thread ●Implement call() method rather than run() •Callable is submitted to ExecutorService ●Call submit() not execute() ●submit() returns a Future object •Retrieve result using get() method of Future object ●If result is ready it is returned ●If not ready calling thread will block
  • 18. . 22 Callable Example class CallableExample implements Callable<String> { public String call() { String result = null; /* Do some work and create a result */ return result; } }
  • 19. . 23 Future Example ExecutorService es = Executors.newSingleThreadedExecutor(); Future<String> f = es.submit (new CallableExample()); /* Do some work in parallel */ try { String callableResult = f.get(); } catch (InterruptedException ie) { /* Handle */ } catch (ExecutionException ee) { /* Handle */ }
  • 20. . 24 ScheduledExecutorService • Deferred and recurring tasks > Schedule execution of Callable or Runnable to run once after a fixed delay > schedule() > Schedule a Runnable to run periodically at a fixed rate > scheduleAtFixedRate() > Schedule a Runnable to run periodically with a fixed delay between executions > scheduleWithFixedDelay() • Submission returns a ScheduledFuture > Can be used to cancel task
  • 21. . 26 Recommendation • Hold Locks for as short a time as possible • Do not perform CPU intensive or I/O ops inside a synchronized method or while locking
  • 22. . 27 Synchronize Critical Section • E.g., shared resource is an customer account. Certain methods called by multiple threads. • Hold monitor lock for as short a time as possible. synchronized double getBalance() { Account acct = verify(name, password); return acct.balance; } Lock held for long time double getBalance() { synchronized (this) { Account acct = verify(name, password); return acct.balance; } } Current object is locked Equivalent to above double getBalance() { Account acct = verify(name, password); synchronized (acct) { return acct.balance}; } Better Only acct object is locked – for shorter tim
  • 23. . 28 Locks • Java provides basic locking via synchronized • Good for many situations, but some issues > Single monitor per object > Not possible to interrupt thread waiting for lock > Not possible to time-out when waiting for a lock > Block structured approach > Aquiring multiple locks is complex > Advanced techniques not possible • New Lock interface addresses these issues
  • 24. . 29 Lock Interface • No automatic unlocking Interface Lock { void lock(); void lockInterruptibly() throws IE; boolean tryLock(); boolean tryLock(long t, TimeUnit u) throws IE; //returns true if lock is aquired void unlock(); Condition newCondition() throws UnsupportedOperationException; } IE = InterruptedException
  • 25. . 31 Lock Example Lock lock = new RentrantLock(); public void accessProtectedResource() throws IllegalMonitorStateException { lock.lock(); try { // Access lock protected resource } finally { // Ensure lock is always released lock.unlock(); } }
  • 26. . 32 ReadWriteLock Interface • Has two locks controlling read and write access > Multiple threads can aquire the read lock if no threads have a write lock > Only one thread can aquire the write lock > Methods to access locks rwl.readLock().lock(); rwl.writeLock().lock(); > Better performance for read-mostly data access
  • 27. . 33 ReadWriteLock Example ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); Lock r = rwl.readLock(); Lock w = rwl.writeLock(); ArrayList<String> data = new ArrayList<String>(); public String getData(int pos) { r.lock(); try { return data.get(pos); } finally { r.unlock(); } } public void addData(int pos, String value) { w.lock(); try { data.add(pos, value); } finally { w.unlock(); } }
  • 28. . 34 Synchronizers • Co-ordinate access and control -can eliminate most uses of wait or notify • Semaphore > Manages a fixed sized pool of resources • CountDownLatch > One or more threads wait for a set of threads to complete an action • CyclicBarrier > Set of threads wait until they all reach a specified point • Exchanger > Two threads reach a fixed point and exchange data
  • 29. . 35 Semaphore Example private Semaphore available; private Resource[] resources; private boolean[] used; public Resource(int poolSize) { available = new Semaphore(poolSize); /* Initialise resource pool */ } public Resource getResource() { try { available.aquire() } catch (IE) {} } public void returnResource(Resource r) { /* Return resource to pool */ available.release(); }
  • 30. . 36 Recommendation • Prefer Concurrency utilities to wait and notify
  • 31. . 37 BlockingQueue Interface • thread safe Producer Consumer Pattern Interface BlockingQueue<E> { void put(E o) throws IE; boolean offer(E o) throws IE; boolean offer(E o, long t, TimeUnit u) throws IE; E take() throws IE; E poll() throws IE; E poll(long t, TimeUnit u) throws IE; int drainTo(Collection<? super E> c); int drainTo(Collection<? super E> c, int max); // Other methods not listed }
  • 32. . 39 Consumer Blocking Queue Example: Logger thread private BlockingQueue<String> msgQueue; public Logger(BlockingQueue<String> mq) { msgQueue = mq; } public void run() { try { while (true) { String message = msgQueue.take(); /* Log message */ } } catch (InterruptedException ie) { } }
  • 33. . 40 Producer Blocking Queue Example: using the Logger thread private ArrayBlockingQueue messageQueue = new ArrayBlockingQueue<String>(10); Logger logger = new Logger(messageQueue); // runnables : public void run() { String someMessage; try { while (true) { /* Do some processing */ /* Blocks if no space available */ messageQueue.put(someMessage); } } catch (InterruptedException ie) { } }
  • 34. . 41 Concurrent Collections • ConcurrentMap (interface) > Extends Map interface with atomic operations • ConcurrentHashMap > Fully concurrent retrieval > Tunable concurrency for updates > Constructor takes number of expected concurrent threads • ConcurrentLinkedQueue > Unbounded, thread safe queue, FIFO • CopyOnWriteArrayList > Optimised for frequent iteration, infrequent modifications
  • 35. . 42 Java SE 6: Concurrency Features •Deque interface for double ended queues >ArrayDeque, LinkedBlockingDeque, ConcurrentLinkedDeque •Concurrent skiplists >ConcurrentSkipListMap, ConcurrentSkipListSet •AbstractQueuedLongSynchronizer >Version of AbstractQueuedSynchronizer that uses a long for internal state information
  • 37. . 44 Java Memory Model • key ideas: • all threads share the main memory • each thread uses a local working memory • refreshing local memory to/from main memory must comply to JMM rules
  • 38. . 45 Java Memory Model • • Volatile writing to a volatile forces a flush to shared memory • a read of volatile variable always returns the most recent write by any thread
  • 39. . 46 Atomics • java.util.concurrent.atomic > classes that support lock-free thread-safe programming on single variables > use efficient machine level atomic processor instructions > AtomicBoolean, AtomicInteger, AtomicLong, AtomicLongArray,AtomicReference, AtomicReferenceArray AtomicInteger balance = new AtomicInteger(0); public int deposit(integer amount) { return balance.addAndGet(amount); }
  • 40. . 47 Prefer immutable objects/data •An immutable object is one whose > State cannot be changed after construction > All fields are private final , no setters •Immutable objects are automatically thread-safe! •Simpler •Safer > Can be freely shared •More scalable > No synchronization required
  • 41. . 48 Multithreaded Lazy Initialization is tricky public class Singleton { private static Singleton instance; public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } private Singleton() {} } Shared mutable Non deterministic type bug can result
  • 42. . 49 Eager Initialization public class Singleton { private static final Singleton instance= new Singleton(); public static Singleton getInstance() { return instance; } private Singleton() {} } immutable
  • 43. . 50 Servlet Counter public class HelloWithHits extends HttpServlet { int hits; // Shared mutable variable (danger) protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { ++hits; // Risks atomicity + visibility failure ... } }
  • 44. . 51 Servlet Counter public class HelloWithHits extends HttpServlet { final AtomicInteger hits = new AtomicInteger(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { hits.incrementAndGet(); ... } }
  • 45. . 52 Audit Web • Find the shared variables > Search all servlet classes for instance fields > Search for all instances of getAttribute() on HttpSession or ServletContext > Search JSPs for session- or application-scoped beans • For each, determine if it is mutable > If so, can you replace it with an immutable object? • Identify variables that will change at the same time > Ensure that read/multiple write accesses are atomic > One way is to encapsulate in an immutable holder http://www.ibm.com/developerworks/library/j-jtp09238.html
  • 46. . 53 Reduce locking • Do As little work as possible inside synchronized blocks • guard different state with different locks > reduces likelihood of lock contention > (but disciplined order or deadlock)
  • 47. . 54 Reduce locking • reduce locking > replace mutable objects with immutable ones > replace shared objects with thread-local ones > java.util.concurrent.atomic, and > Concurrent collections in java.util.concurrent
  • 51. . 58 Trends trend towards concurrent, asynchronous computing
  • 52. . 59 Actors • message passing concurrency • Share Nothing • Communicate through messages • Asynchronous non-blocking
  • 53. 60 EBay: Async Everywhere Pattern: Event Queue > Primary application writes data and queues event > Consumers subscribe to event
  • 54. 62 Ebay: Partition Everything Pattern: Functional Segmentation > Segment processing into pools, services, and stages > Segment data by entity and usage pattern
  • 55. 63 LinkedIn Best Practices Storage and architecture > Keep the system stateless  eBay, Google, etc. > Partition data and services  Facebook, eBay > Cache data > Replicate your data
  • 56. . 64 MySQL replication • Writes replicated to In memory memcache for fast reads master slave
  • 58. . 68 More Info/References •Books > Java Concurrency in Practice (Goetz, et al) > See http://www.jcip.net > Concurrent Programming in Java (Lea) > Effective Java (Bloch) • Articles > http://www.ibm.com/developerworks/java/tutorials/j- concur/section4.html > http://blogs.sun.com/carolmcdonald/entry/some_concurr ency_tips
  • 59. . 69 References • http://www.ibm.com/developerworks/library/j-jtp09238.html • http://www.angelikalanger.com/ • Best Practices for Large-Scale Web Sites -- Lessons from eBay http://java.sun.com/javaone/2009/articles/gen_ebay.jsp • Lessons from Linked In > http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS • http://www.ibm.com/developerworks/library/j-jtp03304/
  • 60. . 70 For More Information • Memory management white paper > http://java.sun.com/j2se/reference/whitepapers/ • Destructors, Finalizers, and Synchronization > http://portal.acm.org/citation.cfm?id=604153 • Finalization, Threads, and the Java Technology Memory Model > http://developers.sun.com/learning/javaoneonline/2005/corep latform/TS-3281.html • Memory-retention due to finalization article > http://www.devx.com/Java/Article/30192
  • 61. . 71 For More Information • FindBugs > http://findbugs.sourceforge.net • Heap analysis tools > Monitoring and Management in 6.0 > http://java.sun.com/developer/technicalArticles/J2SE/monitoring/ > Troubleshooting guide > http://java.sun.com/javase/6/webnotes/trouble/ > VisualVM > http://download- llnw.oracle.com/javase/6/docs/technotes/guides/visualvm/threads.html > JConsole > http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
  • 62. Thank You!Thank You! ● Carol McDonaldCarol McDonald –

Notas del editor

  1. Lets look at some of the changes and new features in the Java Virtual Machine
  2. Previously Java has supported several very primitive mechanisms for co-ordinating a number of threads. The synchronized keyword can be applied to methods or code blocks and the Thread class supports the wait, notify, sleep and interrupt mothods. The problem with these primitive mechanisms is that they are just that: primitive. As we will see the new utilities will greatly enhance Java applications in terms of scalability, performance, readability and thread safety.
  3. The idea behind the new concurrency utilities is to provide a much richer set of functions that programmers can use to create simple and powerful multi-threaded applications, in the same way that the Collections classes provided a much richer set of data structure based APIs. By providing much finer granularity of locking and different approaches such as multiple read-single write locks a secondary aim is to enable the performance of a multi-threaded Java application to match or exceed that of native C applications in the high-end server environment. Previously Java has supported several very primitive mechanisms for co-ordinating a number of threads. The synchronized keyword can be applied to methods or code blocks and the Thread class supports the wait, notify, sleep and interrupt mothods. The problem with these primitive mechanisms is that they are just that: primitive. As we will see the new utilities will greatly enhance Java applications in terms of scalability, performance, readability and thread safety.
  4. Here is a list of things we&amp;apos;ll talk about in this session. This is not an exhaustive list of all the new features, but given the limited time we have this should give you a good grasp of the main areas of functionality. One of the main changes in using the new concurrency utilities is the concept of moving away from interacting directly with a thread object. As we&amp;apos;ll see the new and preferred way is through an interface called ExecutorService. There are several factory methods available to easily provide the programmer with standardised mechanisms for the Executor such as thread pools, single thread and priority threads. # Task Scheduling Framework - The Executor framework is a framework for standardizing invocation, scheduling, execution, and control of asynchronous tasks according to a set of execution policies. Implementations are provided that allow tasks to be executed within the submitting thread, in a single background thread (as with events in Swing), in a newly created thread, or in a thread pool, and developers can create of Executor supporting arbitrary execution policies. The built-in implementations offer configurable policies such as queue length limits and saturation policy which can improve the stability of applications by preventing runaway resource consumption. # Concurrent Collections - Several new Collections classes have been added, including the new Queue and BlockingQueue interfaces, and high-performance, concurrent implementations of Map, List, and Queue. Until now it has required relatively complex coding to allow a child thread to return a result to the its parent. This is even more complex when it is necessary to synchonize the threads so that the parent can only continue when the child has completed generatijng the result. This becomes very simple now through the use of Callable and Future. Semaphores are a well understood mechanism that are now supported on Java. BlockingQueues allow simple data structures to be used by multiple threads in a concurrent way such that the programmer is not responsible for ensuring safe concurrent access. Lastly the idea of an Atomic variable that can safely be accessed and modified is also iincluded in the concurrency utilities.
  5. Here is a list of things we&amp;apos;ll talk about in this session. This is not an exhaustive list of all the new features, but given the limited time we have this should give you a good grasp of the main areas of functionality. One of the main changes in using the new concurrency utilities is the concept of moving away from interacting directly with a thread object. As we&amp;apos;ll see the new and preferred way is through an interface called ExecutorService. There are several factory methods available to easily provide the programmer with standardised mechanisms for the Executor such as thread pools, single thread and priority threads. # Task Scheduling Framework - The Executor framework is a framework for standardizing invocation, scheduling, execution, and control of asynchronous tasks according to a set of execution policies. Implementations are provided that allow tasks to be executed within the submitting thread, in a single background thread (as with events in Swing), in a newly created thread, or in a thread pool, and developers can create of Executor supporting arbitrary execution policies. The built-in implementations offer configurable policies such as queue length limits and saturation policy which can improve the stability of applications by preventing runaway resource consumption. # Concurrent Collections - Several new Collections classes have been added, including the new Queue and BlockingQueue interfaces, and high-performance, concurrent implementations of Map, List, and Queue. Until now it has required relatively complex coding to allow a child thread to return a result to the its parent. This is even more complex when it is necessary to synchonize the threads so that the parent can only continue when the child has completed generatijng the result. This becomes very simple now through the use of Callable and Future. Semaphores are a well understood mechanism that are now supported on Java. BlockingQueues allow simple data structures to be used by multiple threads in a concurrent way such that the programmer is not responsible for ensuring safe concurrent access. Lastly the idea of an Atomic variable that can safely be accessed and modified is also iincluded in the concurrency utilities.
  6. As mentioned earlier programmers should now not interact directly with the Thread class. Before, we would create a class that implemented the Runnable interface. To start this in a new thread we would use a line like this: create a new thread with using the Runnable class and call the start method that would in turn call the run method in our class. This is still quite correct, but the idea is to replace this with an abstracted interface, Executor. Instead of calling start we call execute. Since this is abstracted away from the Thread class it becomes a simple task to change the way we handle the threading should we wish to do so at a later date. For example, we could start with a piece of code that creates a single thread to execute our new code. As requirements and processing power change we find that we need to run a number of threads for our class. We can simply change the factory method we use to create a thread pool and we are then able to use the same class in a number of threads rather than just one.
  7. Here is an example of code that uses the new Executor, Executors and ExecutorService classes. The example is a standard web service class that needs to handle a number of incoming connections simultaneously through a number of separate threads. The number of threads needs to be bounded to prevent the system from running out of resources when the load becomes too high. Previously it would have been necessary to create your own thread pooling class that would create a set of threads and then manage all of the alloaction and deallocation of those threads with all of the required concurrent access controls. With the concurrency utilities this is all provided by default. In the main routine we initialise a new fixed thread pool with a size of 7. We use the newFixedThreadPool method of the Executors class. This will return a ExecutorService object. Since ExecutorService implements the Executor interface we can assign it to an Executor object reference. To handle an incoming connection we simply call execute on our pool object passing it a Runnable object (which in this case is defined through an inner class). The run method does whatever work we need the thread to do. Whenever connections come in they will be allocates a thread from the pool. When the run method completes the thread will automatically be returned to the pool. If a connection comes in and all threads are in use the main loop will block until a thread is freed.
  8. If a new Thread is started in an application there is currently no way to return a result from that thread to the thread that started it without the use of a shared variable and appropriate synchronization. This is complex and makes code harder to understand and maintain. The Callable interface allows a result to be returned easily to a parent thread or an exception to be sent to indicate some problem. To use a Callable you define a class that implements the Callable interface, in the same way that you would define a class that implements Runnable for a new thread. The Callable interface defines one method, call. The return type of the call method is defined by the type argument of the Callable interface specified for the class definition, i.e. Callable is a generic interface. We will see how this works in reality in the next example. The call method contains whatever instructions are required to generate the result that will be returned. The Callable is passed to an ExecutorService through the submit method, rather than using the execute method of the Executor interface. The submit method will return a Future object immediately. The parent thread can continue concurrently with the Callable. When the parent thread requires the result of the Callable it calls the get method of the Future object. If the Callable has completed the result will be returned immediatley. If the Callable has not completed its work, the parent thread will block until the result is available. The submit method may also be called with a Runnable rather than a Callable. In this case get will always return null (but may block until the Runnable has completed its work). There is another form of the submit method that takes a Runnable as an argument and also takes a result as an argument. When the Runnable completes the result will be returned from the Future via the get method. This is useful if the Future is to be passed to another method.
  9. This shows a simple example of a Callable object. Since Callable is generic we specify the type argument of Callable here as a String, so that the return type of the call method is also String. The call method will do some work to generate a result and then return the appropriate String.
  10. Here we see an example of the use of the CallableExample with a Future. Firstly we create a new ExecutorService using a factory method from the Executors class. Next we submit a new CallableExample to the ExecutorService. This returns a Future which we must specify the type argument of. Obviously this must be the same type as the type argument of the Callable (which is String). If we did not we would get a compiler error. After we do some work in this thread we need to access the result of the CallableExample thread. To do this we simply call f.get. If the call method has got to the return statement we will immediately get the result. If not this thread will block until the call method of the CallableExample object calls return. We handle exceptions as appropriate. An ExecutionException is thrown if the call method threw an exception rather than returning a result. The cause of the exception can be determined via the Throwable.getCause() method.
  11. The Lock interface provides more extensive locking operations than using a synchronized block. Because we are using methods of a class to explicitly perform the lock and unlock operations the programmer needs to be more careful in its use. With a synchronized block of code it is impossible to forget to unlock it. Once the code execution leaves that block whether normally or through some for of exception, the lock is released. Using the Lock class the programmer must typically use a try-finally construct and put the unlock call within the finally clause to ensure that the lock is always released. One advantage of the Lock interface over synchronized is the ability to not block if a lock is not available. The tryLock method will always return immediately, returning true if the lock was aquired or false if not. This method may also be called with a timeout parameter so the thread will only block for the specified time if the lock is not aquired. ReentrantLock provides a concrete implementation of the Lock interface. The thread that holds the lock can call the lock method multiple times without blocking. This is especially useful in recursive code that needs to protect access to a certain section of code.
  12. ReentrantLock provides a concrete implementation of the Lock interface. The thread that holds the lock can call the lock method multiple times without blocking. This is especially useful in recursive code that needs to protect access to a certain section of code.
  13. The ReadWriteLock permits multiple threads to have read access to a protected piece of code, but only one thread may access the code in write mode. Effectively the ReadWriteLock consists of two locks that are implemented as inner classes. If a thread aquires the read lock other threads may also aquire the read lock. If a read lock is held no thread may aquire the write lock until all read locks have been released. If a thread holds the write lock, no thread may aquire either the write lock or a read lock. ReadWriteLock is an interface. RentrantReadWriteLock is a concrete implementation of this interface.
  14. Here we see an example of the use of a semaphore. We want to control access to a fixed size pool of resources so that multiple threads can request the use of a resource and return it when they have finished with it. Since there will be multiple threads involved we need to ensure thread safety. The semaphore can do this for us in a simple way. In the creator we create a new semaphore with the same size as the pool of resources we&amp;apos;re creating. When a thread requests a resource we call the aquire method of the semaphore to see if we can aquire a permit to use a resource. If there are resources available this will return and a resource will be returned from the pool. If all the resources are in use the call to aquire will block until another thread calls release on the semaphore. When a thread finishes with a resource the resource is returned to the pool and the release method is called. Both aquire and release can be considered atomic operations. NOTE: This is not a complete code sample, it only demonstrates the use of the semaphore. How the next resource is allocated and resources are returned to the pool would also need to be made thread safe using additional code.
  15. The BlockingQueue interface is a Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element. A BlockingQueue does not accept null elements. ArrayBlockingQueue is a concrete implementation of the BlockingQueue interface. An ArrayBlockingQueue is a bounded blocking queue backed by an array. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. The Blocking queue provides methods to insert and remove objects from the queue (it is generic so can be typed). put() adds the specified element to the tail of this queue, waiting if necessary for space to become available. offer() inserts the specified element at the tail of this queue if possible, returning immediately if this queue is full. peek() retrieves, but does not remove, the head of this queue, returning null if this queue is empty. take() retrieves and removes the head of this queue, waiting if no elements are present on this queue. poll() retrieves and removes the head of this queue, null if this queue is empty. poll can also be specified with a time out so that the call will wait if necessary up to the specified wait time if no elements are present on this queue.
  16. Here is an example of the use of a BlockingQueue. The class implements a logger that will be used by a number of threads to record information. The constructor takes a BlockingQueue (with type argument String). In the run method messages are retrieved from the queue using the take method. When the queue is empty the logging thread will block until messages become available. Once a message is retrieved it can be logged in whichever way is required.
  17. Having defined our logging thread here is a class that uses the logger. A new ArrayBlockingQueue is instantiated with a type argument of String and a size of 10 elements. This is passed to the logger constructor as required. We can now start a number of threads that use this logger to record messages. We use the put method on the messageQueue. If the queue is full the thread will block until the logger has removed messages. The BlockingQueue will handle contention in a thread safe way should multiple threads be waiting for space to become available in the queue.
  18. Welcome to “AJAX Basics” presentation. My name is Sang Shin. I am Java technology architect and evangelist from Sun Microsystems.
  19. The java.util.concurrent.atomic package provides a small toolkit of classes that support lock-free thread-safe programming of single variables. In essence, the classes in this package extend the notion of volatile values, fields, and array elements to those that also provide an atomic conditional update operation of the form: boolean compareAndSet(expectedValue, updateValue); This method (which varies in argument types across different classes) atomically sets a variable to the updateValue if it currently holds the expectedValue, reporting true on success. The classes in this package also contain methods to get and unconditionally set values, as well as a weaker conditional atomic update operation weakCompareAndSet. The weak version may be more efficient in the normal case, but differs in that any given invocation of weakCompareAndSet method may fail, even spuriously (that is, for no apparent reason). A false return means only that the operation may be retried if desired, relying on the guarantee that repeated invocation when the variable holds expectedValue and no other thread is also attempting to set the variable will eventually succeed. The specifications of these methods enable implementations to employ efficient machine-level atomic instructions that are available on contemporary processors. However on some platforms, support may entail some form of internal locking. Thus the methods are not strictly guaranteed to be non-blocking -- a thread may block transiently before performing the operation. Here we see a simple example of the use of an AtomicInteger object for a bank balance.
  20. The java.util.concurrent.atomic package provides a small toolkit of classes that support lock-free thread-safe programming of single variables. In essence, the classes in this package extend the notion of volatile values, fields, and array elements to those that also provide an atomic conditional update operation of the form: boolean compareAndSet(expectedValue, updateValue); This method (which varies in argument types across different classes) atomically sets a variable to the updateValue if it currently holds the expectedValue, reporting true on success. The classes in this package also contain methods to get and unconditionally set values, as well as a weaker conditional atomic update operation weakCompareAndSet. The weak version may be more efficient in the normal case, but differs in that any given invocation of weakCompareAndSet method may fail, even spuriously (that is, for no apparent reason). A false return means only that the operation may be retried if desired, relying on the guarantee that repeated invocation when the variable holds expectedValue and no other thread is also attempting to set the variable will eventually succeed. The specifications of these methods enable implementations to employ efficient machine-level atomic instructions that are available on contemporary processors. However on some platforms, support may entail some form of internal locking. Thus the methods are not strictly guaranteed to be non-blocking -- a thread may block transiently before performing the operation. Here we see a simple example of the use of an AtomicInteger object for a bank balance.
  21. The java.util.concurrent.atomic package provides a small toolkit of classes that support lock-free thread-safe programming of single variables. In essence, the classes in this package extend the notion of volatile values, fields, and array elements to those that also provide an atomic conditional update operation of the form: boolean compareAndSet(expectedValue, updateValue); This method (which varies in argument types across different classes) atomically sets a variable to the updateValue if it currently holds the expectedValue, reporting true on success. The classes in this package also contain methods to get and unconditionally set values, as well as a weaker conditional atomic update operation weakCompareAndSet. The weak version may be more efficient in the normal case, but differs in that any given invocation of weakCompareAndSet method may fail, even spuriously (that is, for no apparent reason). A false return means only that the operation may be retried if desired, relying on the guarantee that repeated invocation when the variable holds expectedValue and no other thread is also attempting to set the variable will eventually succeed. The specifications of these methods enable implementations to employ efficient machine-level atomic instructions that are available on contemporary processors. However on some platforms, support may entail some form of internal locking. Thus the methods are not strictly guaranteed to be non-blocking -- a thread may block transiently before performing the operation. Here we see a simple example of the use of an AtomicInteger object for a bank balance.
  22. Here is an example of the use of a BlockingQueue. The class implements a logger that will be used by a number of threads to record information. The constructor takes a BlockingQueue (with type argument String). In the run method messages are retrieved from the queue using the take method. When the queue is empty the logging thread will block until messages become available. Once a message is retrieved it can be logged in whichever way is required.
  23. Here is an example of the use of a BlockingQueue. The class implements a logger that will be used by a number of threads to record information. The constructor takes a BlockingQueue (with type argument String). In the run method messages are retrieved from the queue using the take method. When the queue is empty the logging thread will block until messages become available. Once a message is retrieved it can be logged in whichever way is required.
  24. Here is an example of the use of a BlockingQueue. The class implements a logger that will be used by a number of threads to record information. The constructor takes a BlockingQueue (with type argument String). In the run method messages are retrieved from the queue using the take method. When the queue is empty the logging thread will block until messages become available. Once a message is retrieved it can be logged in whichever way is required.
  25. Here is an example of the use of a BlockingQueue. The class implements a logger that will be used by a number of threads to record information. The constructor takes a BlockingQueue (with type argument String). In the run method messages are retrieved from the queue using the take method. When the queue is empty the logging thread will block until messages become available. Once a message is retrieved it can be logged in whichever way is required.
  26. you can use multiple storage engines in a single application; you are not limited to using only one storage engine in a particular database. So, you can easily mix and match storage engines for the given application need. This is often the best way to achieve optimal performance for truly demanding applications: use the right storage engine for the right job. You can use multiple storage engines in a single application. This is particularly useful in a replication setup where a master copy of a database on one server is used to supply copies, called slaves, to other servers. A storage engine for a table in a slave can be different than a storage engine for a table in the master. In this way, you can take advantage of each engine&amp;apos;s abilities. For instance, assume a master with two slaves environment. We can have InnoDB tables on the master, for referential integrity and transactional safety. One slave can also be set up with innoDB or the ARCHIVE engine in order to do backups in a consistent state. Another can be set up with MyISAM and MEMORY tables in order to take advantage of FULLTEXT (MyISAM) or HASH-based indexing (MEMORY).