-
API
- java.io
- Java.network
-
java.lang
- ThreadGroup
-
Runnable
- implement run()
- thread pooling
- task schedule
- timing
-
Thread
- name
- target
- group
- stack size
-
Life Cycle
- start()
- run()
- This is only method to terminated thread expect system.exit()
- flag
- interrupt Thread
- call interrupt()
- isAlive()
- interrupt()
- non-blocking
- while(!isInterrupt()){}
- done if out of range
- done after go out of range if in the range
- blocking
- InterruptedExceptioin
- isInterrupted()
-
blocking method
- sleep()
- wait()
- join()
- java io
- currentThread()
-
Nonblocking-IO
- I/O Multiplexing
- Polling
- Signal
-
Threads
-
type
-
Main Thread
- main()
- Garbage Collection
- Java bytecode complier
- System Events
- User customize
-
Issue
- 平行化演算法
- Can't Interrupt immediately
-
Race condition
-
data sharing
-
synchronized
(mutex lock)
- handle instance token
- all method hang on ?
- control method call order because objec is lock by mehod scope
-
thread local memory
-
volatile
- atomic type
(except:long, double)
- setter/getter
(atomic operation)
- thread execute order
-
classification
-
atomic
- 不會有中途狀態的程式碼(bytecode level)
-
Synchronized
-
synchoronized modifier/method
-
Instance Lock
- synchronized method
-
Class Lock
- static synchronized method
-
Block Lock
- synchronized(this){} in code block
某程度上說會比Lock api簡易,
免try{}finally{},new lock instance
-
wait(), notify(), notifyAll() on purpose for usability
- 當Thread執行Wait會Pending且釋放鎖
其它Thread可呼叫Instance Method
當Thread被notify/notifyall會acitve且取回鎖
wait / notifyall / notifyall 要放至於synchoronized method 中避免 race condition
-
ReentrantLock
-
ReentrantLock()
(經由變數命名會較容易表示用途)
- getQueueLength();
- isLocketd();
-
Condition cv = lock.newCondition();
- cv.await(), cv.signal()
-
Nested Lock
- Lock包含Lock JVM識為同一個Lock
-
Atomic
(能讓多個operation 當atomic看待)
- AtomicInteger
- AtomicLong
- AtomicBoolean
- AtomicReference
- Questions