Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multithreading. DCS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.

Similar presentations


Presentation on theme: "Multithreading. DCS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part."— Presentation transcript:

1 Multithreading

2 DCS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part of) the program code, running independently of other threads Threads can share data, but local data can also belong to a specific thread

3 DCS – SWC 3

4 4

5 5 What is a thread Do threads really run in parallel? Depends on your system… At any time, there can be as many threads running as there are CPUs (actually CPU cores) in your system If more threads are running, the operating system will use time-slicing

6 DCS – SWC 6 What is a thread If only one CPU is available, and 10 threads are running, each thread is only truly running 10 % of the time OS manages threads using time slices –Threads are queued up –Each thread gets to run in a time-slice –It is then suspended, and put back in queue

7 DCS – SWC 7 What is a thread Why use threads at all…? It is sometimes unacceptable if a long operation blocks the application Think about an Internet browser; while one page is loading, you can still load other pages, scroll a page, etc. Using threads does not as such reduce the computation effort

8 DCS – SWC 8 Using threads Simple use of threads follows a few steps: First, implement a class which implements the Runnable interface: public interface Runnable { void run(); }

9 DCS – SWC 9 Using threads Next, put the code which needs to run in a a separate thread into the run method: public class MyTask implements Runnable { public void run() { // Code for task here... }

10 DCS – SWC 10 Using threads When the code is to be executed, create an object of your class, and a Thread object taking your object as input Finally, start the thread Runnable r = new MyTask(…); Thread t = new Thread(r); t.start();

11 DCS – SWC 11 Using threads Simple use of threads is thus not in itself particularly complicated When the started task ends, the thread itself is terminated What if we need to stop a thread, before the task has ended (or task is ”infinite”)…?

12 DCS – SWC 12 Using threads public void run() { while (true) { if (theQueue.isEmpty()) sleep(100); else processElement(theQueue.pop()); }

13 DCS – SWC 13 Using threads We cannot directly stop a thread, but we can ask it to stop itself… Call the method interrupt on the thread object The thread itself can check for interruption by calling Thread.interrupted

14 DCS – SWC 14 Using threads public void run() { while (!Thread.interrupted()) { if (theQueue.isEmpty()) sleep(100); else processElement(theQueue.pop()); }

15 DCS – SWC 15 Using threads But…what if the thread is sleeping? If interrupt is called on a sleeping thread, an InterruptedException is thrown in the thread We must catch this exception in run

16 DCS – SWC 16 Using threads public void run() { try { while (!Thread.interrupted()) { if (theQueue.isEmpty()) sleep(100); else processElement(theQueue.pop()); } catch (InterruptedExeption ex) {...} // Code for cleaning up, if needed }

17 DCS – SWC 17 Using threads Can a thread refuse to terminate…? Yes, but why should it… Calling interrupt on a thread is a signal to –Stop what you are doing –Clean up after yourself –Terminate completely We assume that threads are always willing to cooperate

18 Using threads Threads can also be used to perform a (computationally heavy) task in parallel Only makes sense if the task can be split into independent subtasks –Searching through large data –Image rendering –… DCS – SWC 18

19 Using threads Typical setup –Divide task into suitable subtasks –Devise way for subtask to report its result –Run each subtask in separate threads –Main thread will have to wait for all ”worker threads” to complete DCS – SWC 19

20 Using threads How can threads ”wait for each other”? If thread A wishes to wait for thread B to complete, thread A should make to call: threadB.join(); Thread A is often the main thread DCS – SWC 20

21 Using threads Main thread waiting for worker threads: for (Thread t : workerThreads) { try { t.join(); } catch (Exception ex) { // Handle exception if needed… } DCS – SWC 21

22 DCS – SWC 22 Using threads DEMO ThreadDemo project

23 DCS – SWC 23 Collision prevention On a railway through a mountain area, it was at some point only possible to build a tunnel with room for one track (see drawing below). This poses a problem when a train wants to pass through the tunnel – how can it be sure that no train is coming from the opposite direction at the same time? Devise a way for making sure that only one train uses the tunnel at any time. This problem is from the pre-radio era, so no solution involving remote communication is legal Make sure to take note of any assumptions you make when designing your solution Tunnel

24 DCS – SWC 24 Race conditions As long as threads only access their own local data (data created in their own thread), things are pretty easy If threads try to access (and change) shared data, things get complicated So-called race conditions can occur

25 DCS – SWC 25 Race conditions public void deposit(int amount) { int newBalance = balance + amount; balance = newBalance; } public void withdraw(int amount) { int newBalance = balance - amount; balance = newBalance; } balance = 1000 // Called on sepa- // rate threads deposit(100); withdraw(100); newBalance(d) = 1100 newBalance(w) = 900 balance = 900; balance = 1100;

26 DCS – SWC 26 Race conditions A very nasty property of race conditions is that they only occur sometimes… Allocation of time slices to threads is not deterministic – depends on total state of the system Code running correctly on one system may run incorrectly on another system

27 DCS – SWC 27 Race conditions Maybe this will help: public void deposit(int amount) { balance = balance + amount; } Unfortunately not – interruption is at lower level, so statement is not ”atomic”

28 DCS – SWC 28 Syncronised object access We must somehow prevent threads from simultaneous access to shared objects This is done by using a Lock object The Lock type is actually an interface – most commonly used implementation is the ReentrantLock class

29 DCS – SWC 29 Syncronised object access public class BankAccount { private Lock balanceLock; private int balance; public BankAccount() { balance = 0; balanceLock = new ReentrantLock(); }

30 DCS – SWC 30 Syncronised object access public void deposit(int amount) { balanceLock.lock(); balance = balance + amount; balanceLock.unlock(); } public void withdraw(int amount) { balanceLock.lock(); balance = balance - amount; balanceLock.unlock(); } This is almost – but not quite – good enough…

31 DCS – SWC 31 Syncronised object access If a thread calls lock on the Lock object, the thread owns the lock, until it calls unlock on the Lock object Any other thread calling lock on the Lock object will be suspended, until the lock becomes available What if the thread holding the lock never calls unlock …?

32 DCS – SWC 32 Syncronised object access public void aMethod() { aLock.lock(); // Code accessing a shared // resource aLock.unlock(); } What if this code throws an exception…?

33 DCS – SWC 33 Syncronised object access We must be sure that the thread holding the lock will call unlock Otherwise, we could have a deadlock We can ensure the call by using a finally clause

34 DCS – SWC 34 Syncronised object access public void aMethod() { aLock.lock(); try { // Code accessing a shared // ressource } finally { aLock.unlock(); } ALWAYS executed!

35 DCS – SWC 35 Preventing deadlocks Using locks ensures that shared data remains consistent, even in a multi- threaded scenario However, using locks may lead to other problems, typically a deadlock A deadlock occurs when two threads wait for each other to release locks

36 DCS – SWC 36 Preventing deadlocks I have the lock, but I’m waiting for B… I cannot proceed before I get the lock…

37 DCS – SWC 37 Preventing deadlocks public void withdraw(int amount) { balanceLock.lock(); try { while (balance < amount) // Wait for balance to increase } finally { balanceLock.unlock(); } But now it is impossible to deposit money…

38 DCS – SWC 38 Preventing deadlocks One solution is to use a so-called condition object Enables a thread to release a lock temporarily, thereby allowing other threads to obtain it A condition object is always related to a lock object

39 DCS – SWC 39 Preventing deadlocks I’m waiting for a condition to be fulfilled… OK, I’ll tell you when it might be fulfilled

40 DCS – SWC 40 Preventing deadlocks public class BankAccount { private Lock balanceLock; private Condition sufficientFundsCondition; private int balance; public BankAccount() { balance = 0; balanceLock = new ReentrantLock(); sufficientFundsCondition = balanceLock.newCondition(); }

41 DCS – SWC 41 Preventing deadlocks public void withdraw(int amount) { balanceLock.lock(); try { while (balance < amount) sufficientFundsConditions.await(); } finally { balanceLock.unlock(); } Not sleeping, but waiting…

42 DCS – SWC 42 Preventing deadlocks public void deposit(int amount) { balanceLock.lock(); try {... sufficientFundsConditions.signalAll(); } finally { balanceLock.unlock(); } Tell threads waiting on this condition, that the condition might have changed

43 DCS – SWC 43 Preventing deadlocks Notice that this is a coordinated effort between deposit and withdraw We must have control over all pieces of code trying to access a shared object Common errors: –Calling await without calling signalAll –Calling signalAll without locking the lock

44 DCS – SWC 44 Multithreading – final remarks Working with multi- threading is difficult! Hard to get an overview of all possible scenarios and consequences Errors may only show up occasionally

45 DCS – SWC 45 Multithreading – final remarks Debugging a multi- threaded program can be a true nightmare… The debugging process itself may change the behavior of the program Timing changes when debugging

46 DCS – SWC 46 Multithreading – final remarks Use multithreading only if you really need to! Typical example: GUI application with lengthy operations Makes GUI responsive when lengthy operations are executing

47 DCS – SWC 47 Multithreading – final remarks Making a program multi- threaded does not make it faster as such Amount of computation does not become smaller Multi-core CPUs make things a bit more compli- cated…


Download ppt "Multithreading. DCS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part."

Similar presentations


Ads by Google