Threads in Java a tutorial introduction (revision 7) Christian Ratliff [email protected]...

40
Threads in Java Threads in Java a tutorial introduction a tutorial introduction (revision 7) (revision 7) Christian Ratliff Christian Ratliff [email protected] [email protected] Senior Technology Architect Senior Technology Architect Core Libraries Group, DeLorme Core Libraries Group, DeLorme 28 August 2002 28 August 2002

Transcript of Threads in Java a tutorial introduction (revision 7) Christian Ratliff [email protected]...

Page 1: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Threads in JavaThreads in Javaa tutorial introduction a tutorial introduction (revision 7)(revision 7)

Christian Ratliff Christian Ratliff [email protected]@delorme.comSenior Technology ArchitectSenior Technology Architect

Core Libraries Group, DeLormeCore Libraries Group, DeLorme28 August 200228 August 2002

Page 2: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

DemoDemo

SerialPrime.javaSerialPrime.java

Page 3: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

What is a ‘thread’?What is a ‘thread’?

Free Online Dictionary of Computing (FOLDOC)

Sharing a single CPU between multiple tasks (or "threads") in a way designed to minimize the time required to switch tasks. This is accomplished by sharing as much as possible of the program execution environment between the different tasks so that very little state needs to be saved and restored when changing tasks. .

Page 4: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Wherefore art thou?Wherefore art thou?

►To maintain responsiveness of an To maintain responsiveness of an application during a long running task.application during a long running task.

►To enable cancellation of separable tasks.To enable cancellation of separable tasks.►Some problems are intrinsically parallel.Some problems are intrinsically parallel.►To monitor status of some resource (DB).To monitor status of some resource (DB).►Some APIs and systems demand it: Swing.Some APIs and systems demand it: Swing.►To take advantage of multiple processors.To take advantage of multiple processors.► It looks great on your resume.It looks great on your resume.

Page 5: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Threads and ProcessesThreads and ProcessesCPU

Process 1 Process 3Process 2 Process 4

main

run

GC

Page 6: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Oh No! Computer Science!!Oh No! Computer Science!!

►Some critical concepts:Some critical concepts: non-determinismnon-determinism race conditionrace condition concurrencyconcurrency parallel schedulingparallel scheduling

►At least I am not charging $10,000 a At least I am not charging $10,000 a year!year!

Page 7: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Concurrency vs. ParallelismConcurrency vs. ParallelismCPU CPU1 CPU2

Page 8: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Concurrency vs. ParallelismConcurrency vs. ParallelismCPU1 CPU2

main

main

run

CPU

main

run

main

run

mainrun

main

RAM

this.count

Page 9: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Whatever! How about some Whatever! How about some code?code?

► In Java, thread instances come in two In Java, thread instances come in two varieties:varieties: java.lang.Runnablejava.lang.Runnable java.lang.Threadjava.lang.Thread

Page 10: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

java.lang.Runnablejava.lang.Runnable

►An adapter which enables any given An adapter which enables any given class to operate as a thread.class to operate as a thread.

►Requires one method be implemented:Requires one method be implemented: public void run()

►Most common method for adding Most common method for adding threads to an application.threads to an application.

►Prevents certain thread operations Prevents certain thread operations (e.g. (e.g. Thread.isAlive()).).

Page 11: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

DemoDemo

counter/CounterThread0.javacounter/CounterThread0.java

Page 12: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

java.lang.Threadjava.lang.Thread

►A base class with maximum threading A base class with maximum threading functionality.functionality.

►While less common, it is far more While less common, it is far more powerful.powerful.

►Forces the focus of your class to its Forces the focus of your class to its “threadedness”.“threadedness”.

►Minimum requirements are like Minimum requirements are like RunnableRunnable:: public void run()

Page 13: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

DemoDemo

counter/CounterThread1.javacounter/CounterThread1.java

Page 14: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Alive

Thread State DiagramThread State Diagram

New Thread Dead Thread

Running

Runnable

new CounterThread1(max);

run() method returns

while (…) { … }

BlockedObject.wait()Thread.sleep()blocking IO callwaiting on a monitor

cntThread.start();

Page 15: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

DemoDemo

counter/CounterThread2.javacounter/CounterThread2.java

Page 16: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Cooperative MultithreadingCooperative Multithreading

