concurrency

9
Threads in Java 1 Concurrency Synchronizing threads, thread pools, etc.

Upload: daquan-burch

Post on 30-Dec-2015

23 views

Category:

Documents


0 download

DESCRIPTION

Concurrency. Synchronizing threads, thread pools, etc. Benefits of threads More CPU’s. Many modern computers has more than one CPU Dual Core By dividing the program into more threads the program can utilize the CPU’s The program will run faster. Benefits of threads Threads waiting. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Concurrency

Threads in Java 1

Concurrency

Synchronizing threads, thread pools, etc.

Page 2: Concurrency

Benefits of threadsMore CPU’s

• Many modern computers has more than one CPU– Dual Core

• By dividing the program into more threads the program can utilize the CPU’s– The program will run faster

Threads in Java 2

Page 3: Concurrency

Benefits of threadsThreads waiting

• A thread may be waiting for something to happen– File to be read– Incoming network requests– Sleep()– Wait()

• While one thread is waiting other threads should use the CPU– The program will run faster

• Figure from – http://jayford040.blogspot.com/

2009/07/process-concept-operating-system.html

Threads in Java 3

Page 4: Concurrency

Threads in Java 4

Creating threads

Class A extends Thread { public void run() { … }}

A a = new A();a.start();

• A may not extend other classes.

Class B implements Runnable { public void run() { … }}

B b = new B();Tread t = new Thread(b);t.start();

• B may extend another class.• Decouples task submission from thread scheduling, etc.

Page 5: Concurrency

Threads in Java 5

Joining treads

• Some methods on the class Thread– Start()

• Starts the thread

– Join() • waits for the thread to die

Runnable printerA = new Printer("Anders", 10);

Thread threadA = new Thread(printerA);

threadA.start();

// threadA.join();

System.out.println( "Main is done");

Page 6: Concurrency

Threads in Java 6

Synchronizing threads

• Having threads in a programming language is a nice feature, but threads must to be controlled.– If you have 2 or more threads with reference

to the same object, the threads might execute methods on that object simultaneously. • This must be controlled

Page 7: Concurrency

Threads in Java 7

Race conditions and critical sections

• Race condition– 2 or more threads are reading or writing shared data

and the final result depends on the timing of the thread scheduling.

– Race conditions are generally a bad thing!• Critical section

– Part of a program (whole method or just a part of a method) where race conditions might happen.

– To avoid race conditions• We want to make sure that at most one thread executes the

critical section at any point in time.• Threads must be synchronized.

Page 8: Concurrency

Threads in Java 8

References

• Sun Microsystems The Java Tutorial, Threads– http://java.sun.com/docs/books/tutorial/essential/threads/index.ht

ml

• Sun Microsystems Java 2 Platform Standard Edition 5.0 API Specification: java.util.concurrent– http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/packa

ge-summary.html

Page 9: Concurrency

Threads in Java 9

More references• Brian Goetz et al Java

Concurrency in Practice, Addison Wesley 2006 http://javaconcurrencyinpractice.com

• Doug Lea Concurrent Programming in Java 2nd edition, Addison Wesley 2000

• Oaks & Wong Java Threads, O’Reilly 2004

• Niemeyer & Knudsen Learning Java, 3rd edition, O’Reilly 2005– 9. Threads, page 249-297