Concurrency in Java

Refer to Concurrency for a general discussion!

Interrupts
An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.

Joins
Invoking thread X's public non-static join function from inside thread Y means that thread Y will wait for thread X to complete before continuing.

Thread's Live Status
A Thread's public non-static function isAlive returns true if the thread has started and has not yet died (regardless of its interrupt\sleep\block status?).

Synchronization
It is the method used to solve the two problems possible in multi-threaded applications. It does so by using a monitor object that is acquired and released by threads.

Synchronized Functions
It is not possible for two synchronized functions in the same object to interleave. If a thread is executing a synchronized function in an object, all other threads trying to invoke other synchronized functions int he same object will block. Synchronized methods use the containing object as their monitor.  thread interference: when two threads access the same variables simultaneously, the final written result is undefined (read-increment-write) example memory consistency: solved by establishing a happens before relationship 

Synchronized Statements
They are blocks of code that use a user-defined object as the lock. This allows for fine grained synchronization (e.g. when concurrent updates to two different variables are allowed to interleave).

Atomic Access
Insuring that a sequence of operations are executing non-preemptably (without interruption\interleaving). All read and write to primitive data types (except long and double) are atomic. All read and write to volatile data types are atomic.

Liveness
A concurrent application's ability to execute in a timely manner is known as its liveness. The opposite of liveness is deadlocks, starvations and livelocks.

Guarded Blocks
Tt is a technique used to coordinate work between threads (e.g thread 1 should not continue before thread 2 has done something). It uses synchronization features. Guarded blocks are established by using an object's wait method. This method causes the caller to block until another thread calls the same object's notify or notifyAll method which causes all threads blocked on wait to wake up. The thread that executes an object's must own that objects monitor, which means it must be called from a synchronized context (method, statement) of that object. Immediately before the thread sleeps, it releases the monitor. Also notify and notifyAll of an object must be called from inside a synchronized context, but they do not cause the caller to release the lock.

Sending, checking, and Receiving Interrupts
Sending and checking interrupts from outside a thread

The programmer can send an interrupt by invoking a thread object's public function interupt which sets an interrupt flag. The non-static public function isInterrupted is used to non-destructively check the status of that flag.

Receiving and checking interrupts from inside a thread

 Catching InterruptedException around sleep calls inside the thread Periodically checks for Thread.interrupted inside the thread and either terminate or throw a new InterruptedException. Thread.interrupted is destructive: clears the interrupt flag. 

Creating Guarded Blocks
If more than one event are coordinated using the same object, or if more than one thread will wait on the same object, you should use a while loop to check for the required event. Like the following: If only one thread will wait on a given object and only one event can possible notify on that object, you can ignore the while loop.

Synchronized Constructors
Is a syntax error because it doesn't make sense.

leaking Constructor
Exposing "this" inside a constructor may allow other threads to call methods of the object before it is done constructing.

Synchronizing final fields
They are safe to be read outside synchronized methods but may present liveness problems.

Synchronized Static Methods
They use a special class monitor that is distinct from any other object lock.

Reentrant synchronization
If a synchronized code calls another piece of synchronized code that use the same lock, no blocking happens because threads are allowed to acquire locks they already have.

Volatile and Memory Consistency
Using volatile variables only reduces the risk of memory consistency in a simple solutions but does not eliminate it entirely.