►By adding a Thread.yield() call to the By adding a Thread.yield() call to the subthread and the main, all 50 values subthread and the main, all 50 values were fetched.were fetched.

►Success depends on careful cooperation Success depends on careful cooperation between each thread.between each thread.

►This is not a safe option.This is not a safe option.

►Wizards: What is the other problem Wizards: What is the other problem here?here?

Page 17: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Accessing Shared ResourcesAccessing Shared Resources

►The only safe mechanism is through locking.The only safe mechanism is through locking.►There are many kinds of locks:There are many kinds of locks:

busy-wait-flag, semaphore, mutex, etcbusy-wait-flag, semaphore, mutex, etc

►They can offer many differing semantics:They can offer many differing semantics: barriers, reader-writer, critical sections, etcbarriers, reader-writer, critical sections, etc

► Java offers one, coherent mechanism:Java offers one, coherent mechanism: monitorsmonitors

►Wizards: Who invented the monitor? When?Wizards: Who invented the monitor? When?

Page 18: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

synchronizedsynchronized

►Monitors are implemented on a per-object Monitors are implemented on a per-object basis.basis.

►Each method on an object which accesses Each method on an object which accesses protected resources is marked protected resources is marked synchronizedsynchronized..

►The monitor is a “fence” around the object.The monitor is a “fence” around the object.►Each synchronized method is a “gate” in Each synchronized method is a “gate” in

that fence.that fence.

Page 19: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Inside MonitorsInside Monitors

PrimeCache

isPrime

addPrimeToCache

mainrun primes maxPrime

Page 20: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Inside MonitorsInside Monitors

PrimeCache

isPrime

addPrimeToCache

main

run

primes maxPrime

Page 21: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Inside MonitorsInside Monitors

PrimeCache

isPrime

addPrimeToCache

mainprimes maxPrime

Page 22: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Inside MonitorsInside Monitors

PrimeCache

isPrime

addPrimeToCache

main

primes maxPrime

Page 23: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Inside MonitorsInside Monitors

PrimeCache

isPrime

addPrimeToCache

primes maxPrime

Page 24: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

DemoDemo

sieve/SieveThread0.javasieve/SieveThread0.java

Page 25: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Other uses of Other uses of synchronizedsynchronized

►There are two additional situations There are two additional situations where where synchronizedsynchronized may be used: may be used: The synchronized statement may be used The synchronized statement may be used

in a method to synchronize an arbitrary in a method to synchronize an arbitrary block of code.block of code.

A static method may be labeled as A static method may be labeled as synchronized, in which case the monitor it synchronized, in which case the monitor it attached to the enclosing class.attached to the enclosing class.

Page 26: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

*SNORE**SNORE*

WAKE UP!WAKE UP!

SNACK TIME!SNACK TIME!

Page 27: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

SignallingSignalling

►Use of the Use of the synchronizedsynchronized attribute attribute alone is not sufficient.alone is not sufficient.

► If a thread must wait for both access If a thread must wait for both access to a resource and a condition to be to a resource and a condition to be satisfied, the only obvious option is a satisfied, the only obvious option is a busy-wait.busy-wait.

►There must be a better way!There must be a better way!

Page 28: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

SignallingSignalling

Object.wait()Object.wait()►Gives up ownership Gives up ownership

of the monitor.of the monitor.► Blocks until timeout, Blocks until timeout,

interruption, or interruption, or notification.notification.

►On waking, the On waking, the thread enters the thread enters the monitor acquisition monitor acquisition phase.phase.

Object.notify()Object.notify()

Object.notifyAll()Object.notifyAll()►Does not give up Does not give up

ownership of the ownership of the monitor.monitor.

►Wakes an arbitrary, Wakes an arbitrary, or all, thread(s) or all, thread(s) blocked on the blocked on the monitor.monitor.

► No thread preference!No thread preference!

Page 29: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

DemoDemo

sieve/SieveThread1.javasieve/SieveThread1.java

Page 30: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Exception HandlingException Handling

►When an exception is emitted out of When an exception is emitted out of the run() method, the thread is the run() method, the thread is terminated.terminated.

►The exception is consumed by the JVM The exception is consumed by the JVM because once Thread.start() is called, because once Thread.start() is called, the created thread is split from the the created thread is split from the caller.caller.

► Java provides a means for dispatching a Java provides a means for dispatching a copy of the exception: ThreadGroup.copy of the exception: ThreadGroup.

