You are on page 1of 6

19/07/2012

Tm hiu Thread trong JAVA Phn 3 Mobilesprogramming's Blog

Mobilesprogramming's Blog
JAVA | SYMBIAN | ANDROID

Tm hiu Thread trong JAVA Phn 3


Bi 3: Tm Hiu Mt S Phng Thc Hu Dng 1.Phng thc sleep() Thread.sleep lm cho thread hin hnh nh ch thc hin trong mt thi gian quy nh. y l mt phng tin hiu qu lm cho thi gian x l i vi cc thread khc ca mt ng dng hoc cc ng dng khc m c th c chy trn mt h thng my tnh. Phng thc sleep() cng c th c s dng cho pacing, nh trong v d sau y, v ch i mt thread vi nhim v c hiu l c yu cu v thi gian, nh vi v d SleepMessages bn di. C 2 overload ca phng thc sleep() c cung cp: mt xc nh thi gian sleep tnh bng mili giy v mt xc nh thi gian sleep bng nano giy. Tuy nhin, nhng kiu thi gian ny khng m bo chnh xc, bi chng c gii hn bi nhng c s nm bn di h iu hnh. Ngoi ra, thi gian sleep c th c kt thc bi interrupt, m chng ta s gii thiu trong phn sau. Trong mi trng hp, bn khng th gi nh rng vic gi phng thc sleep s nh ch thread v khong thi gian chnh xc cho trc. V d SleepMessages s dng phng thc sleep() in message sau 4 giy:

mobilesprogramming.wordpress.com/2010/08/11/tim-hiu-thread-trong-java-phn-3/

1/6

19/07/2012

Tm hiu Thread trong JAVA Phn 3 Mobilesprogramming's Blog

