at-m42:lecture9
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at-m42:lecture9 [2009/04/15 17:32] – eechris | at-m42:lecture9 [2011/01/14 12:45] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 523: | Line 523: | ||
===== Thread deadlock ===== | ===== Thread deadlock ===== | ||
- | {{ : | + | {{: |
- | Thread deadlock scenario | + | |
- | Thread A enters synchronized method of object foo and gets the key. | + | |
- | Thread A goes to sleep | + | |
- | Thread deadlock scenario | + | |
- | Thread B enters synchronized method of object bar and gets the key. | + | |
- | Thread B tries to enter a synchronized method of object foo, but can't get that key (A has it). | + | |
- | B " | + | |
- | B keeps the bar key. | + | |
- | Thread deadlock scenario | + | |
- | Thread A wakes up and tries to enter a synchronized method on object bar., but can't get that key because B has it. | + | |
- | A " | + | |
- | Thread A can't run until it get's bar key that B is holding. | + | |
- | Thread B can't run until it gets the foo key that A is holding. | + | |
- | Deadlock! | + | |
- | Summary | + | |
- | Single-threaded programming: | + | |
- | Multithreading: | + | |
- | Multithreading makes Java complicated, | + | |
- | Lab Exercises | + | |
- | 1. Inherit a class from Thread and override the run( ) method. Inside run( ), print a message, then call sleep( ). Repeat this 3 times, then return from run( ). Put a start-up message in the constructor and override finalize( ) to print a shut-down message. Make a separate thread class that calls System.gc( ) inside run( ), printing a message as it does so. Make several thread objects of both types and run them to see what happens. | + | |
- | 2. Create two Thread classes, one with a run( ) that starts up, creates and passes its own handle to an object of the second thread class and then calls wait( ). The other classes’ run( ) should cause a notifyAll( ) in the first thread after some number of seconds have passed, so the first thread can print out a message. | + | |
- | {{: | + | ===== Thread deadlock scenario ===== |
- | {{: | + | |
- | ===== Heading 1 ===== | + | {{: |
- | <code groovy 1 | Example 1: Everything is an object | + | * '' |
- | extern> http:// | + | * '' |
- | </ | + | |
- | ---- | + | |
- | Notes ... | + | ===== Thread deadlock scenario ===== |
- | ===== Heading 2 ===== | + | {{: |
- | * [[#Sub head 1]] | + | * '' |
- | * [[#Sub head 2]] | + | * '' |
+ | * B " | ||
+ | * B keeps the '' | ||
+ | ===== Thread deadlock scenario ===== | ||
+ | {{: | ||
+ | * '' | ||
+ | * A " | ||
+ | * '' | ||
+ | * Thread B can't run until it gets the '' | ||
+ | * **Deadlock!** | ||
- | ===== Summary | + | ===== Summary |
- | + | * Single-threaded programming: | |
- | The .... | + | * Multi-threading: |
- | + | * Multi-threading makes programming on the Java Platform complicated. | |
- | + | * Groovy multithreading easier because of the use of the closure. | |
- | * [[#Heading 1]] | + | * **Multi-threading is hard**! |
- | * [[#Heading 2]] | + | |
- | * [[#heading 3]] | + | |
- | + | ||
- | ===== Lab Exercises ===== | + | |
- | + | ||
- | * [[eg-m42: | + | |
---- | ---- |
at-m42/lecture9.1239816724.txt.gz · Last modified: 2011/01/14 12:26 (external edit)