First question is,What is runnable?
So runnable is an interface which is used to provide concurrency to the application by providing the threads to the application.It is the general way of creating threads.
Why should we use runnable interface over Thread class?
If we use Thread class then we have to create our own threads.We will have to manage it on our own.Means a lot of burden will be on the application.
Lets know the execution of runnable interface.So runnable object can be executed by Executor object.So what is this Executor?
An Executor object is used to execute runnable objects.It does this by creating and managing a group of threads in a thread pool.When a Executor begins executing a Runnable,the executor calls the Runnable's run method which is executed in the new thread.
So the next question is how does this Executor manages the threads?
Executor interface has a single method named as execute() which accepts the Runnable as argument.When execute method is called it selects a thread and assigns this thread to the passed Runnable object.If there is no thread available in thread pool then Executor creates the new thread and assigns it to the Runnable object.So this is the main advantage of using Runnable over Thread class.Since Executor can reuse the threads to eliminate the overhead of creating the thread whenever possible.This will optimize the performance of application.
Executor Service can be started by following statement:
ExecutorService service = Executors.newCachedThreadPool();
So runnable is an interface which is used to provide concurrency to the application by providing the threads to the application.It is the general way of creating threads.
Why should we use runnable interface over Thread class?
If we use Thread class then we have to create our own threads.We will have to manage it on our own.Means a lot of burden will be on the application.
Lets know the execution of runnable interface.So runnable object can be executed by Executor object.So what is this Executor?
An Executor object is used to execute runnable objects.It does this by creating and managing a group of threads in a thread pool.When a Executor begins executing a Runnable,the executor calls the Runnable's run method which is executed in the new thread.
So the next question is how does this Executor manages the threads?
Executor interface has a single method named as execute() which accepts the Runnable as argument.When execute method is called it selects a thread and assigns this thread to the passed Runnable object.If there is no thread available in thread pool then Executor creates the new thread and assigns it to the Runnable object.So this is the main advantage of using Runnable over Thread class.Since Executor can reuse the threads to eliminate the overhead of creating the thread whenever possible.This will optimize the performance of application.
Executor Service can be started by following statement:
ExecutorService service = Executors.newCachedThreadPool();
Program Code:
package examples;
public class ThreadEx implements Runnable{
String name;
int time;
public ThreadEx(String name,int time){
this.name=name;
this.time=time;
}
@Override
public void run() {
System.out.println("Hi this is thread "+name);
try{
Thread.sleep(time);
}catch(InterruptedException e){
return ;
}
System.out.println("I am last executable line in thread "+name);
}
}
package examples;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadDemo {
public static void main(String[] args) {
ThreadEx t1=new ThreadEx("t1",2000);
ThreadEx t2=new ThreadEx("t2",1000);
ThreadEx t3=new ThreadEx("t3",1500);
System.out.println("Starting Executor");
ExecutorService service=Executors.newCachedThreadPool();
service.execute(t1);
service.execute(t2);
service.execute(t3);
service.shutdown();
System.out.println("Executor Shutdown");
}
}
Output1:
Starting Executor
Hi this is thread t1
Executor Shutdown
Hi this is thread t2
Hi this is thread t3
I am last executable line in thread t2
I am last executable line in thread t3
I am last executable line in thread t1
Output2:
Starting Executor
Executor Shutdown
Hi this is thread t2
Hi this is thread t1
Hi this is thread t3
I am last executable line in thread t2
I am last executable line in thread t3
I am last executable line in thread t1
In output2 main thread terminates before any of the ThreadEx objects outputs their statements and in output1 t1 object has output its statement before main thread terminated.This illustrates the face that we can't predict the order in which the tasks will be executed.It doesn't dependes upon the order in which they have created and started.package examples;
public class ThreadEx implements Runnable{
String name;
int time;
public ThreadEx(String name,int time){
this.name=name;
this.time=time;
}
@Override
public void run() {
System.out.println("Hi this is thread "+name);
try{
Thread.sleep(time);
}catch(InterruptedException e){
return ;
}
System.out.println("I am last executable line in thread "+name);
}
}
package examples;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadDemo {
public static void main(String[] args) {
ThreadEx t1=new ThreadEx("t1",2000);
ThreadEx t2=new ThreadEx("t2",1000);
ThreadEx t3=new ThreadEx("t3",1500);
System.out.println("Starting Executor");
ExecutorService service=Executors.newCachedThreadPool();
service.execute(t1);
service.execute(t2);
service.execute(t3);
service.shutdown();
System.out.println("Executor Shutdown");
}
}
Output1:
Starting Executor
Hi this is thread t1
Executor Shutdown
Hi this is thread t2
Hi this is thread t3
I am last executable line in thread t2
I am last executable line in thread t3
I am last executable line in thread t1
Output2:
Starting Executor
Executor Shutdown
Hi this is thread t2
Hi this is thread t1
Hi this is thread t3
I am last executable line in thread t2
I am last executable line in thread t3
I am last executable line in thread t1
0 comments:
Post a Comment