What is happening before multithreading?
old days a computer had a single CPU, and was only capable of executing a single program at a time (dos operating system). Later came multitasking which meant that computers could execute multiple programs at the same time creating mutltiple process.
What is multithreading?
To understand multithreading, the concepts process and thread must be understood. A process is a program in execution. A process may be divided into a number of independent units known as threads. A thread is a dispatchable unit of work. Threads are light-weight processes within a process . A process is a collection of one or more threads and associated system resources. The difference between a process and a thread is shownBenfits of multi threading?Main benfit using multi therading is better resource utilization we utlize the resources of cpu using multithreading.
What is Therad?
An instance of a thread is just an object like any other object in java,it has variable and methods and lives in heapbut thread exceution is indvidual stack. one call stack per therad.Example:while downlading file in webapplication it while take some time if devloper not implement multithrading we need to wait until file downlaod i mean we cant click any of other options in webpage if he implemnt mutithreading we can any of other action parlley.
How many ways we can create a thread?
We can create a thread in two ways.1.Extending the java.lang.Thread2.Implmenting the Runnable interfaceExtending java.lang.Thread is eaiset way 1.Extending java.lang.Thread2.Overrding run method.You always write the code that need to be run in separate thread in run method.
Multithreading Example 1:
package com.multi;
class ExtendingThread extends Thread{
/*
* write the code that need to be run in separate thread in run method.
*/
@Override
public void run(){------------------------------------4
for(int i=0;i<=10;i++){
System.out.println("HI this line is excutedby-------"+Thread.currentThread().getName()+":::Thread");
}
}
}
public class Main{
public static void main(String[] args) {
System.out.println("HI this line is excutedby-------"+Thread.currentThread().getName()+":::Thread");
ExtendingThread newThread=new ExtendingThread();------------------1
newThread.setName("surendra");--------------------------------------2
newThread.start();----------------------------------------------------3
System.out.println("HI this line is excutedby-------"+Thread.currentThread().getName()+":::Thread");
}
}
Output:
HI this line is excutedby-------main:::Thread
HI this line is excutedby-------main:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
HI this line is excutedby-------surendra:::Thread
Points description from the program:
1.Creating object for thread
2.We can set name for the thread using setName method
3.This is very important in this call start method of Thread class is excuted
within this method implementation thread will create.
if we call run method thread wont create.
0 comments: