DOC PREVIEW
CU-Boulder CSCI 5448 - Java Concurrency Framework

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java Concurrency FrameworkSidartha GraciasExecutive Summary• This is a beginners introduction to the java concurrency framework• Some familiarity with concurrent programs is assumed– However the presentation does go through a quick background on concurrency– So readers unfamiliar with concurrent programming should still get something out of this• The structure of the presentation is as follows– A brief history into concurrent programming is provided– Issues in concurrent programming are explored– The framework structure with all its major components are covered in some detail– Examples are provided for each major section to reinforce some of the major ideasConcurrency in Java - Overview • Java like most other languages supports concurrency through thread– The JVM creates the Initial thread, which begins execution from main– The main method can then spawn additional threads Thread Basics• All modern OS support the idea of processes – independently running programs that are isolated from each other• Thread can be thought of as light weight processes– Like processes they have independent program counters, call stacks etc– Unlike Processes they share main memory, file pointers and other process state– This means thread are easier for the OS to maintain and switch between– This also means we need to synchronize threads for access to shared resourcesThreads Continued…• So why use threads ?– Multi CPU systems: Most modern systems host multiple CPU’s, by splitting execution between them we can greatly speed up execution– Handling Asynchronous Events: Servers handle multiple clients. Processing each client is best done through a separate thread, because the Server blocks until a new message is received– UI or event driven Processing: event handlers that respond to user input are best handled through separate threads, this makes code easier to write and understandSynchronization Primitives in Java• How does the java language handle synchronization– Concurrent execution is supported through the Thread class.– Access to shared resources is controlled through • Synchronized objects• Synchronized methods– Locking and Unlocking shared resources is handled automatically– However only one (duh.) thread can hold a shared object at one time– A thread that cannot acquire a lock will block.Writing Concurrent Programs• But its not so easy..– Writing non trivial concurrent programs is not so straight – forward, here are a few of the issues you could run into• Deadlock: Two or more threads waiting for each other to release a resourceResource AThread A1: acquire A3: acquire BResource BThread B1: acquire B3: acquire AConcurrency issues continued..• Race Conditions: Non deterministic output, that depends on the order of thread execution• Output: – 11111111… or– 12121212… or etcThread AWhile(true)Print 1Thread BWhile(true)Print 2Concurrency issues continued..• Starvation: A slow thread being starved of a resource by a fast thread• This brings us to thread safe classes– A class that guarantees the internal state of the class as well as returned values from methods are correct while invoked concurrently from multiple threadResource AThread A Thread B Acquire A (1ms) Acquire B (1us)Where does this leave us?• This shows us writing concurrent programs is hard and prone to bugs• The synchronization primitives java provides are of too low a granularity and do not scale with program complexity• It would be nice if we could abstract some of this complexity away• Further many of the problems encountered writing parallel programs are common across a wide area• We really should not have to reinvent the wheel each time we want to write a concurrent program• This is where the Java Concurrency Framework comes in..Framework Overview• The Concurrency utilities includes the following:1. Task Scheduling Framework: The Executor is a framework for handling the invocation, scheduling and execution of tasks2. Concurrent Collection: Concurrent implementations of commonly used classes like map, list and queue (no more reinventing the wheel ).3. Atomic Variables: classes for atomic manipulation of single variables, provides higher performance than simply using synchronization primitives 4. Locks: High performance implementation of locks with the same semantics as the synchronized keyword, with additional functionality like timeouts.5. Timers: Provides access to a nanosecond timer for making fine grained timing measurements, useful for methods that accept timeouts.Advantages of using the framework• Reusability and effort reduction: Many commonly used concurrent classes already implemented• Superior performance: Inbuilt implementation highly optimized and peer reviewed by experts• Higher reliability, less bugs: Working from already developed building blocks lead to more reliable programs• Improved maintainability and scalability: Reusable components leads to programs that are easier to maintain and scale much better• Increased productivity: Developers no longer have to reinvent the wheel each time, programs easier to debug, more likely to understand standard library implementationsThe Executor Framework• The Executor framework provides a mechanism for invoking, scheduling, and executing tasks according to a set of policies• Also provides an implementation of thread pools through the ThreadPoolExecutor class• This provides a way to decouple task submission from task execution policy• Makes it easy to change task execution policy• Supports several different execution policies by default, and developers can create Executors supporting arbitrary execution policiesInterlude: Thread pools• Consider writing a web server that handles an arbitrary number of clients• One way of implementing this is to spawn a new thread for each client that makes a request• However under high load this could crash the server, due to the large amount of resources used to create and maintain the threads• Worse we are creating far more threads than can be handled by the server, probably most threads will sit around waiting for CPU time.Thread pools continued…• A smarter way to handle this is to use the idea of a thread pool• We create a fixed number of threads called a thread pool• We use a queue data structure into which tasks are submitted• Each free thread picks a task of the queue and processes it• If all


View Full Document

CU-Boulder CSCI 5448 - Java Concurrency Framework

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download Java Concurrency Framework
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Concurrency Framework and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Concurrency Framework 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?