User Tools

Site Tools


at-m42:lecture9

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
at-m42:lecture9 [2009/04/15 17:32] eechrisat-m42:lecture9 [2011/01/14 12:45] (current) – external edit 127.0.0.1
Line 523: Line 523:
 ===== Thread deadlock ===== ===== Thread deadlock =====
  
-{{ :at-m42:deadlock1.png?768 |Deadlock}} +{{:at-m42:deadlock1.png?768 |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 "blocks" waiting for foo's key to become available. +
-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 "blocks" waiting for bar's key to become available. +
-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: live by yourself, own everything, no contention for resources +
-Multithreading: suddenly you can have collisions and destroy information, get locked up over the use of resources +
-Multithreading makes Java complicated, but that’s not Java’s fault. Multithreading is hard +
-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.+
  
-{{:at-m42:deposit.png|}} +===== Thread deadlock scenario =====
-{{:at-m42:deadlock1.png|}}+
  
-===== Heading 1 =====+{{:at-m42:deadlock2.png?344 | Deadlock 2.}}
  
-<code groovy 1 | Example 1: Everything is an object (at-m42/Examples/lecture02/example1.groovy)> +  * ''Thread'' A enters ''synchronized'' method of object ''foo'' and gets the key
-extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture02/example1.groovy +  * ''Thread'' A goes to sleep
-</code> +
-----+
  
-Notes ...+===== Thread deadlock scenario =====
  
-===== Heading 2 =====+{{:at-m42:deadlock3.png?455 |}}
  
-  * [[#Sub head 1]] +  * ''Thread'' B enters ''synchronized'' method of object ''bar'' and gets the key. 
-  * [[#Sub head 2]]+  * ''Thread'' B tries to enter a ''synchronized'' method of object ''foo'', but can't get that key (A has it). 
 +  * B "blocks" waiting for ''foo'''s key to become available. 
 +  * B keeps the ''bar'' key.
  
 +===== Thread deadlock scenario =====
 +{{:at-m42:deadlock4.png?455 |Deadlock1}}
 +  * ''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 "blocks" waiting for ''bar'''s key to become available.
 +  * ''Thread'' A can't run until it gets ''bar'' key that B is holding.
 +  * Thread B can't run until it gets the ''foo'' key that A is holding.
 +  * **Deadlock!**
  
-===== Summary of this Lecture ==== +===== Summary ===== 
-  +  * Single-threaded programming: live by yourself, own everything, no contention for resources 
-The .... +  * Multi-threading: suddenly you can have collisions and destroy information, get locked up over the use of resources 
- +  * 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:labs:lab1|Lab 1]] exercises 1-7.+
  
 ---- ----
at-m42/lecture9.1239816724.txt.gz · Last modified: 2011/01/14 12:26 (external edit)