Internet Software Development
200 likes | 353 Views
Internet Software Development. Concurrent Programming Paul J Krause. Contents. What are threads? The risks of threads Threads in Java Two multithreaded programs Final comments. Threads. It is the use of multiple threads that makes concurrent programming difficult
Internet Software Development
E N D
Presentation Transcript
Internet Software Development Concurrent Programming Paul J Krause
Contents • What are threads? • The risks of threads • Threads in Java • Two multithreaded programs • Final comments
Threads • It is the use of multiple threads that makes concurrent programming difficult • Multiple processes executing concurrently is not a problem • Why do I say this? • What is the difference between a thread and a process? • Pause for classroom discussion …
Threads vs. Processes • A process is an independent flow of control • There is no shared memory space among different processes • Processes communicate between each other by specific communication channels (not shared variables) • e.g. Pipes in Unix, e.g. • ls | more
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
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
A:Thread Data: B:Thread A:Thread Data: B:Thread set get get set a) Normal b) Abnormal due to delay in thread A Race Conditions
A:Thread O: P: B:Thread lock lock Example of Deadlock waiting to lock O waiting to lock P
Livelock Picture from Lethbridge and Laganière, 2001
<<interface>> Runnable Thread MyThread MyThread run( ) run( ) Threads in Java
Creation of Threads • java.lang.Thread • run( ) - a “hook” method, that must be overridden • start( ) - invoke this to start a new thread of control
Creation of Threads II public class MyThread extends Thread { // fields … public void run() { // body of the thread … } // other methods … } new MyThread().start(); // Don’t invoke run() directly! (why not?)
Simple Counter public class Counter extends Thread { // From Jia, 2002 protected int count; protected int inc; protected int delay; public void run() { for (;;) { System.out.print(count + " "); count += inc; sleep(delay); } }
main method for Counter public static void main(String[] args) { new Counter(0, 1, 33).start(); new Counter(0, -1, 100).start(); }
How many threads? • The thread that executes the main method terminates after creating the following: • The thread that executes Counter with positive increment • The thread that executes Counter with a negative increment these last two execute concurrently for ever!
Implementing Runnable public interface Runnable { public abstract void run(); } public class MyThread extends SomeClass implements Runnable { public void run() { // thread body here } // other methods … }
Starting a thread • A little more complicated. • Notice MyThread has no start() method • So, we first create an instance of MyThread • Then, use this to create an instance of Thread, and invoke itsstart() method: new Thread(new MyThread()).start();
Counter2 public class Counter2 implements Runnable { // From Jia, 2002 protected int count; protected int inc; protected int delay; public void run() { for (;;) { System.out.print(count + " "); count += inc; Thread.sleep(delay); } }
Counter2 main method public static void main(String[] args) { new Thread(new Counter2(0, 2, 33)).start(); new Thread(new Counter(0, -2, 100)).start(); }
And Finally • We have introduced threads as flows of control within a program • Multiple threads may interact through shared memory space • This is both Good News and Bad News • We have seen some simple examples of controlling threads (start, sleep) • Next week we will see how to cope with the Bad News!