Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internet Software Development Controlling Threads Paul J Krause.

Similar presentations


Presentation on theme: "Internet Software Development Controlling Threads Paul J Krause."— Presentation transcript:

1 Internet Software Development Controlling Threads Paul J Krause

2 Contents  Quick recap  Thread Safety  Synchronization  Locks  Guarded suspension

3 Threads vs. Processes  A thread is a single flow of control within a program  It performs one of the tasks that a program needs to perform to achieve its goals  If a program needs to perform several tasks, these could be handled in different threads (and hence performed concurrently)  These threads will interact and cooperate through a shared memory space

4 The Risks of Threads  Different threads can interact via shared variables and objects  The execution of each thread may proceed independently of the others  In general, the relative ordering of execution of the different threads is non- deterministic  This can lead to safety and liveness problems

5 Thread Safety  Properties that we require to hold throughout the lifetime of a program  Essentially saying “Nothing bad should happen”  Bad Things typically can happen if a thread is interrupted whilst in the process of modifying an object

6 Bank Account Class public class Account { private long balance; // … public boolean withdraw(long amount) { if (amount <= balance) { balance = balance - amount; return true; } else { return false; } }}

7 This could happen! BalanceWithdraw1Withdraw2 1,000,000 amount <= balance 1,000,000 0 balance = …; -1,000,000 -1,000,000 return true; -1,000,000

8 Synchronisation  This was an example of a race condition  To be thread-safe, a class must ensure: consistency of the states of its instances, and consistency of the states of its instances, and consistency of the results of method invocations in the presence of multiple threads consistency of the results of method invocations in the presence of multiple threads  We have seen the Account class is not thread- safe  We can use Synchronisation to make it thread- safe

9 Synchronisation II  What was the problem with the Account class?  Answer -  More than one thread at a time was able to enter a “critical region” in the code  We need to: identify sections of code that should only be accessed by one thread at a time identify sections of code that should only be accessed by one thread at a time use the Java synchronisation mechanism to ensure a second thread cannot access a critical section use the Java synchronisation mechanism to ensure a second thread cannot access a critical section

10 Making operations atomic  An atomic operation is one that cannot be interrupted  Reading and assignment of variables of primitive types is atomic except long and double except long and double  For other operations, use synchronized to make them atomic

11 Synchronising methods class MyClass { synchronized void aMethod() { // do some stuff … }} Here, the whole method is in the critical region

12 Synchronising statements class MyClass { void aMethod() { // do some stuff … synchronised(this) { // atomic stuff … }}} “this” could be replaced by some other reference type

13 What to synchronize? public class Account { private long balance; // … public boolean withdraw(long amount) { if (amount <= balance) { balance = balance - amount; return true; } else { return false; } }}

14 Locks  Each object has an associated lock  A thread must obtain exclusive possession of the appropriate lock before it can enter a critical region i.e. the region marked by a synchronized statement i.e. the region marked by a synchronized statement  The lock is released when the thread leaves the critical region

15 Which lock?  For a synchronized instance method, the lock belongs to “this” instance  For a synchronized statement: synchronised(exp) { // atomic stuff … }  the lock associated with exp is used

16 Sharing locks public class A { synchronized void m1() { … } synchronized void m2() { … } void m3() { … } }

17 Is that sufficient? Thread ProducerConsumer SyncBoundedQueue

18 Cooperating threads  Can resolve additional problems with the use of guarded suspension  The guard is tested before a method is executed  If the guard is true, execution of the method will continue  If the guard is false, execution will be suspended until it becomes true

19 Thread control  Guarded suspension can be implemented through the use of the following thread control methods:  wait(): This is invoked if a thread is temporarily unable to continue (the guard is false)  notify(): One thread notifies another thread that it may continue  notifyAll(): All threads in the wait queue associated with the receiving object will be notified

20 Summary  Multiple threads may lead to safety issues  We have seen how synchronisation can be used to make programs “thread-safe”  But that still does not mean that multi- threaded programs will be perfectly behaved  We have introduced guarded suspension to solve additional problems


Download ppt "Internet Software Development Controlling Threads Paul J Krause."

Similar presentations


Ads by Google