MPhil. Scholar Computer Science @ SALU, Khairpur Mir’s
AGENDA Process in OS Thread in Java Deference b/w Thread and Process Multithreading in Java Implementation of Multithreading in Java Advantages of Multithreading Conclusion
PROCESSOR PROCESS IN OPERATING SYSTEM  Process is a program in execution  Process is not as same as program code but a lot more than it  Process may be made up of multiple threads of execution that execute instructions concurrently MUSIC PLAYER MS-WORD OS SWITCHING PROCESS-1 PROCESS-1
Process Stages
A thread is a light-weight smallest part of a process that can run concurrently with the other parts(other threads) of the same process. Threads are independent because they all have separate path of execution that’s the reason if an exception occurs in one thread, it doesn’t affect the execution of other threads. All threads of a process share the common memory. . Thread in Java
Deference b/w Process and Thread Threads: 1.Threads are light weight processes. Threads are bundled inside the process. 2.Threads have a shared memory, data, resources, files etc. 3.Threads are created using clone() method. 4.Context switch between the threads are not much time consuming as Process. Example: Opening multiple tabs in the browser. Process: 1.Process is a heavy weight process. 2.Process is a separate program that has separate memory, data, resources etc. 3.Process are created using fork() method. 4.Context switch between the process is time consuming. Example: Say, opening any browser (mozilla, Chrome, IE). At this point new process will start to execute.
Multiprocessing in OS  Multiprocessing sometimes refers to executing multiple processes (programs) at the same time. Multitasking :- it is the process of executing several task simultaneously
The process of executing multiple threads concurrently is known as multithreading Multithreading in Java Let’s summarize the discussion in points: 1. The main purpose of multithreading is to provide concurrent execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread. 2. Threads are lightweight sub-processes, they share the common memory space. In Multithreaded environment, programs that are benefited from multithreading, utilize the maximum CPU time so that the idle time can be kept to minimum.
A program (Process) is divided into two or more subprograms (process), which can be implemented at the same time in parallel/ concurrently Example:- suppose we run a program which include two methods M1 and M2 these methods are threads but run concurrently A Program Method-1 Method-2 Multithreading :-
Multitasking Process based Thread based  Facebook  Music Player  Browser Heavyweight Own Memory Address Space MS- Word Home Tab Spelling Check Threads Lightweight Address Space is share
Steps of Thread
Multitasking vs. Multithreading vs. Multiprocessing vs. parallel processing Multitasking: Ability to execute more than one task at the same time is known as multitasking. Multithreading: We already discussed about it. It is a process of executing multiple threads simultaneously. Multithreading is also known as Thread-based Multitasking. Multiprocessing: It is same as multitasking, however in multiprocessing more than one CPUs are involved. On the other hand one CPU is involved in multitasking. Parallel Processing: It refers to the utilization of multiple CPUs in a single computer system.
Creating a thread in Java There are two ways to create a thread in Java: Thread in Java By extending Thread class extends Thread implementing Runnable interface Implements Runnable
Every time a Java program starts up, one thread begins running which is called as the main thread of the program because it is the one that is executed when your program begins When you implements Runnable , you can save a space for your class to extend any other class in future or now. Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java Thread Creation in Java Main Thread Deference B/w Extending and implementing runnable class
Before we begin with the programs(code) of creating threads, let’s have a look at these methods of Thread class. We have used few of these methods in the example below. •getName(): It is used for Obtaining a thread’s name •getPriority(): Obtain a thread’s priority •isAlive(): Determine if a thread is still running •join(): Wait for a thread to terminate •run(): Entry point for the thread •sleep(): suspend a thread for a period of time •start(): start a thread by calling its run() method METHODS OF THREAD CLASS
Method 1: Thread creation by extending Thread class class MultithreadingDemo implements Runnable { public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); Thread objt1 = new Thread(obj); objt1.start(); } } Output: My thread is in running state.
Method 2: Thread creation by implementing Runnable Interface class MultithreadingDemo implements Runnable { public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); Thread tobj =new Thread(obj); tobj.start(); } } A Simple Example Output: My thread is in running state.
Advantages of Multithreading  Enable programmers to do multiple things at one time  Programmers can divided a long program into threads and execute them parallel which will eventually increases speed of program execution  Improved performance and concurrency  Simultaneous access of multiple applications
Run Text Editor Run Command Prompt
Multithreading in-java

