SlideShare a Scribd company logo
Java Performance An Overview
What is Performance? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The Performance Process ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Design Coding Testing Analysis Profile Performance Acceptable ? Start Ship It. NO YES
Performance Measurement ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Performance Measurement ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Java Threads Day – 1
Java Threading Model ,[object Object],[object Object],[object Object],[object Object],[object Object]
Cooperative and Preemptive Threading ,[object Object],[object Object],[object Object],[object Object]
Threads & Java Language ,[object Object],[object Object],[object Object],[object Object],[object Object]
Coverage ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
java.lang.Thread ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example 1 ,[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example 2 ,[object Object],[object Object],[object Object],[object Object]
class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } public void run() { for (;;) { System.out.println(name + ": hello world"); } } } public class Main2 { public static void main(String [] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); t1.start(); t2.start(); } }
thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread1: hello world thread2: hello world thread1: hello world thread2: hello world thread2: hello world thread1: hello world thread2: hello world thread2: hello world
java.lang.Thread ,[object Object],[object Object],[object Object]
class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } public void run() { for (;;) { System.out.println(name + ": hello world"); yield(); } } } public class Main3 { public static void main(String [] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); t1.start(); t2.start(); } }
Some Output ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Notice the alternation of output
More Thread Members ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Join Example
Some output ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Thread State ,[object Object],[object Object],http://java.sun.com/javase/6/docs/api/java/lang/Thread.State.html
Thread Scheduling in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Sharing Data Across  Java Threads ,[object Object],[object Object],[object Object],[object Object]
class SharedData { public int a = 0; public String s = null; public SharedData() { a = 10; s = "Test"; } } class MyThread extends Thread { private SharedData m_data = null; public MyThread(SharedData data) { m_data = data; } public void run() { for (;;) { m_data.a++; } } }
public class Main5 { public static void main(String [] args) { SharedData data = new SharedData(); MyThread t1 = new MyThread(data); t1.start(); for (;;) { data.a--; } } } If we have multiple threads accessing this shared data, how do we synchronize access to ensure it remains in a consistent state?
Basic Tools for Synchronization in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Synchronized Methods: Monitors ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example ,[object Object]
This implementation has a problem! The Consumer prints which slows it down a LOT, and thus the producer is faster, and thus the producer fills up the queue, and causes heap space to run out!! A good exercise here is to alter this example to limit the maximum number of items that are stored in the queue. See BoundedSynchMain.java
Any problems? ,[object Object],[object Object],See also:  http://java.sun.com/docs/books/tutorial/collections/implementations/queue.html java.util.concurrent.LinkedBlockingQueue<E> http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html
wait (see also java.lang.Object) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
notify and notifyAll (see also java.lang.Object) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Typical use of  wait  within a synchronized method ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Re-checking Monitor Conditions ,[object Object],[object Object],[object Object],[object Object],[object Object]
Example ,[object Object],[object Object],[object Object]
Blocking Remove ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example ,[object Object],[object Object],[object Object]
Another Example ,[object Object]
InterruptedException ,[object Object],[object Object],[object Object]
Exception in Wait // In a synchronized method // check your condition, e.g., with a semaphore // operation, test “value” member variable if /* or while */ (/* condition */) { boolean interrupted; do { interrupted = false; try { wait(); } catch (InterruptedException e) { interrupted = true; } } while (interrupted); } Only allows release from wait caused by notify or notifyAll
Synchronized Blocks ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Syntax ,[object Object],synchronized (object) { // object.wait() // object.notify() // object.notifyAll() }
Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lab 5: Agent Simulation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
java.lang.Runnable Interface ,[object Object],[object Object],[object Object],[object Object],[object Object]
Thread Locking ,[object Object],[object Object],[object Object],[object Object],[object Object]
Fine Grained Locks ,[object Object],[object Object],[object Object]
Fine Grained Locks class FineGrainLock { MyMemberClass x, y; Object xlock = new Object(), ylock = new Object(); public void foo() { synchronized(xlock) { //access x here } //do something here - but don't use shared resources synchronized(ylock) { //access y here } } public void bar() { synchronized(xlock) { synchronized(ylock) { //access both x and y here } } //do something here - but don't use shared resources } }
Reducing Lock Granularity Another valuable technique for reducing contention is to spread your synchronizations over more locks. For example, suppose that you have a class that stores user information and service information in two separate hash tables, as shown in example.   public class AttributesStore { private HashMap usersMap = new HashMap(); private HashMap servicesMap = new HashMap(); public synchronized void setUserInfo(String user, UserInfo userInfo) { usersMap.put(user, userInfo); } public synchronized UserInfo getUserInfo(String user) { return usersMap.get(user); } public synchronized void setServiceInfo(String service,  ServiceInfo serviceInfo) { servicesMap.put(service, serviceInfo); } public synchronized ServiceInfo getServiceInfo(String service) { return servicesMap.get(service); } }
Reducing Lock Granularity http://www.ibm.com/developerworks/java/library/j-threads2.html Here, the accessor methods for user and service data are synchronized, which means that they are synchronizing on the AttributesStore object. While this is perfectly thread-safe, it increases the likelihood of contention for no real benefit. If a thread is executing setUserInfo, it means that not only will other threads be locked out of setUserInfo and getUserInfo, as is desired, but they will also be locked out of getServiceInfo and setServiceInfo. This problem can be avoided by having the accessor simply synchronize on the actual objects being shared (the userMap and servicesMap objects), as shown. Now threads accessing the services map will not contend with threads trying to access the users map. (In this case, the same effect could also be obtained by creating the maps using the synchronized wrapper mechanism provided by the Collections framework, Collections.synchronizedMap.) Assuming that requests against the two maps are evenly distributed, in this case this technique would cut the number of potential contentions in half.  public class AttributesStore { private HashMap usersMap = new HashMap(); private HashMap servicesMap = new HashMap(); public void setUserInfo(String user, UserInfo userInfo) { synchronized(usersMap) { usersMap.put(user, userInfo); } } public UserInfo getUserInfo(String user) { synchronized(usersMap) { return usersMap.get(user); } } public void setServiceInfo(String service,  ServiceInfo serviceInfo) { synchronized(servicesMap) { servicesMap.put(service, serviceInfo); } } public ServiceInfo getServiceInfo(String service) { synchronized(servicesMap) { return servicesMap.get(service); } } }
Reducing Lock Granularity – HashMap Example ,[object Object],[object Object],[object Object],[object Object],import java.util.*; /** * LockPoolMap implements a subset of the Map interface (get, put, clear) * and performs synchronization at the bucket level, not at the map * level.  This reduces contention, at the cost of losing some Map * functionality, and is well suited to simple caches.  The number of * buckets is fixed and does not increase. */ public class LockPoolMap { private Node[] buckets; private Object[] locks; private static final class Node { public final Object key; public Object value; public Node next; public Node(Object key) { this.key = key; } } public LockPoolMap(int size) { buckets = new Node[size]; locks = new Object[size]; for (int i = 0; i < size; i++) locks[i] = new Object(); } private final int hash(Object key) { int hash = key.hashCode() % buckets.length; if (hash < 0) hash *= -1; return hash; } public void put(Object key, Object value) { int hash = hash(key); synchronized(locks[hash]) { Node m; for (m=buckets[hash]; m != null; m=m.next) { if (m.key.equals(key)) { m.value = value; return; } } // We must not have found it, so put it at the beginning of the chain m = new Node(key); m.value = value; m.next = buckets[hash]; buckets[hash] = m; } } public Object get(Object key) { int hash = hash(key); synchronized(locks[hash]) { for (Node m=buckets[hash]; m != null; m=m.next)  if (m.key.equals(key)) return m.value; } return null; } }
Reducing Lock Granularity – HashMap Example ,[object Object],[object Object],Table - Scalability comparison between HashMap and LockPoolMap 80.5 1233.3 13.5 32 37.9 577.0 6.8 16 16.7 272.3 3.7 8 7.7 123.5 2.1 4 3.7 57.6 1.1 2 1.6 1.4 1.1 1 LockPoolMap Synchronized HashMap Unsynchronized HashMap (unsafe) Threads
Common Locking Problems - Deadlocking ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Common Locking Problems - Volatile Variables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Common Locking Problems - Inaccessible Threads ,[object Object],[object Object],[object Object],[object Object],[object Object]
Designing Using Locks for Different Threading Models ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],Designing Using Locks for Different Threading Models
Avoiding Contention through Thread Local http://www.ibm.com/developerworks/java/library/j-threads3.html ,[object Object],[object Object],[object Object]
Avoiding Contention through Thread Local What is a Thread Local Variable? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Art of Locking w/o Locking ,[object Object]
Hardware Based Locking in Java ,[object Object]
Lock Interface http://java.sun.com/docs/books/tutorial/essential/concurrency/newlocks.html ,[object Object],[object Object],[object Object]
Executors http://java.sun.com/docs/books/tutorial/essential/concurrency/executors.html ,[object Object],[object Object],[object Object]
Concurrent Collections http://java.sun.com/docs/books/tutorial/essential/concurrency/collections.html ,[object Object],[object Object],[object Object],[object Object],[object Object]
Object – Hidden Queue ,[object Object]
Concept of Synchronization http://java.sun.com/docs/books/tutorial/essential/concurrency/sync.html ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Understanding a Thread Safe Class ,[object Object]
Producer Consumer Problem ,[object Object]
Problems with Stopping a Thread ,[object Object]
Interrupting a Thread http://java.sun.com/docs/books/tutorial/essential/concurrency/interrupt.html ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Handlers ,[object Object]
Instructions Reordering in Processors & Cache ,[object Object]
Cache Coherency of a Processor ,[object Object]
Synchronization - Final & Volatile ,[object Object]
Safe Programming ,[object Object]
Un-Safe Programming ,[object Object]
Handling Contention ,[object Object]
Understanding Hash Map and Concurrent Hash Map ,[object Object]
Understanding Concurrent Linked Queue ,[object Object]
Understanding Concurrent Skip List Map ,[object Object]
Understanding Deque ,[object Object]
Navigable Interface ,[object Object]
Canned Synchronizers ,[object Object]
Concept of Thread Pool ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Thread pool cont.. ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Alternate thread pool ,[object Object],[object Object],[object Object],[object Object]
Multithreading & Thread pool Lab ,[object Object],[object Object],[object Object],[object Object]
References & Resources ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

What's hot

Threading in C#
Threading in C#Threading in C#
Threading in C#
Medhat Dawoud
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Javakoji lin
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
jehan1987
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Sachintha Gunasena
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
Rajesh Ananda Kumar
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
caswenson
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Thread model of java
Thread model of javaThread model of java
Thread model of javamyrajendra
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
 
multi threading
multi threadingmulti threading
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
Appsterdam Milan
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
Gaurav Aggarwal
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
ducquoc_vn
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
Arun Mehra
 

What's hot (19)

Threading in C#
Threading in C#Threading in C#
Threading in C#
 
javathreads
javathreadsjavathreads
javathreads
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java threading
Java threadingJava threading
Java threading
 
multi threading
multi threadingmulti threading
multi threading
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
 

Viewers also liked

Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVATech_MX
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
raksharao
 
Alternate concurrency models
Alternate concurrency modelsAlternate concurrency models
Alternate concurrency models
Abid Khan
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threadingAPU
 
Java class 6
Java class 6Java class 6
Java class 6Edureka!
 
Java class 3
Java class 3Java class 3
Java class 3Edureka!
 
Developer Friendly API Design
Developer Friendly API DesignDeveloper Friendly API Design
Developer Friendly API Design
theamiableapi
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Multi threading
Multi threadingMulti threading
Multi threading
Mavoori Soshmitha
 
Best Coding Practices in Java and C++
Best Coding Practices in Java and C++Best Coding Practices in Java and C++
Best Coding Practices in Java and C++
Nitin Aggarwal
 
(Ebook resume) job interview - 101 dynamite answers to interview questions ...
(Ebook   resume) job interview - 101 dynamite answers to interview questions ...(Ebook   resume) job interview - 101 dynamite answers to interview questions ...
(Ebook resume) job interview - 101 dynamite answers to interview questions ...Farahaa
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014
Haim Yadid
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and Concurrency
Anton Keks
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
Abdul Rahman Sherzad
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Optimizing Java Performance
Optimizing Java PerformanceOptimizing Java Performance
Optimizing Java Performance
Konstantin Pavlov
 
Creating High Performance Big Data Applications with the Java Persistence API
Creating High Performance Big Data Applications with the Java Persistence APICreating High Performance Big Data Applications with the Java Persistence API
Creating High Performance Big Data Applications with the Java Persistence APIDATAVERSITY
 

Viewers also liked (19)

Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
Alternate concurrency models
Alternate concurrency modelsAlternate concurrency models
Alternate concurrency models
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
 
Java class 6
Java class 6Java class 6
Java class 6
 
Java class 3
Java class 3Java class 3
Java class 3
 
Developer Friendly API Design
Developer Friendly API DesignDeveloper Friendly API Design
Developer Friendly API Design
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Best Coding Practices in Java and C++
Best Coding Practices in Java and C++Best Coding Practices in Java and C++
Best Coding Practices in Java and C++
 
(Ebook resume) job interview - 101 dynamite answers to interview questions ...
(Ebook   resume) job interview - 101 dynamite answers to interview questions ...(Ebook   resume) job interview - 101 dynamite answers to interview questions ...
(Ebook resume) job interview - 101 dynamite answers to interview questions ...
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and Concurrency
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Java performance
Java performanceJava performance
Java performance
 
Optimizing Java Performance
Optimizing Java PerformanceOptimizing Java Performance
Optimizing Java Performance
 
Creating High Performance Big Data Applications with the Java Persistence API
Creating High Performance Big Data Applications with the Java Persistence APICreating High Performance Big Data Applications with the Java Persistence API
Creating High Performance Big Data Applications with the Java Persistence API
 

Similar to Java Performance, Threading and Concurrent Data Structures

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Multithreading
MultithreadingMultithreading
Multithreading
Ravi Chythanya
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
Himanshu Rajput
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
Saquib Sajid
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
ssuserfcae42
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
Minal Maniar
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
 
Java
JavaJava
multithreading
multithreadingmultithreading
multithreading
Rajkattamuri
 
Multithreading
MultithreadingMultithreading
Multithreading
F K
 
Basic Thread Knowledge
Basic Thread KnowledgeBasic Thread Knowledge
Basic Thread Knowledge
Shipra Roy
 
Java
JavaJava
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
Ducat India
 

Similar to Java Performance, Threading and Concurrent Data Structures (20)

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
concurrency
concurrencyconcurrency
concurrency
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
Java
JavaJava
Java
 
multithreading
multithreadingmultithreading
multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Basic Thread Knowledge
Basic Thread KnowledgeBasic Thread Knowledge
Basic Thread Knowledge
 
Java
JavaJava
Java
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
 
Java multi thread programming on cmp system
Java multi thread programming on cmp systemJava multi thread programming on cmp system
Java multi thread programming on cmp system
 

Java Performance, Threading and Concurrent Data Structures

  • 2.
  • 3.
  • 4.
  • 5.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16. class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } public void run() { for (;;) { System.out.println(name + &quot;: hello world&quot;); } } } public class Main2 { public static void main(String [] args) { MyThread t1 = new MyThread(&quot;thread1&quot;); MyThread t2 = new MyThread(&quot;thread2&quot;); t1.start(); t2.start(); } }
  • 17. thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread2: hello world thread1: hello world thread2: hello world thread1: hello world thread2: hello world thread2: hello world thread1: hello world thread2: hello world thread2: hello world
  • 18.
  • 19. class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } public void run() { for (;;) { System.out.println(name + &quot;: hello world&quot;); yield(); } } } public class Main3 { public static void main(String [] args) { MyThread t1 = new MyThread(&quot;thread1&quot;); MyThread t2 = new MyThread(&quot;thread2&quot;); t1.start(); t2.start(); } }
  • 20.
  • 21.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27. class SharedData { public int a = 0; public String s = null; public SharedData() { a = 10; s = &quot;Test&quot;; } } class MyThread extends Thread { private SharedData m_data = null; public MyThread(SharedData data) { m_data = data; } public void run() { for (;;) { m_data.a++; } } }
  • 28. public class Main5 { public static void main(String [] args) { SharedData data = new SharedData(); MyThread t1 = new MyThread(data); t1.start(); for (;;) { data.a--; } } } If we have multiple threads accessing this shared data, how do we synchronize access to ensure it remains in a consistent state?
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. This implementation has a problem! The Consumer prints which slows it down a LOT, and thus the producer is faster, and thus the producer fills up the queue, and causes heap space to run out!! A good exercise here is to alter this example to limit the maximum number of items that are stored in the queue. See BoundedSynchMain.java
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48. Exception in Wait // In a synchronized method // check your condition, e.g., with a semaphore // operation, test “value” member variable if /* or while */ (/* condition */) { boolean interrupted; do { interrupted = false; try { wait(); } catch (InterruptedException e) { interrupted = true; } } while (interrupted); } Only allows release from wait caused by notify or notifyAll
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57. Fine Grained Locks class FineGrainLock { MyMemberClass x, y; Object xlock = new Object(), ylock = new Object(); public void foo() { synchronized(xlock) { //access x here } //do something here - but don't use shared resources synchronized(ylock) { //access y here } } public void bar() { synchronized(xlock) { synchronized(ylock) { //access both x and y here } } //do something here - but don't use shared resources } }
  • 58. Reducing Lock Granularity Another valuable technique for reducing contention is to spread your synchronizations over more locks. For example, suppose that you have a class that stores user information and service information in two separate hash tables, as shown in example. public class AttributesStore { private HashMap usersMap = new HashMap(); private HashMap servicesMap = new HashMap(); public synchronized void setUserInfo(String user, UserInfo userInfo) { usersMap.put(user, userInfo); } public synchronized UserInfo getUserInfo(String user) { return usersMap.get(user); } public synchronized void setServiceInfo(String service, ServiceInfo serviceInfo) { servicesMap.put(service, serviceInfo); } public synchronized ServiceInfo getServiceInfo(String service) { return servicesMap.get(service); } }
  • 59. Reducing Lock Granularity http://www.ibm.com/developerworks/java/library/j-threads2.html Here, the accessor methods for user and service data are synchronized, which means that they are synchronizing on the AttributesStore object. While this is perfectly thread-safe, it increases the likelihood of contention for no real benefit. If a thread is executing setUserInfo, it means that not only will other threads be locked out of setUserInfo and getUserInfo, as is desired, but they will also be locked out of getServiceInfo and setServiceInfo. This problem can be avoided by having the accessor simply synchronize on the actual objects being shared (the userMap and servicesMap objects), as shown. Now threads accessing the services map will not contend with threads trying to access the users map. (In this case, the same effect could also be obtained by creating the maps using the synchronized wrapper mechanism provided by the Collections framework, Collections.synchronizedMap.) Assuming that requests against the two maps are evenly distributed, in this case this technique would cut the number of potential contentions in half. public class AttributesStore { private HashMap usersMap = new HashMap(); private HashMap servicesMap = new HashMap(); public void setUserInfo(String user, UserInfo userInfo) { synchronized(usersMap) { usersMap.put(user, userInfo); } } public UserInfo getUserInfo(String user) { synchronized(usersMap) { return usersMap.get(user); } } public void setServiceInfo(String service, ServiceInfo serviceInfo) { synchronized(servicesMap) { servicesMap.put(service, serviceInfo); } } public ServiceInfo getServiceInfo(String service) { synchronized(servicesMap) { return servicesMap.get(service); } } }
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.

Editor's Notes

  1. Next time add Java Conditions into this in synchronization part http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/Condition.html
  2. Don’t call run method directly.
  3. Run on Mac OS X, 3/14/08
  4. Run on Mac OS X, 3/14/08
  5. final on a method indicates it can’t be overridden The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing, t.join(); causes the current thread to pause execution until t&apos;s thread terminates. Overloads of join allow the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify. Like sleep, join responds to an interrupt by exiting with an InterruptedException.
  6. 10 and 1 were obtained on Mac OS X 2/21/05
  7. Of course, part of this depends on how we define “consistent state”.
  8. Should change this to be a generic type. Have them notice that the constructor is not synchronized. Why is that? It is because no two threads can call the constructor and get the same object returned.
  9. I’ve marked the changes to the previous version in red.
  10. Another example of checking/rechecking conditions: Let them work this one in class
  11. See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#wait() for reasons why wait can be woken “ if another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.” See also: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#interrupt()
  12. Another implementation of this without the complex code for dealing with the InterruptedException would be to have the V operation increment a variable, and have the P operation test that variable to see if it is &gt; 0. See Semaphore-v3.java