Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threading and Concurrency COM379T John Murray –

Similar presentations


Presentation on theme: "Threading and Concurrency COM379T John Murray –"— Presentation transcript:

1 Threading and Concurrency COM379T John Murray – john.murray@sunderland.ac.uk

2 The opposite of concurrency  The following is a diagrammatic view of a ‘sequential’ program  The flow of control is linear, i.e. from one instruction to the next.

3 Concurrency  Concurrency consists of using multiple threads running at the same time.

4 Forks  When a single thread spawns another thread it is referred to as a fork.

5 Join  When other threads return their control back to the parent / master thread, this is known as a Join

6 The Thread Class  In order to be able to implement threads in Java you need to make use of the Thread class.  Therefore your class must extend Thread public class TryThread extends Thread

7 The Thread Class  This class provides all the methods required to run your programs threads.  Methods include: start() – Create / Initiate the thread stop() – Stop execution of the thread sleep() – Pause the thread for x time join() – Dispose of the thread in correct manor + many more, see the Thread API

8 run() ing a Thread  Within your class you need to create a method called run(), this is where the thread execution will start  Contains any code you wish your thread to execute.  Can call other methods from your class here.

9 Example – Creating two Threads public class TryThread extends Thread { private String threadName; public TryThread(String theName) {// Constructor accepting a String this.threadName = theName;// Assign string to Class attribute threadName } public static void main(String [] args) { Thread first = new TryThread(“First”);// Define first thread Thread second = new TryThread(“Second”);// Define Second thread first.start();// Start first thread second.start();// Start second thread try { System.in.read();// Wait for enter first.stop();// Stop first thread second.stop();// Stop second thread } catch (IOException e) { System.out.println(e) } public void run() { while(true) { System.out.println(“Name of thread: “ + threadName); }

10 Synchronization  At some point in your program several threads may wish to access a single resource.  Synchronization ensures that only one thread at any given time can access the resource.  This is known as mutual exclusion

11 Synchronized Methods  To make a method mutually exclusive you use the keyword synchronized synchronized public void method1() { // Code for method }  Only one of the synchronized methods in a class object can be executed by ANY thread at a time

12 Lets take a closer look

13 Thread Priorities  Threads have built-in priority scheduling.  If you wish a thread to have higher priority than others you can call the setPriority() method.  Priority’s: Low – 1 Default – 5 Highest - 10

14 Thread Priorities  The priority of a thread is the same as that which created it.  setPriority() accepts a type of int  IllegalArgumentException is thrown if a priority MAX_PRIORITY is set.  To find the current priority use getPriority() int currentPriority = thread1.getPriority(); // Retrieve current priority thread1.setPriority(7); // Set new Priority

15 Fairness of execution  If all threads have the same priority then the current thread of execution may not relinquish is control of resources, i.e. the CPU  This leads to other threads experiencing what is known as ‘starvation’.  We therefore have to make the thread relinquish it’s control of the CPU

16 Thread.sleep()  If you need to get a thread to free up the CPU so that other waiting threads can use it the sleep() method is called.  sleep() halts the current running of the thread and makes it release it’s access to the CPU for a set time.  Thread.sleep(timeInMilliseconds)

17 Runnable Interface  So far to use threads we have used extends Thread.  This could cause problems if we also need to extend JFrame for GUI’s  Therefore we can implements Runnable  The runnable interface only declares one method run()

18 Runnable example public class RunnableTest implements Runnable { Thread myThread;// Reference to our Thread public RunnableTest() { myThread = new Thread(this); // Create the thread myThread.start(); // Start execution of the thread } public static void main(String [] args) { RunnableTest myTest = new RunnableTest(); } public void run() { // Execute some code }

19 Considerations  When using threads several considerations have to be considered due to the nature of threading.  Starvation – Other lower priority threads not gaining access to CPU  Deadlock – Threads holding on to resources waiting for others <- Serious problem

20 Starvation  Related to priorities  Must ensure all threads will at some point get CPU time.  Keep checks on threads if not executed in x time increase priority?

21 Deadlock  Deadlock is a big issue in threaded systems.  It occurs when two threads are each waiting for the other thread to release control of a resource so that they can use it to complete.

22 Deadlock

23  P1 has control of R1 but needs R2  P2 has control of R2 but needs R1  Neither P1 or P2 will release their resource.  Therefore one of the threads needs to yield() its control of the resource


Download ppt "Threading and Concurrency COM379T John Murray –"

Similar presentations


Ads by Google