Concurrency
Concurrency
- multiple sequences of operations are run in overlapping periods of time
- distinct from parallelism
- computational task that is broken down into several, often many, very similar sub-tasks, which can be processed independently and whose results are combined afterwards, upon completion.
- merge sort often has a parallel implementation.
-
Thread Object
- Allows tasks to run in parallel
- Can share data with other
Thread
instances - Less overhead to create and destroy than processes
- Threads are often scheduled according to some prioritization scheme
-
Thread scheduler
- responsible for prioritizing thread execution
- decides when each thread should be active
- decides when each thread should be idle
-
Thread States
-
Thread States
New
- Indicates that
Thread
is setting up - Indicates that
Thread
is not running
-
Thread States
Runnable
- Indicates that
Thread
may be running - Only state where running is possible
-
Thread States
Blocked, Waiting, Timed Waiting
- Indicates that
Thread
is inactive until thread-scheduler tells them to run.
-
Thread States
Terminated
- Indicates that
Thread
has finished running
-
Runnable Interface
Thread
execution is dependent on aRunnable
object.- A thread cannot
run
without aRunnable
instance to say how to run.
- A thread cannot
- To construct a new
Thread
, one must provide the thread with aRunnable
upon construction.
public void demo() {
Runnable runnable = ()->System.out.println("Hello world!");
Thread thread = new Thread(runnable);
thread.start(); // begins an independent thread of execution
}
-
Thread Interruption
Thread
typically terminates upon completing therun
method of the respectiveRunnable
.interrupt
requests early termination, but is not guaranteedrun
is responsible for handlinginterrupt
calls correctly.interrupt
throws anInterruptedException
, if a thread is sleeping.
-
Delaying Execution
- Tells program to cease execution on current thread for 5 seconds (5000 milliseconds).
public void demo() {
try {
Thread.sleep(5000);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
-
Encapsulating InterruptedException
- Defers
try
/catch
to a separate procedure- (A tool for future examples in this lecture)
public class ThreadUtils {
public static void sleep(Integer milliseconds) {
try {
Thread.sleep(milliseconds);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
-
Thread Interruption
Example 1
public void demo() {
Runnable runnable = this::execute;
Thread t = new Thread(runnable);
t.start();
ThreadUtils.sleep(5000);
System.out.println("I'M GONNA STOP THE THREAD NOW!");
t.interrupt();
}
public void execute() {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("I AM RUNNING!");
}
}
Output
I AM RUNNING!
I AM RUNNING!
I'M GONNA STOP THE THREAD NOW!
I AM RUNNING!
I AM RUNNING!
-
Asynchronous Threading
- From output of Example 1, notice the lack of control of the thread scheduler. The scheduler independently decides when to prioritize each thread execution.
- Asynchronous threading leads to unpredictably ordered execution.