Threads Flashcards
How do you create a thread?
Subclass the Thread class and override its (empty) run() method
Create a class that implements the Runnable interface with a run() method that does the required work and then give an instance to the constructor of the Thread class.
class MyRunnable implements Runnable { public void run() { ... Do whatever I want it to } } -- code in some method somewhere -- MyRunnable r = new MyRunnable(); Thread t = new Thread(r); t.start();
How do you start a thread?
When the Thread.start() method is called (by an existing thread) a new thread of execution is started.
How do you put a thread to sleep?
A thread can pause itself by calling the Thread.sleep(int time) method
like updating a progress bar every half second
How do you make one thread
wait for another thread to complete ?
void join() – a thread can call this method on the Thread object that refers to some other thread of execution to stop and wait until that other thread terminates
How do you interrupt a thread?
Thread objects have an internal flag that indicates whether or not they have received an interruption request by a call to the instance method interrupt().
What happens when a thread is
interrupted?
Signal is sent by calling interrupted method
If a sleeping thread is interrupted it will wake up and another thread interrupts it.
It throws an Interrupt Execution
Interruption is often used as a mechanism for telling a thread to cancel what it is doing and terminate.
-When threads are interleaved, what is a race condition and what is a lost
update?
A race condition occurs when two threads access a shared variable at the same time.
The lost update problem occurs when 2 concurrent transactions try to read and update the same data
How do you avoid race condition and lost update problems?
Wrap code inside synchronized block
synchronized(lockingobject) {
…operations to make atomic…
}
Atomic together because no thread can go inside the Synchronized block if one thread is already there
What is the Memory Consistency problem?
The changes made by one thread might not be visible to the other threads and they still all have inconsistent views of the same shared data.
How is memory consistency problem avoided by Synchronization?
synchronization forces the flushing and refreshing of all relevant cache memory (or at least guarantees the same effect if it can be achieved more efficiently).
Next thread will know the value of the variable
What does the Volatile Modifier do?
1) Guarantees that a read or write of the variable will be atomic on all architectures. This affects ONLY double and long variables,
2) Ensures that all reads and writes of the variable are immediately copied to/from main memory. That is, it guarantees memory consistency for that variable. All threads accessing the variable are guaranteed to see all changes to the variable, even without synchronization.
What does the Volatile Modifier not do?
Doesn’t have an effect on race conditions
What is deadlock and how badly used synchronization
can cause it?
NOT FINISHED
A common problem when threads depend on each other to complete some work.
e.g. Thread A waits for Thread B, Thread B waits for Thread A; Both keep waiting forever.
How can badly used synchronization
can cause deadlock?
Bad use o