0% found this document useful (0 votes)
56 views21 pages

Comp322 s12 Lec27 Slides v1

A Java thread goes through the following states during its lifecycle: 1. It is created as a Thread object in the NEW state. 2. Its start() method is called, moving it to the RUNNABLE state where it is eligible to be scheduled for execution. 3. It executes until becoming BLOCKED waiting for a lock or entering WAITING/TIMED_WAITING states waiting for another thread. 4. The thread ultimately terminates and enters the TERMINATED state.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views21 pages

Comp322 s12 Lec27 Slides v1

A Java thread goes through the following states during its lifecycle: 1. It is created as a Thread object in the NEW state. 2. Its start() method is called, moving it to the RUNNABLE state where it is eligible to be scheduled for execution. 3. It executes until becoming BLOCKED waiting for a lock or entering WAITING/TIMED_WAITING states waiting for another thread. 4. The thread ultimately terminates and enters the TERMINATED state.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

COMP 322: Fundamentals of

Parallel Programming

Lecture 27: Introduction to Java Threads

Vivek Sarkar
Department of Computer Science, Rice University
[email protected]

https://wiki.rice.edu/confluence/display/PARPROG/COMP322

COMP 322 Lecture 27 21 March 2012


Acknowledgments for Today’s Lecture
• “Introduction to Concurrent Programming in Java”, Joe Bowbeer, David
Holmes, OOPSLA 2007 tutorial slides
— Contributing authors: Doug Lea, Brian Goetz
— http://www.oopsla.org/oopsla2007/index.php?page=sub/&id=69

2 COMP 322, Spring 2012 (V.Sarkar)


Classification of Parallel
Programming Models
• Library approaches
— POSIX threads
— Message-Passing Interface (MPI)
— MapReduce frameworks

• Pseudocomment “pragma” approaches


— OpenMP

• Language approaches
— Habanero-Java
— Unified Parallel C
— Co-Array Fortran
— Chapel
— X10
—. . .

==> Java takes a library approach with a little bit of language support
(synchronized keyword)

3 COMP 322, Spring 2012 (V.Sarkar)


Closures
• Library-based approaches to parallel programming require
interfaces in which computations can be passed as data

• Recall that a closure is a first-class function with free


variables that are bound in function’s lexical environment e.g.,
the anonymous lambda expression in the following Scheme
program is a closure
; Return a list of all books with at least THRESHOLD copies sold.
(define (best-selling-books threshold)
(filter
(lambda (book)
(>= (book-sales book) threshold))
book-list))

• Note that the value of free variable threshold is captured when


the lambda expression is defined

4 COMP 322, Spring 2012 (V.Sarkar)


HJ Asyncs and Closures
• The body of an HJ async task is a parameter-less closure that
is both created and enabled for execution at the point when the
async statement is executed
• An async captures the values of free variables (local variables in
outer scopes) when it is created
— e.g., variable len in Listing 1 below

5 COMP 322, Spring 2012 (V.Sarkar)


java.lang.Runnable interface
• Any class that implements java.lang.Runnable must provide a
parameter-less run() method with void return type

• Lines 3-7 in Listing 2 show the creation of an instance of an


anonymous inner class that implements the Runnable interface
• The computation in the run() method can be invoked sequentially
by calling r.run()
— We will see next how it can be invoked in parallel

6 COMP 322, Spring 2012 (V.Sarkar)


java.lang.Thread class
• Execution of a Java program begins with an instance of Thread created
by the Java Virtual Machine (JVM) that executes the program’s main()
method.
• Parallelism can be introduced by creating additional instances of class
Thread that execute as parallel threads.

7 COMP 322, Spring 2012 (V.Sarkar)


HJ runtime uses Java threads as workers …

• HJ runtime creates a small number of worker threads, typically one per


core
• Workers push async’s/continuations into a logical work queue
• when an async operation is performed
• when an end-finish operation is reached
• Workers pull task/continuation work item when they are idle
8 COMP 322, Spring 2012 (V.Sarkar)
… because programming directly with Java threads
can be expensive

Lecture 9: Fork-Join Microbenchmark Measurements


(execution time in micro-seconds)

9 COMP 322, Spring 2012 (V.Sarkar)


Two ways to specify computation for a
Java thread
1. Define a class that implements the Runnable
interface and pass an instance of that class to the
Thread constructor in line 3 of Listing 3 (slide 7).
— It is common to create an instance of an
anonymous inner class that implements Runnable
for this purpose. In this case, the Runnable
instance defines the work to be performed, and
the Thread instance identifies the worker that will
perform the work.
2. Subclass Thread and override the run() method. This
is usually inconvenient in practice because of Java’s
single-inheritance constraint.

10 COMP 322, Spring 2012 (V.Sarkar)


start() and join() methods
• A Thread instance starts executing when its start()
method is invoked
—start() can be invoked at most once per Thread instance
—As with async, the parent thread can immediately move to the
next statement after invoking t.start()

