Please enable JavaScript.
Coggle requires JavaScript to display documents.
Thread (Definition (an execution flow with its own program counter and…
Thread
Definition
-
-
currentThread() to get current thread
getId() and getName() to get detail of thread
set/getPriority() to set priority 1-10, default 5
-
only run once, not restartable
setDaemon() - make thread as daemon, will terminate when all non-daemon threads finished.
-
-
States
-
RUNNABLE
called start(), run() is executing and not blocked
doesn't mean being executed by CPU, may be waiting for CPU by OS
-
-
-
-
-
Memory Visibility
-
because change may only happen in cache, not main memory
-
-
two or more threads waiting to obtain a lock that some of the other threads are holding, like mutually waiting for lock release while holding the lock the other thread need
-
-
occurs when multiple threads want the same locks, at the same time, but obtain then in different order
wait/notify
wait()
give up CPU and release locks, block and put current thread on a condition queue
always use wait() in while loop with a condition check, cos thread can be unexpectedly awaken where condition not met yet
-
-
-
Context Switching
costly. when OS switch threads to execute by CPU, need to store old program counter/register and load new pc/reg
-
-
Producer/Comsumer
BlockingQueue
put() - wait() while queue is full, notifyAll() after put
take() - wait() while queue empty, notifyAll after taken