Download presentation
Presentation is loading. Please wait.
Published byColleen Ophelia Black Modified over 8 years ago
1
Multithreading
2
Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed different users to run programs at the same time. Two Types of Multitasking ◦Process Based ◦A process is, in essence, a program that is executing. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently ◦Thread Based ◦In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code. This means that a single program can perform two or more tasks simultaneously.
3
Thread Vs Processes Both threads and processes are methods of parallelizing an application. Processes are independent execution units Processes contain their own state information, use their own address spaces A single process may contain more than one threads All threads within a process share the same state and same memory space They communicate with each other directly, because they share same variables.
4
Introduction to Thread A threads is a light weighted process which runs concurrently with other threads. All threads of program defines a separate path of execution. A single process may contain more than one threads.
5
Multithreading Concept Java is a multithreaded programming language which means we can develop multithreaded program using Java. A multithreaded program contains two or more parts that can run concurrently and each part can handle different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs. By definition multitasking is when multiple processes share common processing resources such as a CPU. Multithreading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application. Multithreading enables you to write in a way where multiple activities can proceed concurrently in the same program.
6
Life Cycle of Multithreading A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. Following diagram shows complete life cycle of a thread. Start() New Thread Runnable Not Runnable New Thread Dead stop() stop(), or Run() exists
7
Multithreading life cycle New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread. Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task. Not Runnable : A thread enters “Not Runnable” state when one of these four events occurs: ◦Someone invokes its sleep() method ◦Someone invokes its suspend() method ◦The thread uses its wait() method to wait on a condition variable ◦The thread is blocking on I/O. Dead: A thread can die in two ways ◦Either from natural, A thread dies naturally when its run() method exists normally i.e. it will die naturally after the loop and the run() method completes. ◦Or By killed (stoped), we can also kill a thread at any time by calling its stop() method.
8
How to Create thread There are two ways to create a thread: ◦By extending Thread class ◦By implementing Runnable interface. The Thread Class ◦Thread class provide constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface. ◦Thread() ◦Thread(String name) ◦Thread(Runnable r) ◦Thread(Runnable r,String name) The thread class has the following methods that are used to control a thread ◦public void start(), which prepares a thread to be run ◦public void run(), actually performs the work of the thread. ◦public final void stop(), dies thread when stop() method is invoked.
9
Simple thread extending Thread public class BytePrinter extends Thread{ public void run(){ for(int b=-128; b<128; b++){ System.out.println(b); } public class ThreadTest { public static void main(String[] args) { System.out.println(“Constructing the thread…”); BytePrinter bp=new BytePrinter(); System.out.prinln(“Starting the thread …”); bp.start(); System.out.println(“Thre thread has been started.”); }
10
Simple thread implementing runnable public class BytePrinter implements Runnable{ public void run(){ for(int b=-128; b<128; b++){ System.out.println(b); } public class ThreadTest { public static void main(String[] args) { System.out.println(“Constructing the thread…”); BytePrinter bp=new BytePrinter(); Thread t1=new Thread(bp); System.out.prinln(“Starting the thread …”); t1.start(); System.out.println(“Thre thread has been started.”); }
11
Thread Priority Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled. Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5). Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and very much platform dependent. To set a thread’s priority, use the setPriority( ) method, which is a member of Thread. This is its general form: final void setPriority(int level)
12
Priority Example class TestMultiPriority extends Thread{ public void run(){ System.out.println("running thread name is:"+Thread.currentThread().getName()); System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); } public static void main(String args[]){ TestMultiPriority m1=new TestMultiPriority(); TestMultiPriority m2=new TestMultiPriority(); m1.setPriority(Thread.MIN_PRIORITY); m2.setPriority(Thread.MAX_PRIORITY); m1.start(); m2.start(); } Output: running thread name is:Thread-0 running thread priority is:10 running thread name is:Thread-1 running thread priority is:1
13
Thread Synchronization When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization If you declare any method as synchronized, it is known as synchronized method. Synchronized method is used to lock an object for any shared resource. When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.
14
Synchronization Example import java.io.IOException; class Table extends Thread{ int n; public Table(int n) { this.n=n; } synchronized void printTable(){//synchronized method for(int i=1;i<=5;i++){ System.out.println(n*i); try{ Thread.sleep(400); }catch(Exception e){System.out.println(e);} } } public void run(){ printTable(); }
15
//Synchronization Contd…… public class TestSynchronization { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Table obj=new Table(5); Thread t1=new Thread(obj); Thread t2=new Thread(obj); t1.start(); t2.start(); }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.