We can
define a thread in the following two ways –
1. By
extending Thread class.
2.By
implementing Runnable interface.
Example –
public class
Thread1 extends Thread{
public void
run(){
for(int i=0;i<10;i++)
{
System.out.println("Child Thread");
}
}
public static void
main(String[] args) {
Thread1 t = new
Thread1();
t.start();
System.out.println("-----------------");
for(int i=0;i<10;i++)
{
System.out.println("Main Thraed");
}
}
}
Output –
-----------------
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
First
Thread1 object is created i.e., object t.start() control goes to Thread1, but
there is no start() method but it is provided by Thread class then job is
executed to call run() method.
Thread Scheduler – If multiple
threads are waiting to get the chance, then which thread will execute first is
decided by thread scheduler which is the part of the jvm. We can’t expect exact
behavior of thread scheduler and it is jvm dependent. Hence we can’t expect
exact execution order and exact output. We can’t tell exact output but we can
tell possible output for multithreaded program.
Difference between t.start() and t.run()
In the
case of t.start(), a new thread will be created which is responsible for the
execution of run() method(in that class object Thread1). But in the case of
t.run(), no new thread will be created and the run() method will be executed
just like a normal method call.
Example –
public class
Thread1 extends Thread{
public void run(){
for(int i=0;i<10;i++)
{
System.out.println("Child Thread");
}
}
public static void
main(String[] args) {
Thread1 t = new
Thread1();
t.run();
System.out.println("-----------------");
for(int i=0;i<10;i++)
{
System.out.println("Main Thraed");
}
}
}
Output –
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
-----------------
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Main Thraed
Importance of Thread class start() method –
The
start() method present in Thread class is responsible to perform all mandatory
activities which are required for our thread (like registering our thread with
thread scheduler etc)
Class Thread
{
void start()
{
1.
register this thread with thread
2. all
remaining functionalities written by
3.
invoke run() method
}
Here
without executing Thread class start() method there is no chance of starting
new thread in java(because it isn’t registered with Thread scheduler so no
memory created by jvm internally).
Overloading of run() method – We can
overload run() method but thread class start() method will always call no
argument run() method only. The other overloaded method we have to call
explicitly and it will be executed just like a normal method call.
Example –
public class
Thread1 extends Thread{
public void
run(){
for(int i=0;i<5;i++)
{
System.out.println("no argument");
}
}
public void
run(int a)
{
for(int i=0;i<5;i++)
{
System.out.println("int argument");
}
}
public static void
main(String[] args) {
Thread1 t = new
Thread1();
t.start();
}
}
Output –
no argument
no argument
no argument
no argument
no argument
Overriding run() method – If we
aren’t overriding run() method then Thread class run() method will be executed
which has empty implementation. Hence we don’t get any output.
Example –
public class
Thread1 extends Thread{
public void
run(int a)
{
for(int i=0;i<5;i++)
{
System.out.println("int argument");
}
}
public static void
main(String[] args) {
Thread1 t = new
Thread1();
t.start();
}
}
Overriding start() method – If we
override start() method, then our start() method will be executed just like a
normal method call and no new Thread will be created.
Example –
public class
Thread1 extends Thread{
public void start()
{
for(int i=0;i<5;i++)
{
System.out.println("start method");
}
}
public static void
main(String[] args) {
Thread1 t = new
Thread1();
t.start();
}
}
Output –
start method
start method
start method
start method
start method
Note - After calling start()
method in a thread, we aren’t allowed to restart the same thread once again, we
get the run time exception saying java.lang.IllegalThreadStateExecution
Example –
public class
Thread1 extends Thread{
public void
run(){
for(int i=0;i<10;i++)
{
System.out.println("Child Thread");
}
}
public static void
main(String[] args) {
Thread1 t = new
Thread1();
t.start();
t.start();
}
}
Output –
Exception in thread "main" Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
java.lang.IllegalThreadStateException
at
java.lang.Thread.start(Unknown Source)
at
threadd.Thread1.main(Thread1.java:13)
Thread by implementing Runnable interface –
We
can define a thread even by implementing Runnable interface too. Runnable
interface is present in java.lang package, it contains only one method called
run() method
Example –
public class
Thread1 implements Runnable{
public void
run(){
for(int i=0;i<10;i++)
{
System.out.println("Child Thread");
}
}
public static void
main(String[] args) {
Thread1 t = new
Thread1();
Thread t1 = new
Thread(t);
t1.start();
}
}
Output –
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
No comments:
Post a Comment