找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

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

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

[复制链接]

2

主题

1

精华

30

积分

新米人

Rank: 1

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

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

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

x
本帖最后由 Sophia 于 10-16-2016 12:35 PM 编辑
/ W9 K. X7 v9 W" ?* q9 Y, l% v
7 z. {2 J6 g% N7 _, yConcurrency and multiple thread- D$ z$ g# J* G" f0 H/ |
' }  R. C: t8 n# V: i9 I
Concurrency vs Paralle:. e: R7 Z/ P; X, d
1 O& |( m7 [' ^3 ~$ U/ K
Concurrency并发:multiple task theoretically run simultaneously, semantic(假装看起来是)- 单核CPU multiple task,其实只能处理一个task,但是看上去像是。。fake multi task
0 ?: G: W. t6 e; Y( {6 u实际存在context switch3 C& v) p/ O8 ]* m# a2 B" v- @
+ t; B6 t+ i. Q% A
Paralle并行: 真实的运行.... multiple tasks physically run simultaneously.
0 O: H, b; ^( a4 {; a4 l: D0 K( w$ A9 G  @/ z) B
Process vs Thread
! ^/ h  ]. h" L( n8 u4 BMain Difference: Process vs Thread: u, v! K" a& x% L! z
whether they have independent memory: [7 z" E( |7 U3 J# F# t2 n
% @) G0 }6 _; H* y
Process:
1 o5 C9 _1 {, B! j3 g. q“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”.
) ^3 {5 ?9 G$ x! G4 m: D9 @3 T+ _; \  ]& K  Y! k* C- D3 L
- Independent executor of instructions exclusive CPU.独立的计算单元1 Q/ E: i  g& f, N" {" B: [
- independent own memory有独立存储单元,可以access a complete memory space
& m! z; i; C3 v9 X9 z, J- IPC communicate :进程之间互相通信。
+ C8 b4 d1 C$ l+ y* E
; M5 h, X" @- K( M* F5 _Thread3 c" R8 q# M) \( s& O
A thread is a light-weight smallest part of a process that can run concurrently with the other parts(other threads) of the same process.: w* R6 R7 E$ j( p/ u
"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* _4 x! f& k: b$ B" j  h) v
3 ?9 _1 ?6 ^" C: K. O- independent executor of instructions
6 k4 ^. m7 r# _9 m- shared memory -> shared heap.private stack, register status
0 \) o8 [- Y& Y. o  O: k( Z- thread之间相互通信:shared memory to communicate -》 Concurrency A thread is blocked when performing IO operations.
$ W- ~) b3 I7 U! _/ o- h, c; B, I9 e4 D$ u% r  A% E/ c( s% \
How to create your own threads???
2 I2 n! i7 \/ c* ^) z
" l& K/ r. C3 J: a* l$ `) d! J     1.  extend Thread Class% Y9 P! G/ u; U2 Q
6 u) _  M1 U$ u# X
- implement Runnable Interface5 U: H8 d( I/ r% E1 T1 |
class MyRunnable implements Runnable {}
/ K. W. L( }" a  n: bThread t = new Thread(new MyRunnable) thread里有constructor可以接受class
6 I  j) o" g9 R% C
% r( N2 r- F5 J; U$ ~$ zHow to interrupt thread and do context switch, W& V3 Y; u4 F3 Q
/ S' ~) F  |2 t$ s; j
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.
  n; z2 X, X& \# _
# U& g0 d% b' i) x" p4 B8 ?# \) a6 a写一个interrupt method! M% H/ G$ p, Y/ |% y( u
(1) 可以直接return
& f3 W5 ^+ H4 H& W) v* S( o(2)可以try catch exception  @$ w6 r5 _5 G3 ~! g1 @( `- @

% U8 C' ^( }2 d5 PHow CPU deal with multi thread
4 y  i/ P4 {0 |; X# Fevery CPU core at one time unit can execute one thread at one time.
' `7 h* X+ b% @  d3 x) C, z; V+ O* Kscheduler + queue
3 a) h$ E  N: ?& ?Scheduler : controls which thread to run at the current time.
$ O* G" }# X- c% |Queue: store the waited thread.
+ C1 a/ d7 ?5 h% v7 [
! D" Y$ r; c7 D6 t6 k9 g" AThread Life Cycle. l8 p3 I4 l" P7 o1 B+ T
A thread can be in only one state at a given point in time.; Y, G" V9 D4 L- E2 m8 i. L
5 h& w6 d1 |+ z$ c) i  j2 D1 D" ^
NEW – A thread that has not yet started is in this state.
8 h5 M8 S' d6 B; [YIELD-! a, r5 R7 C) T- ~/ N/ S( Y  E
SLEEP - sleep(time out)  or be notified- H* c2 x  Z; X5 a8 a/ D9 ~
RUNNABLE – A thread executing in the Java virtual machine is in this state.
6 X8 [7 L2 w& d* J: W: KBLOCKED – 只是为了mark住这是 IO operations. 特殊情况。
8 S& }# f$ `# T/ yWAITING – notify
/ _% h, s  g! p' K0 V  uTIMED_WAITING – time out wait+ ?, ?$ D/ E6 i
TERMINATED – A thread that has exited is in this state.
1 Z% ]! U- a/ n" K: W8 A: j2 Y# x/ [9 T; o; ?, p1 B* R
Load Balancer
& I3 s  \* z) A( C. UConsistent Hashing- ]; d. Z7 U* u' z' b" h
; D" H) W! }7 g* Q- A1 g+ |5 N
Synchronization:同步
% K+ ~; `3 d" Z. \make shared memory consistent among threads. Use lock or others0 l( _: `; s8 j$ Q1 q& B8 c. N  i
If not synchronizied data races/ race condition+ j7 M% x1 _$ n' o
三个要素:7 c* B0 c% Z. m2 e, E2 i( b
(1)operate on the same resources/memory
1 A/ G; `1 _1 ?0 r" \1 {(2) one will do write% w9 p! m# D! ^# H0 {
(3) they may operate concurrently.
& F1 r( B% M& a5 {" C/ O7 V& K3 G0 ?0 R4 j
如何解决: Use Lock& G9 i" w* L. H/ p) b
(1) use locks to make mutual exclusion
3 i" `" o6 @# z4 h2 P) X& ^(2) Critical Section
- }! ^" h9 S1 F/ @: {the share memory/block/shared resources/code block that should be protected by lock.0 a' m/ Q2 W7 ?6 Z

1 Z3 T8 i9 {0 J( yDeadLock发生条件
( x% o$ M  [) F' G6 x& cDeadlock describes a situation where two or more threads are blocked forever, waiting for each other.
* B% v) Y# s' `3 x(1) Mutual Exclusion; K1 R9 T/ S0 s* i
(2) hold and wait
) P6 }7 z# d. K# p0 M(3) no preemption (can only be released by holding thread)
3 s2 ~) Z' ?( G$ h3 ?/ @, k' X(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回帖支持!~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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