Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Concurrency in java

978 views

Published on

Published in: Technology, News & Politics
  • Be the first to comment

Concurrency in java

  1. 1. ABHRA BASAK APOORVA KUMAR
  2. 2. What will be covered?
  3. 3. • Concurrency and Parallelism in the Java Runtime Environment • The Java Thread Model - Decisions for Design • Implementing Multithreading – Creating and Managing Threads • Remote Method Execution – Java RMI • The Concurrency Utilities – java.util.concurrent • Thread Safety – Shared Access to Data • GUI Applications in Java
  4. 4. The Java Runtime Environment
  5. 5. • Allows you to run the Java Compiler at the same time that you are using an editor Process-based multitasking • A text editor can format text at the same time that it is printing Thread-based multitasking
  6. 6. Java Libraries Java Code Byte code
  7. 7. Byte code Java Virtual Machine Executable Instructions
  8. 8. Decisions for Design
  9. 9. • Thread priorities are integers assigned by the Java runtime that specify the relative priority of one thread to another. • A thread can voluntarily relinquish control. • A thread can be preempted by a higher priority thread.
  10. 10. • An elegant twist to an age-old model of inter-process synchronization – the monitor. • Most multithreaded systems expose monitors as objects that the program must explicitly acquire and manipulate. • Java provides a cleaner solution : each object has its own implicit monitor, automatically called while invoking synchronized methods.
  11. 11. Thread 1 Thread 2
  12. 12. Creating and Managing Threads
  13. 13. Source : Parallel Programming for Multicore and Cluster Systems - Rauber & Runger
  14. 14. Source : Parallel Programming for Multicore and Cluster Systems - Rauber & Runger
  15. 15. • Thread execution • start() – Start a thread by calling its run() method • run() – Entry point for the thread • Thread blocking • sleep() – Suspend a thread for a period of time • yield() – Voluntarily relinquish thread control to another thread of the same priority
  16. 16. • Thread lifetime and termination • isAlive() – Determine if a thread is still running • join() – Wait for a thread to terminate • Thread communication • wait() – Instructs the calling thread to give up the monitor and sleep until some other thread enters the same monitor and calls notify() • notify() – Wakes up a thread that called wait() on the same object • notifyAll() – Wakes up all threads that called wait() on the same object
  17. 17. Remote Method Invocation
  18. 18. • The RMI facility allows you to invoke operations on objects running in other JVMs. • When a remote method is called, the RMI stub, created by the RMI compiler, packages the method parameters and sends them over the network. • The remote system unpacks the arguments and then calls the remote method.
  19. 19. The Concurrent API Packages
  20. 20. • Concurrency APIs java.util.concurrent • Atomic data types java.util.concurrent.atomic • Locks for synchronization java.util.concurrent.locks
  21. 21. • Implements the classic semaphore Semaphore • Waits until a specified number of events have occurred CountDownLatch • Enables a group of threads to wait at a predefined execution point CyclicBarrier • Exchanges data between two threads Exchanger
  22. 22. Executor ExecutorService ThreadPool Executor ScheduledPool Executor Future Callable
  23. 23. Concurrent HashMap Concurrent LinkedQueue CopyOnWrite ArrayList Array BlockingQueue Concurrent SkipListMap Concurrent SkipListSet CopyOnWrite ArraySet DelayQueue LinkedBlocking Deque LinkedBlocking Queue Prioirty BlockingQueue Synchronous Queue
  24. 24. TimeUnit Enumeration DAYS HOURS MINUTES SECONDS MICRO SECONDS MILLI SECONDS NANO SECONDS
  25. 25. Lock ReentrantLock ReadWriteLock Reentrant ReadWriteLock
  26. 26. AtomicInteger AtomicLong decrementAndGet() compareAndSet() getAndSet()
  27. 27. Sharing Access to Data
  28. 28. Thread safe Behaves correctly (unambiguous) Independent of scheduling and interleaving Without additional synchronization
  29. 29. synchronized • The primary mechanism for synchronization volatile • Enables the thread to bypass the cache when accessing the data
  30. 30. Multithreaded GUIs
  31. 31. • GUI systems use a single thread called the Event Dispatch Thread for handling GUI events • Multithreaded GUIs tend to be particularly prone to deadlocks. • Swing is single-threaded particularly because of observations of AWT which tried to be provide a higher degree of multi threaded access. • Longer running tasks triggered by event listeners in the even dispatch thread are switched over to different threads to ensure the GUI doesn’t freeze.
  32. 32. What we looked at
  33. 33. • Concurrency and Parallelism in the Java Runtime Environment • The Java Thread Model - Decisions for Design • Implementing Multithreading – Creating and Managing Threads • Remote Method Execution – Java RMI • The Concurrency Utilities – java.util.concurrent • Thread Safety – Shared Access to Data • GUI Applications in Java
  34. 34. • Android – A Java based mobile application platform • Java Swing, AWT and Applets • Process scheduling in Java Operating Systems
  35. 35. Time for Questions and Comments
  36. 36. • Tim Peierls, Brian Goetz, Joshua Bloch, Joseph Bowbeer, Doug Lea, and David Holmes. 2005. Java Concurrency in Practice. Addison-Wesley Professional. • Kazuaki Ishizaki, Shahrokh Daijavad, and Toshio Nakatani. 2011. Refactoring Java programs using concurrent libraries. In Proceedings of the Workshop on Parallel and Distributed Systems: Testing, Analysis, and Debugging (PADTAD '11) • Dan Grossman and Ruth E. Anderson. 2012. Introducing parallelism and concurrency in the data structures course. In Proceedings of the 43rd ACM technical symposium on Computer Science Education (SIGCSE '12)
  37. 37. • Alan D. Fekete. 2008. Teaching students to develop thread-safe java classes. In Proceedings of the 13th annual conference on Innovation and technology in computer science education (ITiCSE '08) • Doug Lea. Concurrent Programming in Java: Design principles and patterns, Addison-Wesley. First edition, October 1996 (also, German, Chinese, and Japanese translations, and an on-line supplement). Second edition, November 1999. • Brian Goetz, with Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea, Java Concurrency in Practice, Addison Wesley, 2006.

×