• A t.join() call forces the invoking thread to wait till


thread t completes.
—Lower-level primitive than finish since it only waits for a
single thread rather than a collection of threads
—No restriction on which thread performs a join on which
thread, so it is possible to create a deadlock cycle using join()
—No notion of an Immediately Enclosing Finish in Java threads
—No propagation of exceptions to parent/ancestor threads

11 COMP 322, Spring 2012 (V.Sarkar)


Listing 4: Two-way Parallel ArraySum
using Java threads

12 COMP 322, Spring 2012 (V.Sarkar)


Callable Objects can be used to create
Future Tasks in Java
• Any class that implements java.lang.Callable<V> must provide a call()
method with return type V

• Sequential example with Callable interface

13 COMP 322, Spring 2012 (V.Sarkar)


4 steps to create future tasks in Java
using Callable objects and Java threads
1. Create a parameter-less callable closure using a
statement like
Callable<Object> c = new Callable<Object>()
{public Object call() { return ...; }}; ”
2. Encapsulate the closure as a task using a statement like
FutureTask<Object> ft = new
FutureTask<Object>(c);
3. Start executing the task in a new thread by issuing the
statement: new Thread(ft).start();
4. Wait for the task to complete and obtain its result by
issuing the statement: Object o = ft.get();

14 COMP 322, Spring 2012 (V.Sarkar)


HTML renderer in Java after parallelization of
Callable tasks, and corresponding HJ version
Java version

HJ version

15 COMP 322, Spring 2012 (V.Sarkar)


Example of creating Java threads by subclassing Thread
(not recommended due to Java’s single inheritance rule)

• This program uses two threads: the main thread and a HelloThread
— Each prints a greeting – the order of which is nondeterministic

public static void main(String[] args) {


class HelloThread extends Thread {
public void run() {
System.out.println(“Hello from thread ”
+ Thread.currentThread().getName());}
}
Thread t = new HelloThread(); // create HelloThread
t.start(); // start HelloThread
System.out.println(“Hello from main thread”);
}
• Program execution ends when both user threads have completed

16 COMP 322, Spring 2012 (V.Sarkar)


Another Example: Sequential Web Server

public class SequentialWebServer {


public static final int PORT = 8080;
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(PORT);
while (true) {
Socket sock = server.accept(); // get next connection
try {
processRequest(sock); // do the real work
} catch (IOException ex) {
System.err.println("An error occurred ...");
ex.printStackTrace();
}
}
}
// ... rest of class definition

17 COMP 322, Spring 2012 (V.Sarkar)


Parallelization of Web Server Example
using Runnable Tasks
public class ThreadPerTaskWebServer { . . .
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(PORT);
while (true) {
final Socket sock = server.accept();
Runnable r = new Runnable() { // anonymous inner class
public void run() {
try {
processRequest(sock);
} catch (IOException ex) {
System.err.println("An error occurred ...");
}
}
};
new Thread(r).start();
} . . .

18 COMP 322, Spring 2012 (V.Sarkar)


Possible states for a Java thread
(java.lang.Thread.State)
• NEW
— A thread that has not yet started is in this state.

• RUNNABLE
— A thread executing in the Java virtual machine is in this state.

• BLOCKED
— A thread that is blocked waiting for a monitor lock is in this state.

• WAITING
— A thread that is waiting indefinitely for another thread to perform a
particular action is in this state e.g., join()

• TIMED_WAITING
— A thread that is waiting for another thread to perform an action for up
to a specified waiting time is in this state e.g., join() with timeout

• TERMINATED
— A thread that has exited is in this state.

19 COMP 322, Spring 2012 (V.Sarkar)


Summary: Lifecycle of a Java thread
• A thread is created by instantiating a Thread object
• A thread is started by calling Thread.start() on that object
— Causes execution of its run() method in a new thread of execution

• A thread’s state can be inspected by calling Thread.getState()


• A thread terminates by:
— Returning normally from its run() method
— Throwing an exception that isn't caught by any catch block
— The VM being shut down

• The JVM shuts down when all user (non-daemon) threads terminate
— Or when shutdown is requested by System.exit, CTRL/C, signal, or other process
termination triggers

• Daemon threads are terminated when JVM shuts down


— Child thread inherits daemon status from parent thread
— Override by calling Thread.setDaemon(boolean) before starting thread
— Main thread is started as user thread

20 COMP 322, Spring 2012 (V.Sarkar)


Announcements
• Homework 4 due today

• Homework 5 (written assignment) due on Friday, April 6th

• Monday’s lecture will be a guest lecture by Prof. John Mellor-


Crummey on parallel sorting algorithms (Bitonic sort)

• Midterm and HW3 grading is still under way


— Midterms will be returned by next Wednesday

21 COMP 322, Spring 2012 (V.Sarkar)

You might also like