Professional Documents
Culture Documents
Mobilesprogramming's Blog
JAVA | SYMBIAN | ANDROID
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
19/07/2012
10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: } }
private static void print(String msg) { String threadName = Thread.currentThread().getName(); System.out.println(threadName + : + msg);
public static void main(String[] args) { try { Runnable r = new Runnable() { public void run() { print(getNextSerialNum()= + getNextSerialNum()); } };
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
2/14
19/07/2012
35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: } } }
Thread.sleep(1500);
Thread.sleep(500);
Thread.sleep(2500);
Lp StaticNeedSync c mt bin thnh vin nextSerialNum vi khai bo private static, c dng lu gi tr cc s tip theo s c to ra (dng 2). Phng thc getNextSerialNum () (dng 416) l c khai bo public v static. Khi gi phng thc ny, n c gi tr hin ti ca nextSerialNum v lu tr n trong mt bin cc b sn (dng 5). Sau , thread gi phng thc ny tm sleep 1 giy(dng 11) mt thread th 2 thc hin. Khi cc thread nhn c mt c hi chy li, n tng nextSerialNum ln 1 bin chun b cho cuc gi tip theo (dng 14). Main thread c 4 thread tng tc vi phng thc getNextSerialNum(). C 4 thread u s dng cng mt i tng Runnable(dng 25-30). Main thread bt u vi threadA(dng 33) v sleep khong 1,5 giy. Thi gian ny threadA vo v tr v t phng thc getNextSerialNum(). Tip theo, main Thread tip tc bt u vi threadB(dng 38), sau n sleep khong 0,5 giy(dng
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/ 3/14
19/07/2012
40) trc khi n bt u threadC(dng 43). C threadB v threadC cng nm trong phng thc getNextSerialNum(), v iu ny lm ny sinh mt vi vn . Sau khi ch 2,5 giy (nhiu thi gian cho threadB v threadC tr v), main thread bt u threadD (dng 45-48). threadD gi phng thc getNextSerialNum () ln cui cng. Kt qu di y m t v d trn: threadA: getNextSerialNum()=10001 threadB: getNextSerialNum()=10002 threadC: getNextSerialNum()=10002 threadD: getNextSerialNum()=10004 gii quyt vn trn, ta ch cn thm vo t kha synchronized vo phng thc getNextSerialNum(). 1: public class StaticSync extends Object { 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/ 4/14
// Simulate a delay that is possible if the thread // scheduler chooses to swap this thread off the // processor at this point. The delay is exaggerated // for demonstration purposes. try { Thread.sleep(1000); } catch ( InterruptedException x ) { }
19/07/2012
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42:
private static void print(String msg) { String threadName = Thread.currentThread().getName(); System.out.println(threadName + : + msg); }
public static void main(String[] args) { try { Runnable r = new Runnable() { public void run() { print(getNextSerialNum()= + getNextSerialNum()); } };
Thread.sleep(1500);
Thread.sleep(500);
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
19/07/2012
43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: } } }
threadC.start();
Thread.sleep(2500);
Kt qu t v d trn: threadA: getNextSerialNum()=10001 threadB: getNextSerialNum()=10002 threadC: getNextSerialNum()=10003 threadD: getNextSerialNum()=10004 S d vn trn c gii quyt l do khi threadC vo phng thc getNextSerialNum(), n lp tc ri vo trng thi block v i threadB kt thc n mi c php tip tc thc hin trong phng thc getNextSerialNum(). 4.S dng Class-level lock trong synchronized statement. s dng synchronized statement trong Class-level lock, ta s dng theo c php sau: synchronized ( ClassName.class ) { // body } V d: ta to lp StaticBlock m phng k thut trn nh sau: 1: public class StaticBlock extends Object {
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/ 6/14
19/07/2012
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
System.out.println(leaving staticA()); }
System.out.println(leaving staticB()); }
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
19/07/2012
27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: } } }; };
Trong lp StaticBlock, phng thc staticA() c khai bo synchronized v static. Phng thc staticB() c khai bo static v c cha mt block synchronized(dng 14-20). Cc i tng s dng kim sot truy cp vo block ny l i tng Class cho StaticBlock v c tm thy bng cch s StaticBlock.class(dng 14). Trong phng thc main, threadA c bt u v gi phng thc staticA()(dng 28). Sau mt khong thi gian 200 mili giy, threadB bt u v gi phng thc staticB(). Trong khi threadA sleep trong phng thc staticA(), threadB vo phng thc staticB(), in mt message v i vo
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/ 8/14
19/07/2012
block static synchronized(dng 14). Khi threadA tr v t staticA(), threadB nhn class-level lock v hon tt phng thc staticB(). Sau y l kt qu ca v d trn: 1: entering staticA() 2: entering staticB() 3: leaving staticA() 4: in staticB() inside sync block 5: leaving staticB() Lu rng, mc d threadB c th vo phng thc staticB()(dng 2) nhng n khng th vo thc hin block synchronized(dng 4) cho n khi threadA tr v t staticA()(dng 3). ThreadB s ri vo trng thi block cho ti khi threadA gii phng class-level lock. 5.Deadlocks: Khi s dng nhiu thread truy cp vo cc i tng c gi lock, nu khng cn thn th rt c th xy ra tnh trng deadlocks. l khi mt threadA nm gi lock1, mt threadB nm gi lock2. Trong khi threadA nm gi lock1, n li mun nm gi thm lock2, nhng v threadB ang nm gi lock2 nn threadA s ri vo trng thi block, v n phi i cho ti khi threadB gii phng lock2. Tuy nhin, vo thi im , trong khi threadB ang nm gi lock2, n li mun tip tc nm gi lock1, v lock1 ang c nm gi bi threadA, nn threadB li ri vo trng thi block. Lc ny, c threadA v threadB ri vo trng thi block mi mi v phi i thread kia gii phng lock. y chnh l trng hp deadlocks. V d: 1: public class Deadlock extends Object { 2: 3: 4: 5: 6: 7: 8: public synchronized void checkOther(Deadlock other) {
9/14
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
19/07/2012
9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: } }
print(entering checkOther());
print(leaving checkOther());
print(leaving action());
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
19/07/2012
34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
public static void threadPrint(String msg) { String threadName = Thread.currentThread().getName(); System.out.println(threadName + : + msg); }
public static void main(String[] args) { final Deadlock obj1 = new Deadlock(obj1); final Deadlock obj2 = new Deadlock(obj2);
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
19/07/2012
59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: }; }
obj2.checkOther(obj1);
threadPrint(finished sleeping);
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
19/07/2012
84: 85: }
Trong phng thc main(), c hai instance ca Deadlock c to ra v hai thread c bt u, mi thread chy trn mt instance; instance th nht c thit lp l obj1, ci kia l obj2(dng 4243). ThreadA c start v gi phng thc checkOther() ca obj1 v gn mt tham chiu n obj2(dng 47), sau khi sleep khong 200 mili giy, threadB c start v gi phng thc checkOther() ca obj2 v n gn mt tham chiu ti obj1. Phng thc checkOther() l synchronized nn thread truy cp vo n s nhn lock. C threadA v threadB s khng bao gi thc hin c phng thc action(), v khi threadA ang trong phng thc checkOther() n nm gi lock trn obj1, th threadB cng trong phng thc checkOther() v nm gi lock ca obj2. Khi threadA c gng gi phng thc action() trong obj2, n s b block do threadB ang nm gi lock trn obj2, v n s i cho ti khi threadB gii phng lock trn obj2. Sau , threadB gi phng thc action() trn obj1 v cng ri vo trng thi block do lock trn obj1 b threadA chim gi; main thread sleep trong 5 giy khi deadlock c to ra(dng 66), khi tnh dy, n c gng ph v deadlock bng cch interrupt threadA(dng 72), sau 1 giy n tip tc c gng interrupt threadB. Tht khng may, iu ny khng ph v deadlock, bi khi mt thread ang b block v i c c lock, n khng p ng vi interrupt. Kt qu t v d trn: threadA: objID=obj1 entering checkOther() threadB: objID=obj2 entering checkOther() threadA: objID=obj1 in checkOther() about to invoke other.action() threadB: objID=obj2 in checkOther() about to invoke other.action() main: finished sleeping main: about to interrupt() threadA main: about to interrupt() threadB main: did that break the deadlock? Bn thy bi vit ny th no? Cc bi lin quan: Tm hiu Thread trong Java Phn 4 08/12/2010 mobilesprogramming Categories: Thread Tags: java thread
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
13/14
19/07/2012
https://mobilesprogramming.wordpress.com/2010/08/12/tim-hiu-thread-trong-java-phn-5/
14/14