1 / 19

Concurrent Programming: Concepts, Java Threads, and Fork/Join Model

This lecture explores the concepts of concurrent programming, Java threads, and the fork/join model. It covers thread lifetime, communication, synchronization, and the creation of threads using both inheritance and implementing interfaces. It also introduces the fork/join model in Java.

pixley
Download Presentation

Concurrent Programming: Concepts, Java Threads, and Fork/Join Model

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. The University of North Carolina at Chapel Hill Lecture 37: Beyond Sequential Programs COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos April 24 COMP 144 Programming Language Concepts Felix Hernandez-Campos

  2. Concurrent Programming • The previous lectures dealt with sequential programs • Programs with a single active execution context • It is also possible to develop programs with more than one execution context • They are concurrent programs • For example, • http://java.sun.com/docs/books/tutorial/essential/threads/ • We will refer to an execution context as a thread COMP 144 Programming Language Concepts Felix Hernandez-Campos

  3. Concurrent ProgrammingMotivation • Capture the logical structure of the problem • E.g., servers and graphical applications • Cope with independent physical devices • E.g., multiple processors in a real-time control system • Execute a single program in more than one processors • Parallel computing • E.g., scientific simulations such as weather prediction COMP 144 Programming Language Concepts Felix Hernandez-Campos

  4. Concurrent ProgrammingLanguage Support • Most languages support some sort of concurrent execution • We will discuss a number of approaches • The most important issues in concurrent programming are • Thread lifetime • Communication • Synchronization • Let’s illustrate these concepts briefly with Java COMP 144 Programming Language Concepts Felix Hernandez-Campos

  5. Concurrent ProgrammingJava Threads • We will follow the Java Tutorial • http://java.sun.com/docs/books/tutorial/essential/threads/ • Threads are the units of execution context in Java Concurrent Program Sequential Program COMP 144 Programming Language Concepts Felix Hernandez-Campos

  6. Thread Lifetime COMP 144 Programming Language Concepts Felix Hernandez-Campos

  7. Creating ThreadsExtending Class Thread public class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { sleep((long)(Math.random() * 1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + getName()); } } COMP 144 Programming Language Concepts Felix Hernandez-Campos

  8. Creating ThreadsImplementing Interface Runnable • See • http://java.sun.com/docs/books/tutorial/essential/threads/clock.html • Java API • Thread • http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Thread.html • Runnable • http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Runnable.html COMP 144 Programming Language Concepts Felix Hernandez-Campos

  9. Thread Lifetime • http://java.sun.com/docs/books/tutorial/essential/threads/lifecycle.html COMP 144 Programming Language Concepts Felix Hernandez-Campos

  10. Co-begin • Co-Begin (Algol-68, Occam, SR) • Sequential begin a := 3, b := 4 end • Concurrent par begin a := 3, b := 4 end COMP 144 Programming Language Concepts Felix Hernandez-Campos

  11. Co-begin • Co-Begin par begin p(a, b, c), begin d := q(e, f); r(d, g, h) end, s(i, j) end p(a, b, c) d:=q(e, f) r(d, g, h) s(i, j) COMP 144 Programming Language Concepts Felix Hernandez-Campos

  12. Parallel Loops • Each iteration is executed concurrently • SR co (i := 5 to 10 -> p(a, b, i) oc • Occam par i = 5 for 6 p(a, b, i) COMP 144 Programming Language Concepts Felix Hernandez-Campos

  13. Parallel Loops • Fortran • OpenMP !$OMP PARALLEL DO do i=1,n-1 A(i) = B(i) + C(i) enddo !$OMP END PARALLEL DO • HPF forall (i = 1:n-1) A(i) = B(i) + C(i) end forall COMP 144 Programming Language Concepts Felix Hernandez-Campos

  14. Data dependency Parallel Loops • Fortran • HPF forall (i = 1:n-1) A(i) = B(i) + C(i) A(i + 1) = A(i) + A(i + 1) end forall COMP 144 Programming Language Concepts Felix Hernandez-Campos

  15. Data dependency Parallel Loops • Fortran • HPF forall (i = 1:n-1) A(i) = B(i) + C(i) A(i + 1) = A(i) + A(i + 1) end forall • Data dependencies make sequences of statements inherently sequential • The two statement must execute sequentially • Updates to A(i+1) should not be seen by A(i) in the subsequent operation COMP 144 Programming Language Concepts Felix Hernandez-Campos

  16. Launch-At-Elaboration • Ada procedure P is task T is … end T; begin --P … end P; COMP 144 Programming Language Concepts Felix Hernandez-Campos

  17. Fork/Join • In previous mechanisms, the creation of threads is implicit, while it is explicit in the fork-join models • The fork join model is more general • E.g., Ada task type T is … begin … end T; pt : access T := new T; • Java threads follow the fork/join model Forks a new thread COMP 144 Programming Language Concepts Felix Hernandez-Campos

  18. Fork/Join • (a) Threads are always properly nested with co-begin statements • (b) The fork/join mechanism is more general • In Java, • Fork by creating a Thread and executing start() • Join by executing join() on the thread COMP 144 Programming Language Concepts Felix Hernandez-Campos

  19. Reading Assignment • Read Scott • Ch. 12 intro • Ch. 12.2 COMP 144 Programming Language Concepts Felix Hernandez-Campos

More Related