找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2932|回复: 3
收起左侧

[面经总结] java multi thread 的一些总结

[复制链接]

2

主题

1

精华

30

积分

新米人

Rank: 1

积分
30
发表于 10-16-2016 03:49 AM | 显示全部楼层 |阅读模式

亲!马上注册或者登录会查看更多内容!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 Sophia 于 10-16-2016 12:35 PM 编辑 * `4 m$ K/ ^7 B: Y- \' Q) s5 U
2 {( N1 Y6 r) T
Concurrency and multiple thread
: y- v1 f# E! b4 X1 W5 M# A1 A5 `5 I! }
( Q: K$ q2 ~/ v9 wConcurrency vs Paralle:2 @4 D; u# A; o8 U
. J! y; q' }- r$ g- U5 V$ E+ x
Concurrency并发:multiple task theoretically run simultaneously, semantic(假装看起来是)- 单核CPU multiple task,其实只能处理一个task,但是看上去像是。。fake multi task# n1 d. \! Z7 f' B9 B9 q
实际存在context switch
9 x  ~. H: h# O# i4 `4 D9 I& O- u0 U+ ?$ f% I1 n# d+ Q8 g2 |
Paralle并行: 真实的运行.... multiple tasks physically run simultaneously.
$ j6 b( C( O2 c% N( c( q% b- W7 G/ g# a
Process vs Thread( w7 d$ z, ~  J2 o
Main Difference: Process vs Thread
( q! Z. S3 E3 c' X3 mwhether they have independent memory
3 _; v, Q: L) ^6 O' ?. A1 s! x
* Q; j" r  f9 ^/ S, I0 pProcess:
: Y' _$ r+ ^' V) p' A“A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space”.& Y' f( d$ e# J5 \( \  W

( i# \1 D" h' d( `. u* E4 F- Independent executor of instructions exclusive CPU.独立的计算单元
  I8 ~) R5 ?# R5 z/ T- W- K# ]- independent own memory有独立存储单元,可以access a complete memory space0 i5 U; i0 n1 x" R* ?. u
- IPC communicate :进程之间互相通信。1 ?6 L- `* \, ^' t2 R: B

$ a# Y* g3 a0 Q+ Y9 B: GThread
# b% K* r" ]* [  uA thread is a light-weight smallest part of a process that can run concurrently with the other parts(other threads) of the same process.# ?3 b6 }- h7 }0 M+ a8 v
"Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files. This makes for efficient, but potentially problematic, communication."
- P% h+ ]' b1 o% u. K4 ~- J' O' ~/ P
- independent executor of instructions$ b: v8 n& m7 I
- shared memory -> shared heap.private stack, register status
3 ]: k) w3 x0 h( B7 n9 P- thread之间相互通信:shared memory to communicate -》 Concurrency A thread is blocked when performing IO operations.
% M5 B/ |3 S" R. N8 q6 F' j( ]; v: g. F0 Q4 ~! P2 m
How to create your own threads???/ C, G. }. _8 @* V

2 l) \4 T, \  x1 g     1.  extend Thread Class
- i; Q' j1 L) `. b
' C/ n4 `4 W0 T( M$ g- implement Runnable Interface
9 ]: J& @) w$ c* p# Rclass MyRunnable implements Runnable {}
- V7 l- f; b! m1 D& a$ hThread t = new Thread(new MyRunnable) thread里有constructor可以接受class
7 K- B! G* @5 L6 l& O
4 b* I) `  b# x8 S8 `How to interrupt thread and do context switch- w' t$ i5 O1 N# c# L& R. ^# S
! V$ i! Y5 S9 `3 O( m! S
An interrupt is an indication to a thread that is should stop what it is doing and doing something else. It depends on programmer to decide exactly how a thread responds to an interrupt, but it is very common for a  thread to terminate.
# g2 k. l) A6 B+ o: l; L7 E
  Q$ p9 z8 ~% Z5 o% b写一个interrupt method* T* h: I& O. ~* j8 h. O# e
(1) 可以直接return8 R8 u) ]5 O  m1 i' o  Q, d' O. n
(2)可以try catch exception
# ^4 Y" b+ ~1 R* a! g
$ V4 b) A- s) a& r9 }5 i0 mHow CPU deal with multi thread
4 Z; m  F) X) Severy CPU core at one time unit can execute one thread at one time.
, @5 O5 s1 a1 q3 Z2 x: I' Ascheduler + queue+ ~8 N! m# r" @7 D9 W) Y
Scheduler : controls which thread to run at the current time.
+ a# k# }2 v7 `. {  l% j. a4 R) MQueue: store the waited thread.
1 `) V5 J5 Z6 g5 g! B8 f1 ]5 @
& r# U: w3 r" W& @/ n0 x7 FThread Life Cycle
- R' t. k' `  R( iA thread can be in only one state at a given point in time.
" @# \, x  x5 c8 W% g6 t% Z/ n+ ]2 ?# `2 k0 c7 E; d4 @% {2 R- w
NEW – A thread that has not yet started is in this state., B" p6 a1 F3 [( I' p( r
YIELD-
3 Q, K1 O# M0 S( m( dSLEEP - sleep(time out)  or be notified8 }2 M7 S- Q1 {. o) m( {
RUNNABLE – A thread executing in the Java virtual machine is in this state.
7 y3 x! R* G& [: P3 n! J( yBLOCKED – 只是为了mark住这是 IO operations. 特殊情况。0 [: M3 l% O" G& p6 `, u0 ~
WAITING – notify  D. {8 u6 `* a
TIMED_WAITING – time out wait
! u  a" l( |; ?# lTERMINATED – A thread that has exited is in this state.
& t( |: w8 w: ]& `/ {1 E6 u( Z& L+ a, O6 q
Load Balancer5 P: r, G' [2 j% J* H
Consistent Hashing
3 a) \/ p- b, z1 \
; s9 p) A* G) f! QSynchronization:同步* i1 e9 J* }2 V: a5 [6 {" T
make shared memory consistent among threads. Use lock or others9 a/ i  n! }3 s
If not synchronizied data races/ race condition9 ~* {8 I0 _& O- z- F
三个要素:/ }9 O! y5 j" m. h$ g) ^" D
(1)operate on the same resources/memory
$ i8 _5 l. W) @(2) one will do write
, c5 D) k- G; t* b0 N6 e( n(3) they may operate concurrently.9 ^+ y" |$ Y2 X5 v/ q
9 s6 W9 g8 N' _0 R4 d1 Q
如何解决: Use Lock% l8 ^4 z! \3 Q0 u2 r$ o
(1) use locks to make mutual exclusion" N) [* P( `. x/ W7 A6 m
(2) Critical Section
2 }1 z& F5 A' G, z# Lthe share memory/block/shared resources/code block that should be protected by lock.
* e! P! t0 y& x, V# h  `- C
' F- C* x* n% bDeadLock发生条件2 N0 y) X3 x8 W* U, s
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.
1 J! Z+ N! I1 Z9 i" ?; a/ l(1) Mutual Exclusion
  v3 ^1 R2 f% F1 z$ x' R(2) hold and wait7 ^' X, {% n. T! C+ U- n
(3) no preemption (can only be released by holding thread)
# I1 X/ R. G; x( J(4) circular wait

0

主题

0

精华

10

积分

新米人

Rank: 1

积分
10
发表于 10-16-2016 03:49 AM 来自美国米群网手机版 | 显示全部楼层
幸福爱人回帖支持!~~~

0

主题

0

精华

2

积分

新米人

Rank: 1

积分
2
发表于 10-17-2016 05:03 PM 来自美国米群网手机版 | 显示全部楼层
Elizabeth回帖支持!~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表