1 / 20

Internet Software Development

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

gin
Télécharger la présentation

Internet Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internet Software Development Concurrent Programming Paul J Krause

  2. Contents • What are threads? • The risks of threads • Threads in Java • Two multithreaded programs • Final comments

  3. 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 …

  4. 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

  5. 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

  6. 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

  7. 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

  8. A:Thread O: P: B:Thread lock lock Example of Deadlock waiting to lock O waiting to lock P

  9. Livelock Picture from Lethbridge and Laganière, 2001

  10. <<interface>> Runnable Thread MyThread MyThread run( ) run( ) Threads in Java

  11. Creation of Threads • java.lang.Thread • run( ) - a “hook” method, that must be overridden • start( ) - invoke this to start a new thread of control

  12. 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?)

  13. 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); } }

  14. main method for Counter public static void main(String[] args) { new Counter(0, 1, 33).start(); new Counter(0, -1, 100).start(); }

  15. 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!

  16. Implementing Runnable public interface Runnable { public abstract void run(); } public class MyThread extends SomeClass implements Runnable { public void run() { // thread body here } // other methods … }

  17. 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();

  18. 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); } }

  19. 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(); }

  20. 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!

More Related