pbi casSepesgs{ ulc ls leMsae pbi sai vi mi(tigag[)trw Itrutdxeto { ulc ttc od anSrn rs] hos nerpeEcpin Srn ipratno]={ tig motnIf[ "ae etot" Mrs a as, "osetot" De a as, "itelmsetiy, Ltl ab a v" " kdwl etiyto A i il a v o" } ; fr(n i=0 i<ipratnolnt;i+ { o it ; motnIf.egh +) /Puefr4scns /as o eod Tra.le(00; hedsep40) /Pitamsae /rn esg Sse.u.rnl(motnIf[]; ytmotpitnipratnoi) } } } Ch : phng thc main() nm mt exception l InterruptedException. y l exception m phng thc sleep() s nm nu mt thread khc ngt thread hin hnh trong khi sleep() ang kch hot. Nu ng dng khng nh ngha mt thread khc lm gin on(interrupt), th khng cn bn tm ti InterruptedException. 2.Phng thc wait() v notify() C ch chim dng ng b ha ngn cho cc tuyn on chng cho nhau. Nhng trong mt s trng hp ta cng cn phi cung cp mt cch no cc tuyn on truyn tin vi nhau. Java cung cp cho ngi s dng cc phng thc cho php cc tuyn on khng b x l chng cho m vn c th trao i thng tin vi nhau bng cch s dng cc phng thc wait() v notify(). thc hin iu ny phng thc wait() c nh ngha cho php mt tuyn on i cho ti khi mt iu kin no xy ra. Phng thc notify() c nh ngha bo cho cc tuyn on bit s kin no xy ra. Cc phng thc ny c nh ngha trong lp Object v c tha k t cc lp Object. Ta tm hiu v d sau : public class WaitNotify extends Thread { public static void main(String args[]) throws Exception{ Thread notificationThread = new WaitNotify(); notificationThread.start(); // Ch tuyn on cnh bo kch hot s kin synchronized (notificationThread){ notificationThread.wait(); }
mobilesprogramming.wordpress.com/2010/08/11/tim-hiu-thread-trong-java-phn-3/ 2/6

19/07/2012

Tm hiu Thread trong JAVA Phn 3 Mobilesprogramming's Blog

// Bo cho ngi dng bit phng thc wait hon thnh System.out.println (The wait is over); } public void run(){ System.out.println (Hit enter to stop waiting thread); try { System.in.read(); } catch (java.io.IOException ioe){ // no code reqd } // Notify any threads waiting on this thread synchronized (this){ this.notifyAll(); } } } Mt s dng ca phng thc wait v notify Tt c cc phng thc u c trong lp Object v hot ng trn i tng hin thi: public final void wait(long timeout) throws InterruptedException Tuyn on hin thi ch cho ti khi c cnh bo hoc mt khong thi gian timeout nht nh. Nu timeout bng 0 th phng thc s ch ch cho ti khi c cnh bo v s kin. public final void notify() Cnh bo t nht mt tuyn on ang ch mt iu kin no thay i. Cc tuyn on phi ch mt iu kin thay i trc khi c th gi phng thc wait no . public final void notifyAll() Phng thc ny cnh bo tt c cc tuyn on ang ch mt iu kin thay i.Cc tuyn on ang ch thng ch mt tuyn on khc thay i iu kin no . Trong s cc tuyn on c cnh bo, tuyn on no c u tin cao nht th s chy trc tin. 3.Mt s phng thc v Interrupting Thread Mt Thread c xem l kt thc khi phng thc run() ca n c tr v. Khng c cch no bt buc mt Thread kt thc. Tuy nhin, phng thc interrupt() c th c s dng yu cu mt Thread kt thc. Khi mt Thread gi phng thc interrupt(), trng thi interrupted ca Thread c thit lp. l mt flag biu Boolean i din cho mi Thread. Thnh thong, mi Thread phi t kim tra
mobilesprogramming.wordpress.com/2010/08/11/tim-hiu-thread-trong-java-phn-3/ 3/6

19/07/2012

Tm hiu Thread trong JAVA Phn 3 Mobilesprogramming's Blog

xem n b interrupted hay cha. Mt s phng thc interrupt ca Thread: void interrupt(): Gi mt request interrupt vo Thread. Trng thi interrupted ca Thread s c thit lp = true. Phng thc ny n gin set flag trong Thread ch cho bit rng n b interrupted v c return. Phng thc ny c th nm mt ngoi l SecurityException, cho bit rng Thread yu cu interrupted khng c quyn interrupt Thread khc. V d: 1: public class SleepInterrupt extends Object implements Runnable { 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: public static void main(String[] args) { SleepInterrupt si = new SleepInterrupt();
4/6

public void run() { try { System.out.println( in run() about to sleep for 20 seconds); Thread.sleep(20000); System.out.println(in run() woke up); } catch ( InterruptedException x ) { System.out.println( in run() interrupted while sleeping); return; }

System.out.println(in run() doing stuff after nap); System.out.println(in run() leaving normally); }

mobilesprogramming.wordpress.com/2010/08/11/tim-hiu-thread-trong-java-phn-3/

19/07/2012

Tm hiu Thread trong JAVA Phn 3 Mobilesprogramming's Blog

21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: } }

Thread t = new Thread(si); t.start();

// Be sure that the new thread gets a chance to // run for a while. try { Thread.sleep(2000); } catch ( InterruptedException x ) { }

System.out.println( in main() interrupting other thread); t.interrupt(); System.out.println(in main() leaving);

Kt qu: in run() about to sleep for 20 seconds in main() interrupting other thread in main() leaving in run() interrupted while sleeping static boolean interrupted(): Kim tra trng thi interrupted ca Thread hin ti. Lu y l phng thc static. Phng thc ny tr v true nu Thread hin ti b interrupted v flag interrupted vn cha c clear. Gi Thread.interrupted() l mt cch th 2 tr v false cho n khi Thread b reinterrupted. V d: 1: public class InterruptReset extends Object { 2: public static void main(String[] args) {
5/6

mobilesprogramming.wordpress.com/2010/08/11/tim-hiu-thread-trong-java-phn-3/

19/07/2012

Tm hiu Thread trong JAVA Phn 3 Mobilesprogramming's Blog

3: 4: 5: 6: 7: 8: 9: 10: 11: } }

System.out.println( Point X: Thread.interrupted()= + Thread.interrupted()); Thread.currentThread().interrupt(); System.out.println( Point Y: Thread.interrupted()= + Thread.interrupted()); System.out.println( Point Z: Thread.interrupted()= + Thread.interrupted());

Kt qu: Point X: Thread.interrupted()=false Point Y: Thread.interrupted()=true Point Z: Thread.interrupted()=false Ban u, ti im X, trng thi interrupted = false, sau Thread hin ti b interrupt ti dng(5). Ti Y, trng thi interrupted = true, v n t ng reset li = false bi Thread.interrupted() (dng 6-7 ). Cui cng, kim tra trng thi ti Z v kt qu bng false. boolean isInterrupted(): Kim tra xem Thread ny b interrupt hay cha. Gi phng thc ny khng nh hng n trng thi interrupt ca Thread ny. Bn thy bi vit ny th no? Cc bi vit lin quan: Tm hiu Thread trong Java Phn 2 Tm hiu Thread trong Java- Phn 4 08/11/2010 mobilesprogramming Categories: Thread Tags: java thread Blog at WordPress.com. | Theme: Dark Wood by Nischal Maniar.

mobilesprogramming.wordpress.com/2010/08/11/tim-hiu-thread-trong-java-phn-3/

6/6

You might also like