JAVA Threads
1. What is a Thread...?
When simply put a thread is a program's path of execution. A java program may have one or more threads.
2. Purpose of having threads
The purpose of threads is to allow the program to do multiple things at one time. This helps to make time and resource efficient programs.
Ex: One thread can do calculations while another thread prints a data sheet and some other thread could deal with user in puts at the same time.If only one thread existed in the program all above tasks should have done one after another. Thanks to threads all three task can be perform at the same time and hence make the program time efficient. And also the resources (CPU time, the memory, etc.) of the computer could be waist if the program has to wait until it take all the inputs to do calculations. This could cause the resources to be idle for some time. Threading concept helps the program to utilize all the resources it has.
Having multiple threads in a program is called as Multithreading.
Having multiple threads in a program is called as Multithreading.
3. How to create a thread in java
Java has two approaches to create a thread.
- Implementing Runnable interface.
- Extending Thread class.
1. Implementing Runnable interface.
Runnable is an interface provided by java which only contains a single abstract method called "run". All the classes implement Runnable interface should override the public abstract void run(); method.
public class MyThreadClass implements Runnable
{
// Constructor
public MyThreadClass()
{
Thread myThread = new Thread(this, "My Thread"); // Create new thread named My Thread
System.out.println("Child Thread: " + myThread);
myThread.start(); // Start the thread
}
public class MyThreadClass implements Runnable
{
// Constructor
public MyThreadClass()
{
Thread myThread = new Thread(this, "My Thread"); // Create new thread named My Thread
System.out.println("Child Thread: " + myThread);
myThread.start(); // Start the thread
}
/**
* Overridden method from Runnable interface
*/
@Override
public void run()
{
try
{
System.out.println("Child thread: run method");
}
catch (InterruptedException ex)
{
System.out.println("Child thread interrupted");
}
}
}
/**
* Demo class
*/
public class DemoThread
{
// Main method
public static void main(String[] args)
{
System.out.println("Main thread: "+ Thread.currentThread());
new MyThreadClass(); // Create an object of MyThreadClass
try
{
System.out.println("Main thread running");
}
catch (InterruptedException ex)
{
System.out.println("Main thread interrupted");
}
}
}
When the program starts, main method of "DemoThread" class is invoked. Every java program has a main thread and it has the responsibility of executing the code parts written in main method.
So in above programs the it has two threads, the thread we created (My Thread) and the main thread.
When it creates a new object of "MyThreadClass" the constructor of that class creates a new thread called My Thread and starts it.
When "start" method is invoked on the created thread object it will invoke the overridden run method.
The "My Thread" is a separate thread that has a separate execution path from main thread.
{
System.out.println("Main thread: "+ Thread.currentThread());
new MyThreadClass(); // Create an object of MyThreadClass
try
{
System.out.println("Main thread running");
}
catch (InterruptedException ex)
{
System.out.println("Main thread interrupted");
}
}
}
2. Extending Thread class.
Thread java built in class which implements Runnable interface.
public class MyThreadClass extends Thread
{
// Constructor
public MyThreadClass()
{
super("My Thread"); // Create new thread object by calling super
System.out.println("Child Thread: "+this);
start(); // Start the thread
}
@Override
public void run()
{
try
{
System.out.println("Child thread: run method");
}
catch (InterruptedException ex)
{
System.out.println("Child thread interrupted");
}
}
}
The implementation of the demo class which has main method is same as the DemoThread class in above example.
When the program starts, main method of "DemoThread" class is invoked. Every java program has a main thread and it has the responsibility of executing the code parts written in main method.
So in above programs the it has two threads, the thread we created (My Thread) and the main thread.
When it creates a new object of "MyThreadClass" the constructor of that class creates a new thread called My Thread and starts it.
When "start" method is invoked on the created thread object it will invoke the overridden run method.
The "My Thread" is a separate thread that has a separate execution path from main thread.
Wow! In the end I got a webpage from where I know how to truly
ReplyDeleteobtain useful information regarding my study and knowledge.
My web-site :: farm & castle android cheats
Thanks. It's so long since I update this blog
ReplyDelete