Java - Threads
Home > Flashcards > Print Preview
The flashcards below were created by user
on FreezingBlue Flashcards
. What would you like to do?
multiple lines of a single program can be executed at the same time
Under Unix, FORKING means?
forking a process creates a child process with a different address space for both code and data
What are the 2 ways of creating a Thread?
Implementing and Runnable interface and extending the Thread class
Once a Thread is stopped, can it be restarted with start()?
No, stop() will terminate the execution of a thread.
What will happen to the Thread you can pause it?
The thread will sleep for a certain period of time and then begin executing when the time limit is reached
What does the Thread Scheduler monitor do?
monitors all running threads in all programs and decides which threads should be running and which are in line to be executed
What are the 2 characteristics of a thread that the scheduler identifies in its decision process?
- The most important: The priority of the thread
- The daemon flag.
What are daemon threads?
Daemon threads are non-user threads. They are typically used to carry out low-priority tasks that should not take priority over the main task of the program
What happens to the JVM when there are only daemon threads running?
JVM will exit!
The scheduler comes in what 2 flavors?
What are Preemptive schedulers?
They give a certain time-slice to all threads running on the system.
What do non-preemptive schedulers accomplish?
decide which thread should run and run it until the thread is complete
method is a way for a thread to force the scheduler to start executing another waiting thread.
What is Synchronization?
Synchronization is the act of serializing (or ordering one at a time) thread access to those code sequences that let multiple threads manipulate class and instance field variables, and other shared resources
Can Threads share local variables and parameters?
No, because local variables and parameters allocate on a thread's method-call stack. As a result, each thread receives its own copy of those variables
Why do we need synchronization?
Think of a withdrawal/deposit of financial transactions? Each thread manipulates a pair of shared variables.
3 problems with synchronization
- no synchronization
- time cost associated with lock acquisition
After a thread voluntarily or involuntarily (through an exception) exits a critical code section, it releases a lock so another thread can gain entry. Suppose two threads want to enter the same critical code section. To prevent both threads from entering that critical code section simultaneously, each thread must attempt to acquire the same lock. If each thread attempts to acquire a different lock and succeeds, both threads enter the critical code section; neither thread has to wait for the other thread to release its lock because the other thread acquires a different lock. The end result: no synchronization
How to avoid no-synchronization?
choose an object common to all relevant threads. That way, those threads compete to acquire the same object's lock, and only one thread at a time can enter the associated critical code section.
Thread A acquires a lock that thread B needs before thread B can enter B's critical code section. Similarly, thread B acquires a lock that thread A needs before thread A can enter A's critical code section. Because neither thread has the lock it needs, each thread must wait to acquire its lock. Furthermore, because neither thread can proceed, neither thread can release the other thread's lock, and program execution freezes.
How to avoid deadlock?
carefully analyze your source code for situations where threads might attempt to acquire each others' locks, such as when a synchronized method calls another synchronized method. You must do that because a JVM cannot detect or prevent deadlock
What is thread scheduling?
Manages the one or more threads to share the processor.
What are the 4 thread states?
- Initial State
- Runnable State
- Blocked State
- Terminating State
A program has created a thread's thread object, but the thread does not yet exist because the thread object's start() method has not yet been called.
This is a thread's default state. After the call to start() completes, a thread becomes runnable whether or not that thread is running, that is, using the processor. Although many threads might be runnable, only one currently runs. Thread schedulers determine which runnable thread to assign to the processor.
When a thread executes the sleep(), wait(), or join() methods, when a thread attempts to read data not yet available from a network, and when a thread waits to acquire a lock, that thread is in the blocked state: it is neither running nor in a position to run. (You can probably think of other times when a thread would wait for something to happen.) When a blocked thread unblocks, that thread moves to the runnable state.
Once execution leaves a thread's run() method, that thread is in the terminating state. In other words, the thread ceases to exist.
What are green threads?
What are native threads?
OS handles thread scheduling
How does native thread schedulers prevent starvation?
It uses time-slicing to prevent processor starvation of equal-priority threads.
The idea is to give each equal-priority thread the same amount of time, known as a quantum. A timer tracks each quantum's remaining time and alerts the thread scheduler when the quantum expires. The thread scheduler then schedules another equal-priority thread to run, unless a higher-priority thread unblocks.
How to prevent lower-priority threads from starving?
some thread schedulers, such as Windows schedulers, give temporary priority boosts to threads that have not run in a long time. When the thread runs, that priority boost decays. Thread schedulers still give higher-priority threads preference over lower-priority threads, but at least all threads receive a chance to run.
What is the significance of setPriority(int priority)?
proves useful in preventing processor starvation.
What would you like to do?
Home > Flashcards > Print Preview