Logout

Home Topic 4 Last Next

--- Thinking concurrently ---

4.1.14

Identify the parts of a solution that could be implemented concurrently.

 

Teaching Note:

Could include computer systems or real-life situations.

LINK Thinking ahead, thinking procedurally. Connecting computational thinking and program design, introduction to programming.

 

Sample Question:

From Sample Paper 1 2014:

sample question

 

JSR Notes:

The Basics of Concurrency

Concurrency describes a situation in which more than one thing can happen at the same time (i.e. happen concurrently).

So, for example, you can pat your head and rub your stomach at the same time (maybe...). A person can drive a golf cart and chew gum at the same time. And so on. When two or more things can happen a the same time, this is concurrency.

In terms of computers, concurrency is the ability of the software and hardware of a computer to process two or more things at the same time. The first thing that is required is that the hardware has been designed with the capacity to do concurrent processing. But even if that is the case, still, logically, two things can only happen at the same time if neither depends on the other being finished first.


Non-Computer Examples

GANTT Chart Project

Here, a "GANTT chart" can allow us to clearly see from its structure the sub-tasks which can happen at the same time; they overlap horizontally on the chart.:

GANTT Chart

 

The Building a House Example


In the constructing a house example at the top of this page, the following three pairs of tasks could all be done concurrently, since neither depends on the other being finished:

 

 


But the following cannot be done concurrently; they must be done in the order listed:

 

 

Computer Examples

A CPU does things incredibly quickly. A CPU with a clock speed of 2 or 3 GHz is accomplishing 2 or 3 billion of the most fundamental steps in a single second. Normally it is only doing these things one after the other. But with hardware and software that support concurrency, more than one "thread" of processing can be done at once.


Hardware Requirements - Multi-core Processors and/or Hyper-Threading


In order for a computer to be able to do processes concurrently, it first needs to have been designed to be able to do so. There are a couple of approaches for this:

Multi-core Processor - a CPU can have more than one core, and so can do concurrent processing in a true, pure way.

Hyper-threading - CPUs which support multi-threading, or "hyper-threading" are able to make it seem like they are accomplishing two things at the same time by quickly switching back and forth betweens two threads of processing. For all intents and purposes, this is concurrent processing.

Here's a depiction of both of these kinds of concurrent processing.

It gives you a view of both multi-processing across a "quad-core" processor (i.e. 4 cores), and hyper-threading within each one of them, effectively giving you the capacity of doing 8 processes all at the same time (i.e. concurrently.)

 

Software Requirements - Threads

For concurrency to proceed, not only does hardware have to support it, but programs and operating systems also have to support it. "Threads" are used to separate and keep track of two or more tasks that may proceed at the same time.

So a thread is one process of multiple concurrent processes. Think of the various "threads" of a good novel, i.e. several story-lines progressing through the novel. Or even think of the various threads that run through a scarf from one end to the other.

In Java, there is a Thread class. You can create threads similar to creating other objects.

Thread t1 = new Thread (this, "myFirstThread");
Thread t2 = new Thead(this, "mySecondThread"); 

So as long as it's a multi-threading environment, i.e. one with hardware capable of hyper-threading and/or multi-core processing, Java can allow you to run multiple threads at the same time. Though note that you as the programmer have to decide to do this extra work. In the end it may be very much worth it, since your programs may have the ability to run much faster than otherwise.

 

Examples of processes that could theoretically be run concurrently

Example 1:

--> A video is being streamed and buffered while,

--> the first part of the video (having been buffered 30 seconds ago) is being played

 

Example 2:

--> half of the frames of a 3d animation are dished out to one processor,

--> and the other half of the frames are dished out to another processor (to be rendered, i.e. strung together in good quality images, as one video).

 

Example 3:

A certain Photoshop filter, at the same time,

--> changes the colors of every pixel a certain way,

--> and also moves those pixels around, the odd pixels up by one, and the even pixels down by one.

 

Some Actual Java Code - BEYOND WHAT IS REQUIRED

There are two ways in Java to work with Threads. One is by implementing the Runnable interface, and as with the example below, via extending the Thread class.

public class ThreadRunExample {
    public static void main(String[] args){
        Thread t1 = new MyThread("t1");
        Thread t2 = new MyThread("t2");
        System.out.println("Starting MyThreads");
        t1.start();
        t2.start();
        System.out.println("MyThreads has been started");
    }
}

Output:

Starting MyThreads
MyThreads has been started
MyThread - START t1
MyThread - START t2
MyThread - END t1
MyThread - END t2

So the idea here is that during the t1 thread crunching away, the t2 thread was also started and was running at the same time.

And the MyThread class, which the above code uses:

public class MyThread extends Thread {

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("MyThread - START "+Thread.currentThread().getName());
        try {
            Thread.sleep(1000);
            //Get database connection, delete unused data from DB
            doDBProcessing();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("MyThread - END "+Thread.currentThread().getName());
    }

    private void doDBProcessing() throws InterruptedException {
        Thread.sleep(5000);
    }

}