Download presentation
Presentation is loading. Please wait.
Published byEstifanos Tilahun Mihret Modified over 5 years ago
1
Advanced Programming Estifanos T. (MSc in Computer Networking)
2
CHAPTER THREE Multithreading Concept Estifanos T. (MSc in Computer Networking)
3
Objectives 3 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) To explain Threads Vs Process To describe the Multiple Threads To discuss Thread Priorities To explore Thread Synchronization
4
Introduction 4 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking)
5
Introduction 5 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking)
6
Introduction 6 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) Thread: single sequential flow of control within a program Single-threaded program can handle one task at any time Multitasking allows single processor to run several concurrent threads Most modern operating systems support multitasking
7
Multithreading 7 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) video interaction networking Video Game Process
8
Advantages of Multithreading 8 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) Reactive Systems – constantly monitoring More responsive to user input – GUI application can interrupt a time- consuming task Server can handle multiple clients simultaneously Can take advantage of parallel processing Different processes do not share memory space A thread can execute concurrently with other threads within a single process All threads managed by the JVM share memory space and can communicate with each other
9
Threads Concept 9 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) Multiple Threads On Multiple CPUs Multiple Threads Sharing A Single CPUs
10
Threads in Java 10 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) Creating threads in Java: Extend java.lang.Thread class OR Implement java.lang.Runnable interface
11
Threads in Java 11 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) Creating threads in Java via Extend java.lang.Thread class run() method must be overridden (similar to main method of sequential program) run() is called when execution of the thread begins A thread terminates when run() returns start() method invokes run() Calling run() does not create a new thread
12
Threads in Java 12 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) Creating threads in Java via Implement java.lang.Runnable interface If already inheriting another class (i.e., JApplet) Single method: public void run() Thread class implements Runnable
13
Threads States 13 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking)
14
Threads Termination 14 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) A thread becomes Not Runnable when one of these events occurs: Its sleep method is invoked The thread calls the wait method to wait for a specific condition to be satisfied The thread is blocking on I/O
15
Creating Threads Demo 1 15 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) class Output extends Thread { private String toSay; public Output (String st) { toSay = st; } public void run() { try { for ( int i=1; i<=2; i++) { // you can change the iteration as you want System.out.println(toSay); sleep(1000); } } catch (InterruptedException e) { System.out.println(e); } }
16
Creating Threads Demo 1 16 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) public static void main(String [ ] args) { Output thr1 = new Output (“Hello”); Output thr2 = new Output (“There”); thr1.start(); thr2.start(); } Main thread is just another thread (happens to start first) Main thread can end before the others do Any thread can spawn more threads
17
Creating Threads Demo 2 17 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) class Output implements Runnable { private String toSay; public Output(String st) { toSay = st; } public void run() { try { for (int i=1; i<=2; i++) { // you can change the iteration as you want System.out.println(toSay); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(e); }}}
18
Creating Threads Demo 2 18 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) public static void main(String [ ] args) { Output out1 = new Output(“Hello”); Output out2 = new Output(“There”); Thread thr1 = new Thread(out1); Thread thr2 = new Thread(out2); thr1.start(); thr2.start(); }} Main is a bit more complex Everything else identical for the most part
19
Controlling Java Threads 19 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) start(): begins a thread running wait() and notify(): for synchronization stop(): kills a specific thread (deprecated) suspend() and resume(): deprecated join(): wait for specific thread to finish setPriority(): 0 to 10 (MIN_PRIORITY to MAX_PRIORITY); 5 is default (NORM_PRIORITY)
20
yield() Method 20 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) yield() method used to temporarily release time for other threads public void run() { for (int i = 1; i <= lastNum; i++) { System.out.print(" " + i); Thread.yield(); } }
21
sleep() Method 21 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) The sleep (long mills) method puts the thread to sleep for the specified time in milliseconds public void run() { for (int i = 1; i <= 60; i++) { System.out.print(" " + i); try { if (i >= 50) Thread.sleep(1); } catch (InterruptedException ex) { System.out.println(ex); }}}
22
sleep() Vs yield() Methods 22 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) There is a big difference Calling sleep put the current running thread into the blocked state Calling yield does not put the calling thread, t1 into the blocked state It merely let the scheduler kick in and pick another thread to run It might happen that the t1 is select to run again. This happens when t1 has a higher priority than all other runnable threads
23
join() Method 23 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) join() method used to force one thread to wait for another thread to finish public void run(){ Thread thread4 = new Thread(new PrintChar('c', 60)); thread4.start(); try { for (int i = 1; i < lastNum; i++){ System.out.print(" " + i); if (i == 50) thread4.join(); }} catch (InterruptedException ex) { System.out.println(ex); }}
24
isAlive(), interrupt(), and isInterrupt() 24 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) The isAlive() method is used to find out the state of a thread. It returns true if a thread is in the Ready, Blocked, or Running state; it returns false if a thread is new and has not started or if it is finished The interrupt() method interrupts a thread in the following way: If a thread is currently in the Ready or Running state, its interrupted flag is set; if a thread is currently blocked, it is awakened and enters the Ready state, and an java.io.InterruptedException is thrown The isInterrupt() method tests whether the thread is interrupted
25
Thread Priority 25 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) Each thread is assigned a default priority of Thread.NORM_PRIORITY. You can reset the priority using setPriority (int priority) Some constants for priorities include Thread.MIN_PRIORITY, Thread.MAX_PRIORITY, Thread.NORM_PRIORITY
26
Thread Synchronization 26 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) Synchronization is prevent data corruption Synchronization allows only one thread to perform an operation on a object at a time If multiple threads require an access to an object, synchronization helps in maintaining consistency
27
The synchronized Keyword 27 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) To avoid resource conflicts, it is necessary to prevent more than one thread from simultaneously entering certain part of the program, known as critical region You can use the synchronized keyword to synchronize the method so that only one thread can access the method at a time A synchronized method acquires a lock before it executes. In the case of an instance method, the lock is on the object for which the method was invoked public synchronized void deposit (double amount)
28
Thread Prioritizing Demo 28 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) class Output extends Thread { private String toSay; public Output (String st) { toSay = st; } public void run() { try { for ( int i=1; i<=2; i++) { // you can change the iteration as you want System.out.println(toSay); sleep(1000); } } catch (InterruptedException e) { System.out.println(e); } }
29
Thread Prioritizing Demo 29 Lecture 3: Multithreading Concept 9/9/2019 Estifanos T. (MSc in Computer Networking) public static void main(String [ ] args) { Output thr1 = new Output (“Hello”); Output thr2 = new Output (“There”); thr1.setPriority (MIN_PRIORITY); thr2.setPriority (MAX_PRIORITY); thr1.start(); thr2.start(); }
30
End Of Chapter Three
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.