comp 213
play

COMP 213 Advanced Object-oriented Programming Lecture 21 - PowerPoint PPT Presentation

COMP 213 Advanced Object-oriented Programming Lecture 21 Concurrency What is Concurrency? Concurrency is when two or more programs execute at the same time, either: sharing the same processor ( multi-threading ), or on different


  1. COMP 213 Advanced Object-oriented Programming Lecture 21 Concurrency

  2. What is Concurrency? Concurrency is when two or more programs execute ‘at the same time’, either: sharing the same processor ( multi-threading ), or on different processors; these may be in the same machine ( multi-processor architecture ), or in different machines ( distributed computation ).

  3. What Use is Concurrency? Reasons for using concurrency include: to perform a computation more quickly (usually using many processors), or to allow users to interact with programs while a computation is in progress. (E.g., allow more than one client to access a server at a time; or, allow users to interact with a GUI while some computation proceeds)

  4. Concurrency Comes at a Cost For both kinds of concurrency, there are overheads: communication between different processors takes time (and the more processors, the higher the costs) to share one processor among many programs, one program has to be chosen to run; when its time is up, it has to be stopped, its current state stored, another program chosen to run, and its previous state restored — all of which takes time.

  5. Multithreading We will concentrate on multithreading : where several programs run on one processor. This is familiar from window managers that typically allow computer users to have several windows on the screen at once, each running a different program (and usually with several other programs running in the background).

  6. Multithreading Multithreading is typically implemented by time-slicing : one program is chosen to run for a certain amount of time (a ‘quantum’) — the period is not determined by the program; once this amount of time has passed, the program is stopped and the values of its variables are stored in memory; another program is chosen to run, and the values of its variables are restored from memory; the program runs, and this procedure is repeated for as long as there are programs left to run.

  7. Multithreading Multithreading is typically implemented by time-slicing : one program is chosen to run for a certain amount of time (a ‘quantum’) — the period is not determined by the program; once this amount of time has passed, the program is stopped and the values of its variables are stored in memory; another program is chosen to run, and the values of its variables are restored from memory; the program runs, and this procedure is repeated for as long as there are programs left to run.

  8. Multithreading Multithreading is typically implemented by time-slicing : one program is chosen to run for a certain amount of time (a ‘quantum’) — the period is not determined by the program; once this amount of time has passed, the program is stopped and the values of its variables are stored in memory; another program is chosen to run, and the values of its variables are restored from memory; the program runs, and this procedure is repeated for as long as there are programs left to run.

  9. Multithreading Multithreading is typically implemented by time-slicing : one program is chosen to run for a certain amount of time (a ‘quantum’) — the period is not determined by the program; once this amount of time has passed, the program is stopped and the values of its variables are stored in memory; another program is chosen to run, and the values of its variables are restored from memory; the program runs, and this procedure is repeated for as long as there are programs left to run.

  10. Multithreading Multithreading is typically implemented by time-slicing : one program is chosen to run for a certain amount of time (a ‘quantum’) — the period is not determined by the program; once this amount of time has passed, the program is stopped and the values of its variables are stored in memory; another program is chosen to run, and the values of its variables are restored from memory; the program runs, and this procedure is repeated for as long as there are programs left to run.

  11. Multithreading Note that this ‘switching’ between programs is not determined by the programs that are running, but by the ‘time slicer’: this might be, e.g., the window manager, the operating system, or the implementation of the Java interpreter. Since we are interested in multithreaded Java programs, we’ll be concerned with time slicing as it is implemented by the Java interpreter.

  12. Multithreading Note that this ‘switching’ between programs is not determined by the programs that are running, but by the ‘time slicer’: this might be, e.g., the window manager, the operating system, or the implementation of the Java interpreter. Since we are interested in multithreaded Java programs, we’ll be concerned with time slicing as it is implemented by the Java interpreter.

  13. How to Write Concurrent Programs? At its simplest, concurrency can be achieved by writing two or more programs, and then starting them. Java supports this simplest form of concurrency. A Java programmer can simply write two or more programs, and then start them. Of course, since we’re talking about Java, these programs have to be contained in some class. . . . . . we’ll look at the classes (and interfaces) needed for this, and the support that Java provides for managing the complexities that arise from running several programs at the same time.

  14. How to Write Concurrent Programs? At its simplest, concurrency can be achieved by writing two or more programs, and then starting them. Java supports this simplest form of concurrency. A Java programmer can simply write two or more programs, and then start them. Of course, since we’re talking about Java, these programs have to be contained in some class. . . . . . we’ll look at the classes (and interfaces) needed for this, and the support that Java provides for managing the complexities that arise from running several programs at the same time.

  15. How to Write Concurrent Programs? At its simplest, concurrency can be achieved by writing two or more programs, and then starting them. Java supports this simplest form of concurrency. A Java programmer can simply write two or more programs, and then start them. Of course, since we’re talking about Java, these programs have to be contained in some class. . . . . . we’ll look at the classes (and interfaces) needed for this, and the support that Java provides for managing the complexities that arise from running several programs at the same time.

  16. Concurrency in Java Concurrency in Java is programmed using threads . The notion of a thread of computation is captured in Java by the Thread class. In Java, all programs run in an instance of the Thread class. (main() methods run in a special thread called the ‘main’ thread. Remember the error messages: terminal output Exception in thread "main" ... )

  17. Concurrency in Java Concurrency in Java is programmed using threads . The notion of a thread of computation is captured in Java by the Thread class. In Java, all programs run in an instance of the Thread class. (main() methods run in a special thread called the ‘main’ thread. Remember the error messages: terminal output Exception in thread "main" ... )

  18. How to Write Concurrent Programs ? — You have to write the programs. A program is just a sequence of instructions (assignments, conditionals, loops, method-calls, etc.) . . . which is exactly what you can place in a void method in java. So writing a program is the same as writing a void method.

  19. How to Write Concurrent Programs ? — You have to write the programs. A program is just a sequence of instructions (assignments, conditionals, loops, method-calls, etc.) . . . which is exactly what you can place in a void method in java. So writing a program is the same as writing a void method.

  20. How to Write Concurrent Programs ? — You have to write the programs. A program is just a sequence of instructions (assignments, conditionals, loops, method-calls, etc.) . . . which is exactly what you can place in a void method in java. So writing a program is the same as writing a void method.

  21. How to Write Concurrent Programs ? — You have to write the programs. A program is just a sequence of instructions (assignments, conditionals, loops, method-calls, etc.) . . . which is exactly what you can place in a void method in java. So writing a program is the same as writing a void method.

  22. How to Write Concurrent Programs ? — You have to write the programs. A program is just a sequence of instructions (assignments, conditionals, loops, method-calls, etc.) . . . which is exactly what you can place in a void method in java. So writing a program is the same as writing a void method.

  23. Interface java.lang.Runnable An interface is a good way of specifying, in Java, a void method. This is exactly what is done by: java.lang.Runnable public interface Runnable { public void run(); }

  24. Multithreading So you can write a program that you want to run concurrently with other programs by implementing interface Runnable. There is more to multithreading than just writing a program; the Java interpreter has to maintain a list of programs, and run them concurrently using time-slicing. The programmer gets the interpreter to do this with their program by: implementing interface Runnable with the program they want to run; creating a Thread (thread of computation) with that program; starting that Thread. You already know how to implement interfaces.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend