sharif university of technology spring 2015. agenda need for multi-thread programming threads in...

51
Advanced Programming in Java Sharif University of Technology Spring 2015

Upload: matilda-doyle

Post on 13-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1

Sharif University of Technology Spring 2015 Slide 2 Agenda Need for multi-thread programming Threads in java Samples Synchronization synchronized wait & notify join Sharif University of Technology2Fall 2014 Slide 3 Sequential Programming Up to this point, we learned sequential programming. Everything in a program happens one step at a time. What is wrong with this approach? Fall 2014Sharif University of Technology3 Slide 4 Multitasking and Multithreading Multitasking refers to a computer's ability to perform multiple jobs concurrently more than one program are running concurrently, e.g., UNIX A thread is a single sequence of execution within a program Multithreading refers to multiple threads of control within a single program each program can run multiple threads of control within it, e.g., Web Browser Fall 2014Sharif University of Technology4 Slide 5 Concurrency vs. Parallelism CPUCPU1CPU2 Fall 2014Sharif University of Technology5 Slide 6 Threads and Processes CPU Process 1Process 3Process 2Process 4 main run GC Fall 2014Sharif University of Technology6 Slide 7 What are Threads Good For? To maintain responsiveness of an application during a long running task. To enable cancellation of separable tasks. Some problems are intrinsically parallel. To monitor status of some resource (DB). Fall 2014Sharif University of Technology7 Slide 8 Parallel Processing Multi-Processor Systems Multi-core CPUs Dual core Core2duo Corei7, corei5 Even with no multi-core processors, Multithreading is useful How? I/O bounded tasks Responsive UI Simulated multi-threading Fall 2014Sharif University of Technology8 Slide 9 OS Support Multi-task OS Windows & Unix Multi-thread OS Single task OS DOS Fall 2014Sharif University of Technology9 Slide 10 Language Support Some languages have no built-in mechanism for muli- threading C, C++, QT as a solution OS-dependent libraries pthread in linux Windows API Java has multi-threading in its core language Pros and cons ISA experience Fall 2014Sharif University of Technology10 Slide 11 Application Thread When we execute an application: The JVM creates a Thread object whose task is defined by the main() method It starts the thread The thread executes the statements of the program one by one until the method returns and the thread dies Fall 2014Sharif University of Technology11 Slide 12 Multiple Threads in an Application Each thread has its private run-time stack If two threads execute the same method, each will have its own copy of the local variables the methods uses However, all threads see the same dynamic memory (heap) Two different threads can act on the same object and same static fields concurrently Fall 2014Sharif University of Technology12 Slide 13 Creating Threads There are two ways to create our own Thread object 1. Subclassing the Thread class and instantiating a new object of that class 2. Implementing the Runnable interface In both cases the run() method should be implemented Fall 2014Sharif University of Technology13 Slide 14 Extending Thread public class ThreadExample extends Thread { public void run() { for (int i = 1; i