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
Threadinstances - 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
Threadis setting up - Indicates that
Threadis not running
-
Thread States
Runnable
- Indicates that
Threadmay be running - Only state where running is possible
-
Thread States
Blocked, Waiting, Timed Waiting
- Indicates that
Threadis inactive until thread-scheduler tells them to run.
-
Thread States
Terminated
- Indicates that
Threadhas finished running
-
Runnable Interface
Threadexecution is dependent on aRunnableobject.- A thread cannot
runwithout aRunnableinstance to say how to run.
- A thread cannot
- To construct a new
Thread, one must provide the thread with aRunnableupon 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
Threadtypically terminates upon completing therunmethod of the respectiveRunnable.interruptrequests early termination, but is not guaranteedrunis responsible for handlinginterruptcalls correctly.interruptthrows 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/catchto 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.