(, )..,., (!)....... Timer.,.,.. 8 9.,..
14.,. (http :/ / www.j avaworld.com)........,... ThreadRunnable synchronized, wait (), notify (). Thread AWT,. C+ +, David FlanaganJ ava in a Nutshell C+ +. C+ +, Perter van der LindenJ ust J ava 2, 4th Edition.
15 http :/ / www.holub.com.. bugs @holub.com.., ( ). This program includes code from Allen Holub' s book Taming J ava Threads. 2000 Allen I. Holub. All rights reserved. http :/ / www.holub.com.,...,..,,.,.
. GUI,..,...,. ( )........
18 Chapte r 1.,.,.,.,.... (AWT) ( 1.1AWT + AWT ). AWT..., main (), AWT. (. ) AWT., AWT ( AWT., UI ).,. AWT ( )., (race condition). synchronized.
19 AWT.... AWT, ( ). [1.1]. Sleep Hello. Sleep 5 ( ). Hello Hello World. Sleep 5 Hello. Hello Hello World.,. 1.1 /text/books/threads/ch1/hang.java. 01: import javax.swing.*; 02: import java.awt.*; 03: import java.awt.event.*; 04: 05: class Hang extends JFrame 06: { 07: publi c Hang() 08: { JButton b1 = newjbutton( "Sleep" ); 09: JButton b2 = newjbutton( "Hello" ); 10: 11: b1.addact ionlistener 12: ( newact ionlistener () 13: { public void act ionperformed( Act ionevent event ) 14: { t ry 15: { Thread.currentThread().sleep(5000);
20 Chapte r 1 16: } 17: cat ch(except ion e){} 18: } 19: } 20: ); 21: 22: b2.addact ionlistener 23: ( newact ionlistener () 24: { public void act ionperformed( Act ionevent event ) 25: { System.out.print ln("hel lo wor ld"); 26: } 27: } 28: ); 29: 30: getcontentpane().setlayout ( new FlowLayout () ); 31: getcontentpane().add( b1 ); 32: getcontentpane().add( b2 ); 33: pack(); 34: show(); 35: } 36: 37: public stat ic void main( St ring[] args ) 38: { newhang(); 39: } 40: } GUI...,, (AWT )., UI. ( AWT )..
2 1.. ( % ).....,.,. (!).,...,..... (Doug SchmidtACE. http :/ / www.cs.wustl.edu/ ~ schmidt/ ACE.html ( ACE J ACE. http :
22 Chapte r 1 / / www.cs.wustl.edu/ ~ schmidt/ J ACE.html ).,..?.,. (, )...... ( ).. (new ). (,.,. )....,..
23., ( ).. ( ).,.....,. ( )... (.,. )..,..,..
24 Chapte r 1. (semaphore).. ( )..... Dijkstra.. synchronized. synchronized.. J NI (J ava
25 Native Interface) OS.... synchronized,. [1.2].. [1.2]13 test (...) 1,000,000.. (200MHz P5, NT4/ SP3, J DK 1.2.1 Hot Spot 1.0fcs, build E). %java -verbose:gc Synch Pass 0: Time lost : 234ms. 121.39%increase Pass 1: Time lost : 139ms. 149.29%increase Pass 2: Time lost : 156ms. 155.52%increase Pass 3: Time lost : 157ms. 155.87%increase Pass 4: Time lost : 157ms. 155.87%increase Pass 5: Time lost : 155ms. 154.96%increase Pass 6: Time lost : 156ms. 155.52%increase Pass 7: Time lost : 3,891ms. 1, 484.7%increase Pass 8: Time lost : 4, 407ms. 1,668.33%increase test (). Pass 0.,. (Pass 6). 1.5. Pass 7Pass 8.,
26 Chapte r 1. Pass.. 150....,. atomic-bit-test- and- set (.,, ).,.,. (0), (1).... (), ( [System call] ).. NT 600.,. Pass 7Pass8. Sun Alexander Garthwaite.
27. (.., monitorentermonitorexit ACC_SYNCHRONIZED. ). ( [release] )..,.,. (thin-lock)/ (fat-lock)., (16 ~ 64).., waitnotify...,. hashcode (). ( )...,.
28 Chapte r 1 1.2 /text/books/threads/ch1/synch.java 01: import java.ut i l.*; 02: import java.t ext.numberformat ; 03: /**... <ht tp:/ / java.sun.com/ products/ hot spot/q+a.html.> */ 04: class Synch 05: { 06: private stat ic long[] locking_t ime = new long[100]; 07: private static long[] not_ locking_time = new long[100]; 08: private static final int ITERATIONS = 1000000; 09: 10: synchronized long locking (long a, long b){ret urn a + b; } 11: long not_locking (long a, long b){return a + b; } 12: 13: private void test ( int id ) 14: { 15: long start = System.currentTimeMillis(); 16: 17: for (long i = ITERATIONS; -- i >= 0 ; ) 18: { locking(i, i ); 19: } 20: 21: locking_time[id] = System.currentTimeMillis() - start ; 22: start = System.currentTimeMillis (); 23: 24: for (long i = ITERATIONS; -- i >= 0 ; ) 25: { not_ locking(i, i); 26: } 27: 28: not_ locking_time[id] = System. currenttimemillis () - start ; 29: } 30: 31: stat ic void print_results( int id )
29 32: { 33: 34: NumberFormat compositor = NumberFormat.getInstance(); 35: compositor.setmaximumfract iondigits( 2 ); 36: 37: double t ime_in_synchronizat ion = locking_t ime[id] - not_ locking_t ime[ id]; 38: 39: System.out.pr int ln( "Pass " + id + ": Time lost : " 40: + compositor.format ( t ime_in_synchronizat ion ) 41: + " ms. " 42: + compositor.format ( ((double) locking_t ime[id]/ not_ locking_t ime[id])*100.0 ) 43: + "%increase" 44: ); 45: } 46: 47: stat ic public void main(string[] args) throws InterruptedExcept ion 48: { 49: / /. 50: 51: final Synch tester = newsynch(); 52: t ester. test (0); print_results (0); 53: t ester. test (1); print_results (1); 54: t ester. test (2); print_results (2); 55: t ester. test (3); print_results (3); 56: t ester. test (4); print_results (4); 57: t ester. test (5); print_results (5); 58: t ester. test (6); print_results (6); 59: 60: / /. 61: / / HotSpot 62: / /. 63: 64: final Object start_gate = newobject (); 65: 66: Thread t 1 = newthread() 67: { public void run() 68: { t ry{ synchronized(start_gate) { start_gate.wait (); } } 69: catch( InterruptedExcept ion e ){} 70: 71: t ester. test (7); 72: } 73: };
30 Chapte r 1 74: Thread t2 = newthread() 75: { public void run() 76: { t ry{ synchronized(start_gat e) { start_gat e.wait (); } } 77: cat ch( InterruptedExcept ion e ){} 78: 79: tester.test (8); 80: } 81: }; 82: 83: Thread.currentThread().setPriority( Thread.MIN_PRIORITY ); 84: 85: t 1.start (); 86: t2.start (); 87: 88: synchronized(st art_gate){ start_gate.not ifyal l (); } 89: 90: t 1. join(); 91: t2. join(); 92: 93: print_results( 7 ); 94: print_results( 8 ); 95: } 96: {,.,. (. ).. (, )...
3 1 :...,.. longdouble. ( ). class Unreliable { private long x; } public long get_x( ) { ret urn x; } public void set_x(long value ) { x = value; }, obj.set_x( 0 );, obj.set_x( 0x0123456789abcdef );. x = value; 64 32. x.high_word = value. high_word; x. low_word = value. low_word;
32 Chapte r 1, 64. 32 32,. 64JVM( ). 32.,., x 0x0123456789abcdef, 0x0123456700000000, 0x0000000089abcdef 0x0000000000000000. set_x ()get_x(). volatile volatile.,... volatile,. public volatile. public. volatile..,. 32 ( ).,, long double., xint.
33. x = + + y x + = y xy.,. synchronized. (Race Condition),.. synchronized, boolean. synchronized. (Immutability). (Immut able object),. String. String.. string1 + = string2 string1 string2..., (., ). final...,.
34 Chapte r 1 class I_am_immutable { private f i nal int MAX_VALUE = 10; pr ivate f i nal int blank_final; } public I_am_ immutable( int init ial_value ) { blank_final = init ial_value; },,.,.. (Synchronization Wrappers).. 2. J DK 1.1 ( ),.. ( ).. Gang-of-Four (Decorator Pattern, ) ( Gang-of-Four Erich Gamma, Richard Helm, Ralph J ohnson, J ohn Vlissides Design Patterns : Elements of Reusable Object- Oriented Software, Addison Wesley, 1995.. GoF )... j ava.io., BufferedInputStreamInput Stream
35 InputStream. Buffered Input- Stream InputStream, InputStream., BufferedInputStreamFile- InputStream, FileInputStreamread(), BufferedInputStreamread(). FileInput Stream BufferedInput Stream..,. 01: interface Some_interface 02: { public Object message(); 03: } 04: 05: class Not_t hread_safe implements Some_interface 06: { 07: public Object message() 08: { / /. 09: return null; 10: } 11: } 12: 13: class Thread_safe_wrapper implements Some_ interface 14: { 15: Some_interface not_t hread_safe; 16: 17: public Thread_safe_wrapper ( Some_interface not_t hread_safe ) 18: { this.not_thread_safe = not_thread_safe; 19: } 20: 21: public Some_interface ext ract () 22: { return not_t hread_safe(); 23: } 24: 25: public synchroni zed Object message() 26: { return not_t hread_safe.message(); 27: } 28: }
36 Chapte r 1 Not_thread_safe.,. Some_interface object = newnot_thread_safe(); / /... object = newthread_safe_wrapper (object ); / /.,. object = ((Thread_safe_wrapper)object ).ext ract ();.? (Concurrency) (Parallelism)..,. [ 1.1].. CPU. 1 2 1 2 1.1
37., (,, )..,.,..,..,.,., CPU.,....., NT.
38 Chapte r 1 10 (, ). 2 3 1., 10., 10.. NT. NT 7. 7 10.,., 12 NT 1, 8, 9, 10NT 7. NT. NT. NT (Priority boosing). C,., NT I/ O,.,. I/ O. UI.....,.,
39.,. NT. NT.,,. NT.,. NT (Priority classes). [ 1.2]. ( 7 31., 7 ). 22 ( NT ).. NT 1.2 NT Idle 1 6 15. Normal 1, 6 10 15.. 1, 7 10 15., Idle, Normal. High 6.
40 Chapte r 1 NT., (boosting)....,... NT.,, NT. NT.? NT,.., set- Priority(), Thread.MAX_PRIORITYThread.MIN_PRIORITY Thread. NORM_PRIORITY. 7 10., os.name NT., Sun... Sun MS( ) JNI., RNI., MS 3158 RNI JNI.
4 1, NORM_PRIORITY..!., ( ). (starvation) (,. ).. (, 3.x. 3.1 ).., (, ). C setjump/ longj ump....,..
42 Chapte r 1 1.... 2... Timer. Timer,. Timer (time slice)... (, ).,.... ( ). 3.1,. NT, (, NT. (fiber). (fiber)., )....
43,. NT[ 1.3] 1:1. 1.3 NT NT.,,. (, 600 )... [ 1.4].
44 Chapte r 1 1.4 (lightweight process. LWP)... (pool), CPU.,, CPU. CPU. (, ).,,.. (, ). CPU..
45... 1.,.. NT. 2...... ( ), ( )....,..,.?.,..
46 Chapte r 1 1.. synchronized. 2..,. yield()sleep () (, )., 100 yield(), (yield(),. sleep (), )..,., synchronized. yield()sleep () I/ O.,.. NTNT7 10., 2 1. NT.