Java supports concurrecy with the support of
java.util.concurrency package.
Process and Threads
In concurrent programming, there are two basic units of execution: processes and threads. In Java programming language, concurrent programming is mostly concerned with threads.
Threads can exists in more than one number. Every applicatoin has at least one thread -- or several, if you count "system" threads that do things like memory management and signal handling
Each thread is associated with an instance of the class
Thread.
Defining and Starting a Thread
An applicaiton that crates an instance of
Thread must provide the code that will run in that thread. There are two ways to do this:
- Provide a Runnable object: The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread, as shown below:
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
- Subclass Thread. The Thread class itself implements Runnable, through its run method does nothing. An application can subclass Thread, providing its own implementation of run as follows:
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
Pausing Execution with SleepThread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. There are two overloaded methods of sleep.
It is important to declare that the
main method throws an
InterruptedException. This is an exception that sleep throws when another thread interrupts the current thread while sleep is active.
InterruptsInterrupts are supported by threads with the help of
interrupt method.
JoinsThe
join method allows one thread to wait for the completion of another.
Synchronization
Threads communicate by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible:
thread interference and
memory consistency errors.
Thread InterferenceThis is about how errors are introduced when multiple threads access shared data. This situation occurs when a single object is referenced by 2 or more threads.
Memory Consistency ErrorsMemory consistency errors occur when differenct threads have inconsistent views of what should be the same data. The key to avoiding memory consistency errors is understanding the
happens-before relationship. This relationship is simply a guarantee that memory writes by one specific statement are visible to another specific statement.
Two statements that we know, which create a
happens-before relationships are:
Synchornized Methods and StatementsSynchronized methods allow only execution of a single object at a time while all other objects are blocked. Constructors cannot be synchronized. Methods and statements are declared as
synchronized by having the keyword
synchronized added in the declaration.
Some other important concepts that are part of this Concurrency section are given belows:
tutorials