Page 31: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

java.lang.ThreadGroupjava.lang.ThreadGroup

►When an exception is emitted by the When an exception is emitted by the Thread.runThread.run method, the method method, the method ThreadGroup.uncaughtExceptionThreadGroup.uncaughtException is is called.called.

►Derive a class from Derive a class from ThreadGroupThreadGroup..►Override the Override the uncaughtExceptionuncaughtException

method, relaying the thread and method, relaying the thread and exception information.exception information.

Page 32: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

DemoDemo

group/SieveThread.javagroup/SieveThread.java

Page 33: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Jane, stop this crazy thing!Jane, stop this crazy thing!

►There are three ways to halt a thread:There are three ways to halt a thread: The wrong wayThe wrong way

►Thread.suspend(), Thread.stop(), Thread.suspend(), Thread.stop(), Thread.destroy()Thread.destroy()

The long wayThe long way►The thread tests a control flag in the instanceThe thread tests a control flag in the instance

The pull-the-rug-out wayThe pull-the-rug-out way►Thread.interrupt(), destroy a dependent Thread.interrupt(), destroy a dependent

resourceresource

Page 34: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

DemoDemo

halt/SieveThread.javahalt/SieveThread.java

Page 35: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Three Concurrency RisksThree Concurrency Risks

►AtomicityAtomicity An atomic operation permits no interruptions.An atomic operation permits no interruptions. The JMM promises that 32bit reads and writes The JMM promises that 32bit reads and writes

are atomic.are atomic. Any types greater than 32bits in size should Any types greater than 32bits in size should

be protected with a monitor.be protected with a monitor. No multi-step operation can ever be atomic No multi-step operation can ever be atomic

without the use of a monitor.without the use of a monitor. Fields marked as Fields marked as volatilevolatile are always are always

completely flushed at every write (even 64bit completely flushed at every write (even 64bit ones).ones).

Page 36: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Three Concurrency RisksThree Concurrency Risks

►OrderingOrdering In a In a synchronizedsynchronized block, instructions are block, instructions are

not reordered (as-if-serial). This is the not reordered (as-if-serial). This is the same as within try-catch-finally blocks.same as within try-catch-finally blocks.

When another thread is watching the When another thread is watching the fields of the synchronized block, it fields of the synchronized block, it perceives non-reordered semantics as perceives non-reordered semantics as well. well.

Page 37: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Three Concurrency RisksThree Concurrency Risks

class Test {

static int i = 0;

static int j = 0;

static void one()

{

i++; j++;

}

static void two()

{

System.out.println("i=" + i + " j=" + j);

}

}

class Test {

static int i = 0;

static int j = 0;

static synchronized void one() {

i++; j++;

}

static synchronized void two() {

System.out.println("i=" + i + " j=" + j);

}

}

Page 38: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Three Concurrency RisksThree Concurrency Risks

class Test {

static int i = 0;

static int j = 0;

static void one()

{

i++; j++;

}

static void two()

{

System.out.println("i=" + i + " j=" + j);

}

}

class Test {

static volatile int i = 0;

static volatile int j = 0;

static void one()

{

i++; j++;

}

static void two()

{

System.out.println("i=" + i + " j=" + j);

}

}

Page 39: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Three Concurrency RisksThree Concurrency Risks

►VisibilityVisibility Reads and writes of instance fields must act Reads and writes of instance fields must act

on the same data, even on different CPUs.on the same data, even on different CPUs. Exiting a monitor flushs all writes from the Exiting a monitor flushs all writes from the

variable cache to main memory.variable cache to main memory. Acquiring a monitor forces the JVM to Acquiring a monitor forces the JVM to

reload any cached variable information.reload any cached variable information. When a thread exits, its cache is flushed to When a thread exits, its cache is flushed to

memory.memory.

Page 40: Threads in Java a tutorial introduction (revision 7) Christian Ratliff cratliff@delorme.com cratliff@delorme.com Senior Technology Architect Core Libraries.

Copyright © 2002, DeLorme

Advanced TopicsAdvanced Topics

►Thread scheduling issues: one-to-one, Thread scheduling issues: one-to-one, many-to-one, many-to-many.many-to-one, many-to-many.

►Thread pooling implementations.Thread pooling implementations.►Deadlock detection and prevention.Deadlock detection and prevention.