2013년 12월 27일 금요일

java concurrency in practice ch1

ch1 - introduction

멀티 쓰레드 프로그램은 복잡하다 근대 왜 써야 하는가?

멀티 프로세서에서 가장 쉽게 확장 할 수 있음으로


1.1 brief history of concurrency
in the acient past,
running only a single program at a time was an inefficient use of expensive and scarce computer resource

os evolved and run more than one program to run at once
running individual programs in process
if they need to, processes can communicate with one another

Resouce utilization
it it more efficient to use that wait time to let another program run

Fairness
let them(users, processes) share the computer via finger-grained time slicing

Convenience
wrtie serverel programs that each perform a single task and have them coordinate wite each other


"virtual von Neumann computer it had a memory space storing both instructions and data
, executing instructions sequentially" mean sequential programming model

Threads allow multiple streams of program control flow to coexist within a process
thread share process-wide resource such as memory and file handler
thread not share program counter, stack, local valiables

multiple threads within the same program can be scheduled simultaneouly on multiple CPUs


most moderan os treat threads as the basic unit of scheduling
thread share memory so it allows finer-grained data sharing than inter-process
but modify variables that another thread is in the middle of using, with unpreditable results

프로세스 는 여러개의 쓰레드로 이루어질수 있음
쓰레드는 프로세스의 메모리공간 공유
쓰레드의 스텍, 로컬 변수, 프로그램 카운터는 따로씀

하나의 프로그램을 쉽게 여러개의 코드로 분리 한 후 서로 협동하게 만들 수 있음
또한 멀티 cpu에도 쉽게 배분할 수 있음

문제는.. 쓰레드 관리가 힘들다는거!

1.2 Benefits of Threads
thread make it easier to program, by turing asynchronous workflow into mostly sequetial ones

1.2.1 Exploiting Multiple Processors
single thread program run at just one processor if 100 cpus than 99% of processor remains idels

1.2.3. simplicity of modeling
it is better assinging a thread to each type of task than manaing multiple different types of task at once
thread is used by framewoks such as servlets or RMI(servlet writers do not need to worry about mulitple user connections)

1.2.3 simplified Handling of Asynchronous Events
1.2.4 More Responsive User Interfaces
if long-running taks is executed in a serperate thread , the event thread remains free to prcess UI events, making thre UI more responsive

장점
1.여러개의 코어 사용가능
2.하나의 쓰레드만 노출하고 그 쓰레드와 작동하는 다른 쓰레드들을 감춤으로서 쉽게 프로그램 개발가능
(framework 개발에사용)
복잡한 일을 몇개의 쓰레드로 분리해서 작성 후 서로 연동하게함
3.비동기 이벤트 다루는대 좋음
nio
4.UI에서 오래 걸리는 일을 다른 쓰레드에서 실행 시킴으로서 유저의 입력에 대한 반응을 계속 할수 있음


1.3 Risks of Thread
thread is more esoteric, concurrency was an "advancded" topic

1.3.1 Safety Hazards