자바 쓰레드 능숙하게 다루기

Similar documents
PowerPoint 프레젠테이션

05-class.key

02 C h a p t e r Java

Microsoft PowerPoint - 04-UDP Programming.ppt

rmi_박준용_final.PDF

10장.key

제11장 프로세스와 쓰레드

PowerPoint 프레젠테이션

JMF3_심빈구.PDF

Chap12

11 템플릿적용 - Java Program Performance Tuning (김명호기술이사)

자바GUI실전프로그래밍2_장대원.PDF

자바 프로그래밍


ch09

PowerPoint 프레젠테이션

비긴쿡-자바 00앞부속

6주차.key

12-file.key

Microsoft PowerPoint - 14주차 강의자료

쉽게 풀어쓴 C 프로그래밍

01-OOPConcepts(2).PDF

OOP 소개

초보자를 위한 자바 2 21일 완성 - 최신개정판

Interstage5 SOAP서비스 설정 가이드

9장.key

<4D F736F F F696E74202D20C1A63139C0E520B9E8C4A120B0FCB8AEC0DA28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Microsoft PowerPoint - Java7.pptx

신림프로그래머_클린코드.key

untitled

5장.key

09-interface.key

11장.key

제8장 자바 GUI 프로그래밍 II

PowerPoint Presentation

초보자를 위한 C# 21일 완성

untitled

03-JAVA Syntax(2).PDF

PowerPoint Presentation

Java ~ Java program: main() class class» public static void main(string args[])» First.java (main class ) /* The first simple program */ public class

PowerPoint Presentation

Chap7.PDF

1

ePapyrus PDF Document

mytalk

자바-11장N'1-502

Analytics > Log & Crash Search > Unity ios SDK [Deprecated] Log & Crash Unity ios SDK. TOAST SDK. Log & Crash Unity SDK Log & Crash Search. Log & Cras

PowerPoint Presentation

gnu-lee-oop-kor-lec10-1-chap10

소프트웨어 개발의 성공 열쇠 - 오브젝트 디자인

07 자바의 다양한 클래스.key

fundamentalOfCommandPattern_calmglow_pattern_jstorm_1.0_f…

<4D F736F F F696E74202D20C1A63233C0E520B1D7B7A1C7C820C7C1B7CEB1D7B7A1B9D628B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Microsoft PowerPoint - RMI.ppt

PowerPoint 프레젠테이션

Java Programing Environment

Microsoft PowerPoint - java2-lecture6.ppt [호환 모드]

(Microsoft PowerPoint - java2-lecture6.ppt [\310\243\310\257 \270\360\265\345])

Spring Data JPA Many To Many 양방향 관계 예제

@OneToOne(cascade = = "addr_id") private Addr addr; public Emp(String ename, Addr addr) { this.ename = ename; this.a

<4D F736F F F696E74202D20C1A63138C0E520C0CCBAA5C6AE20C3B3B8AE28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

강의자료

10주차.key

( )부록

Network Programming

ilist.add(new Integer(1))과 같이 사용하지 않고 ilist.add(1)과 같이 사용한 것은 자바 5.0에 추가된 기본 자료형과 해당 객체 자료 형과의 오토박싱/언박싱 기능을 사용한 것으로 오토박싱이란 자바 컴파일러가 객체를 요구하는 곳에 기본 자료형

초보자를 위한 C++

Microsoft PowerPoint - lec12 [호환 모드]

<4D F736F F F696E74202D20C1A63236C0E520BED6C7C3B8B428B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Design Issues

I T C o t e n s P r o v i d e r h t t p : / / w w w. h a n b i t b o o k. c o. k r

PowerPoint Presentation

Spring Boot/JDBC JdbcTemplate/CRUD 예제

스레드를적용하지않은결과와스레드를적용한결과의비교 1) 두개의작업을스레드를사용하지않고수행한예 ) : 순차작업 class ThreadTest2 { System.out.print("-");// 화면에 - 를출력하는작업 System.out.print(" ");// 화면에 를출력

Java

* Factory class for query and DML clause creation * tiwe * */ public class JPAQueryFactory implements JPQLQueryFactory private f

Mobile Service > IAP > Android SDK [ ] IAP SDK TOAST SDK. IAP SDK. Android Studio IDE Android SDK Version (API Level 10). Name Reference V

Something that can be seen, touched or otherwise sensed

14-Servlet

Connection 8 22 UniSQLConnection / / 9 3 UniSQL OID SET

MasoJava4_Dongbin.PDF

Microsoft PowerPoint - EEL2 Lecture10 -Swing and Event Handling.pptx

ESP1ºÎ-04

교육2 ? 그림

JAVA PROGRAMMING 실습 08.다형성

스레드의우선순위 우선순위설정메소드 : void setpriority(int newpriority) newpriority 에설정할수있는등급 : 1( 가장낮은우선순위 ) 부터 10( 가장높은우선순위 ) 가장높은우선순위 : MAX_PRIORITY, 보통우선순위 : NORM_

Microsoft PowerPoint - 2강

PowerPoint 프레젠테이션

쉽게 풀어쓴 C 프로그래밍

교육자료

No Slide Title

untitled

03장

105È£4fš

[ 정보 ] 과학고 R&E 결과보고서 Monte Carlo Method 를이용한 고교배정시뮬레이션 연구기간 : ~ 연구책임자 : 강대욱 ( 전남대전자컴퓨터공학부 ) 지도교사 : 최미경 ( 전남과학고정보 컴퓨터과 ) 참여학생 : 박진명 ( 전

<4D F736F F F696E74202D20C1A63230C0E520BDBAC0AE20C4C4C6F7B3CDC6AE203128B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

歯 Final.PDF

FileMaker ODBC and JDBC Guide

SRC PLUS 제어기 MANUAL

Sena Technologies, Inc. HelloDevice Super 1.1.0

Transcription:

(, )..,., (!)....... 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.