Multithreading in-java

  • 3.
    MPhil. Scholar Computer Science @SALU, Khairpur Mir’s
  • 6.
    AGENDA Process in OS Threadin Java Deference b/w Thread and Process Multithreading in Java Implementation of Multithreading in Java Advantages of Multithreading Conclusion
  • 7.
    PROCESSOR PROCESS IN OPERATINGSYSTEM  Process is a program in execution  Process is not as same as program code but a lot more than it  Process may be made up of multiple threads of execution that execute instructions concurrently MUSIC PLAYER MS-WORD OS SWITCHING PROCESS-1 PROCESS-1
  • 8.
  • 9.
    A thread isa light-weight smallest part of a process that can run concurrently with the other parts(other threads) of the same process. Threads are independent because they all have separate path of execution that’s the reason if an exception occurs in one thread, it doesn’t affect the execution of other threads. All threads of a process share the common memory. . Thread in Java
  • 10.
    Deference b/w Processand Thread Threads: 1.Threads are light weight processes. Threads are bundled inside the process. 2.Threads have a shared memory, data, resources, files etc. 3.Threads are created using clone() method. 4.Context switch between the threads are not much time consuming as Process. Example: Opening multiple tabs in the browser. Process: 1.Process is a heavy weight process. 2.Process is a separate program that has separate memory, data, resources etc. 3.Process are created using fork() method. 4.Context switch between the process is time consuming. Example: Say, opening any browser (mozilla, Chrome, IE). At this point new process will start to execute.
  • 11.
    Multiprocessing in OS Multiprocessing sometimes refers to executing multiple processes (programs) at the same time. Multitasking :- it is the process of executing several task simultaneously
  • 12.
    The process ofexecuting multiple threads concurrently is known as multithreading Multithreading in Java Let’s summarize the discussion in points: 1. The main purpose of multithreading is to provide concurrent execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread. 2. Threads are lightweight sub-processes, they share the common memory space. In Multithreaded environment, programs that are benefited from multithreading, utilize the maximum CPU time so that the idle time can be kept to minimum.
  • 13.
    A program (Process)is divided into two or more subprograms (process), which can be implemented at the same time in parallel/ concurrently Example:- suppose we run a program which include two methods M1 and M2 these methods are threads but run concurrently A Program Method-1 Method-2 Multithreading :-
  • 14.
    Multitasking Process based Threadbased  Facebook  Music Player  Browser Heavyweight Own Memory Address Space MS- Word Home Tab Spelling Check Threads Lightweight Address Space is share
  • 15.
  • 16.
    Multitasking vs. Multithreadingvs. Multiprocessing vs. parallel processing Multitasking: Ability to execute more than one task at the same time is known as multitasking. Multithreading: We already discussed about it. It is a process of executing multiple threads simultaneously. Multithreading is also known as Thread-based Multitasking. Multiprocessing: It is same as multitasking, however in multiprocessing more than one CPUs are involved. On the other hand one CPU is involved in multitasking. Parallel Processing: It refers to the utilization of multiple CPUs in a single computer system.
  • 18.
    Creating a threadin Java There are two ways to create a thread in Java: Thread in Java By extending Thread class extends Thread implementing Runnable interface Implements Runnable
  • 19.
    Every time aJava program starts up, one thread begins running which is called as the main thread of the program because it is the one that is executed when your program begins When you implements Runnable , you can save a space for your class to extend any other class in future or now. Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java Thread Creation in Java Main Thread Deference B/w Extending and implementing runnable class
  • 20.
    Before we beginwith the programs(code) of creating threads, let’s have a look at these methods of Thread class. We have used few of these methods in the example below. •getName(): It is used for Obtaining a thread’s name •getPriority(): Obtain a thread’s priority •isAlive(): Determine if a thread is still running •join(): Wait for a thread to terminate •run(): Entry point for the thread •sleep(): suspend a thread for a period of time •start(): start a thread by calling its run() method METHODS OF THREAD CLASS
  • 21.
    Method 1: Threadcreation by extending Thread class class MultithreadingDemo implements Runnable { public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); Thread objt1 = new Thread(obj); objt1.start(); } } Output: My thread is in running state.
  • 22.
    Method 2: Threadcreation by implementing Runnable Interface class MultithreadingDemo implements Runnable { public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); Thread tobj =new Thread(obj); tobj.start(); } } A Simple Example Output: My thread is in running state.
  • 23.
    Advantages of Multithreading Enable programmers to do multiple things at one time  Programmers can divided a long program into threads and execute them parallel which will eventually increases speed of program execution  Improved performance and concurrency  Simultaneous access of multiple applications
  • 24.
    Run Text Editor RunCommand Prompt