Please enable JavaScript.
Coggle requires JavaScript to display documents.
Desktop Java Application (Thread (Monitor States (wait() and notify()
…
Desktop Java Application
Thread
- Single-threaded: one entry point (main() method) and one exit point, run from start to finish
- Multithreaded: has a very first entry point (main() method), followed by multiple entry and exit points for other methods
-
- Thread scheduler manage a thread (thread scheduler is part of JVM or operating system)
- Calling start() method make a thread eligible to run (not run immediately), it must still contend for CPU time and all other thread
When the thread gets to execute, it executes a run() method:
- Its own run() method or
- The run() method of some other object
- If you create a class extends Thread -> your class "is a" thread
- If you create a class implements Runnable -> your class "is associated with" a thread
- Can't restart a dead thread (can't call it start() or run() method)
- Can call other method (beside start() and run() of a dead thread
Intermediate States
Running: A running thread gets the full attention of the JVM’s processor, which executes the
thread’s run() method
Various non-running states: The basic non-running states are Suspended, Sleeping, and Blocked. There are also some non-running states that relate to monitors
-
-
Priority
- Every thread has a priority, which is an integer from 1 to 10; threads with higher priority should
get preference over threads with lower priority
- The thread scheduler considers the priority when it decides which ready thread should execute
- The default priority is 5
- Methods: setPriority(), getPriority()
- Constants: MAX_PRIORITY (10), MIN_PRIORITY (1), NORM_PRIORITY (5)
-
Control
Yielding:
- A thread can offer to move out of the virtual CPU by yielding
- A call to the yield() method causes the currently executing thread to move to the Ready state if the scheduler is willing to run any other thread in place of the yielding thread
Suspending:
- A mechanism that allows any arbitrary thread to make another thread unready for an indefinite period of time
- The suspended thread becomes ready when some other thread resumes it
- Caused by suspend(), waits for resume()
Sleeping:
- A sleeping thread passes time without doing anything and without using the CPU
- Methods:
public static void sleep(long milliseconds) throws InterruptedException or
public static void sleep(long milliseconds, int nanoseconds) throws InterruptedException
- When the thread has finished sleeping, it does not continue execution -> it enters the Ready
state and will execute only when the thread scheduler allows it to do so
- A sleeping thread that receives an interrupt() call moves immediately into the Ready state; when it gets to run, it will execute its InterruptedException handler
- Caused by sleep(), waits for timeout
Blocking:
- Many methods that perform input or output have to wait for some occurrence in the outside world before they can proceed; this behavior is known as blocking
- A thread can also become blocked if it fails to acquire the lock for a monitor or if it issues a
wait() call
- Caused by various I/O calls or by failing to get a monitor’s lock, waits for I/O or for the monitor’s lock
Monitor States
- The wait() method puts an executing thread into the Waiting state
- The notify() and notifyAll() methods move waiting threads out of the Waiting state
- These methods are implemented in the Object class, not in Thread and can be called only in synchronized code
-
-
-
-
Deadlock: If a thread blocks because it is waiting for a condition, and something else in the program makes it impossible for that condition to arise, then the thread is said to be deadlocked