Thread in Java.

In this article,Thread in Java is explained in detail with examples.
A thread is a small work or task which is performed in a single program with same specific functionality technically,it is known as "Light-weight-process".
Executing two or more methods in a single Java program along with main() method is known as "Multi-threading".
Single thread Application
In this application,program execution starts with main() and line by line execution is done.Any other methods which are called in main method,they are executed one after the other in linear/sequential fashion. (From topic Thread in Java)
Multithreading-javaform
Multi-Threaded Application
In this application,program execution starts with main() method and line by line execution is done.Any other methods which are called in main() methods they are executed parallel at the same time by sharing CPU-time. (From topic Thread in Java)
MultithreadedApplication-javaform
Note
According to JVM,in Multithreading concept,main() method and other methods are known as thread.
Multi Tasking
Task means work in computer,such work in multiple times done at same time is known as Multi Tasking. (From topic Thread in Java)
It is basically of 2 types,
  • Process based.
  • Thread based.

**How to perform multithreading in Java program?
Thread is a class,available in "Java.lang" package using which we perform multi-threading.
Steps for multitasking
1) Inherit your class from thread class
Ex
class MyThread extends Thread
{
----
---
}
2)Now,redefine the following method of base class in your class for performing multi-threading logic.
Ex
class MyThread extends Thread
{
public void run()
{
----MT--logic---
}
}
3) Create an object of your thread-class.
Ex MyThread t=new Mythread();
4)Now invoke your run method by calling the following method.
Ex t.start();

Ex
//program related to Thread in Java --Multitasking
class ExampleThread1 extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("Hello Thread1");
}
}
}
class ExampleThread2 extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("\t \t Hello Thread2");
}
}
}
public class Thread1
{
public static void main(String args[])
{
ExampleThread1 t1=new ExampleThread1();
ExampleThread2 t2=new ExampleThread2();
t1.start();
t2.start();
}
}
Output
Multitasking-javaform
                             To get the code file use me.

Other methods of thread class.
Thread class provides multi threading in a java program using simple 4 steps.
This class also provides other necessary methods,which helps us to perform multi-threading very effectively in a program. (From topic Thread in Java)
Some necessary methods are
run();
It is redefined in our thread class to perform Multi-Threading Logic.
Start();
It is used to invoke or call run method of a particular thread class.
Stop();
It is used to STOP thread execution process.
destroy();
It does STOP and Garbage Collection from memory of a particular thread.
isAlive();
It checks whether a particular thread isAlive in JVM memory or not.
Suspend();
It suspends thread execution process till next signal.
resume();
It takes suspended thread back to execution process in JVM memory.
setName(String);
It provides a name to a particular thread.
getName();
It provides what is the name of a particular thread.
activeCount();
It provides the number of threads currently active in JVM memory.
setPriority(int);
It is used to provide priority to a particular thread(Levels 1-10).
getPriority();
It provides what is the current priority level of a particular thread.
sleep(long);
it makes a particular thread sleep for given number of milliseconds.
yield();
Giving a chance for other thread for execution in memory(voluntarily).
setDaemon(boolean);
it makes a particular thread with least priority level(1).
isDaemon();
Checks whether a particular thread is least priority thread or not.

Continue to the next topic User Defined Exceptions.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required