Thread in Java - 01
Thread in Java - 01
A thread is a separate flow of execution, each execution is having a separate job during the execution.We can define a thread by extending java.lang.Thread class and implementing Runnable interface.
Thread scheduler is part of JVM, It's responsible for scheduling Threads. That's if multiple threads are waiting to get the chance of execution, this execution order is decided by Thread scheduler.
We can not expect an exact algorithm is followed by thread scheduler. It's varied from JVM to JVM, Hence we can not expect thread execution order under an exact output.
Creating and starting a thread
In this post, I am going to explain how to create a thread by using Thread class, how to start this created thread, also analyze possible cases.
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("My Thread");
}
}
}
public class Main {
public static void main(String[] args) {
MyThread mythread = new MyThread();
mythread.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread");
}
}
}
In this above program, the MyThread class is created by extending Thread class. The override run() method is responsible for this thread execution when we start this MyThread object.
Inside this Main class, we call the main method so main thread is created(now the total number of thread is equal to 1, which is java main thread). Inside main method we create a MyThread class object, still, we have only one thread which is java main thread. In the next step we call start method by mythread.start() syntax, now another thread is created by our existing main thread, now two threads are running which are the main thread and MyThread Object thread. So the output is varied on each execution because it's depending on our Thread Scheduler algorithm.
Case 1: Calling run method directly.
public class Main {
public static void main(String[] args) {
MyThread mythread = new MyThread();
//mythread.start();
mythread.run();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread");
}
}
}
By executing this program, We will get an output which is same for each execution, because mythread.run() won't create a new thread but the program will be running on the main thread as the usual java program. So if we want to start a thread we should call start() method.
Case 2: Overriding start method inside MyThread class and call mythread.start() inside the Main class main method.
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("My Thread");
}
}
@Override
public void start() {
for (int i = 0; i < 10; i++) {
System.out.println("calling start);
}
}
}
public class Main {
public static void main(String[] args) {
MyThread mythread = new MyThread();
mythread.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread");
}
}
}
Can we expect the output from run() method implementation(for loop)?
No, Because when we call mythread.start() it will be executing the current start method implementation also it won't start mythread object thread so run() method won't execute.
If we put super.start() inside our implemented start() method we will get run() method execution output as well as start() method execution output with main thread output.
So the coding will be,
@Override
public void start() {
super.start();
super.start();
for (int i = 0; i < 10; i++) {
System.out.println("calling start);
}
}
Case 3: Overloading run() method with parameters.
If we overload the run method with certain parameter(s), which run method will be executing when we start this MyThread class object thread?
The empty parameter run() method only will be executing. If we want to call parameterized run method we should call this mythread.run(4) which won't to create any thread it will be only executed on the main thread.
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("My Thread");
}
}
//Thread start() method won't call this method
//Thread start() method won't call this method
public void run(int x) {
for (int i = 0; i < x; i++) {
System.out.println("Parameterized run method");
}
}
}
Case 4: After starting a thread again restart that thread.
After starting a thread if we trying to restart the same thread again, then it will throw an IllegalThreadExeception.
Case 4: After starting a thread again restart that thread.
After starting a thread if we trying to restart the same thread again, then it will throw an IllegalThreadExeception.
public static void main(String[] args) {
MyThread mythread = new MyThread();
mythread.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread");
}
//starting thread again
mythread.start(); //Exception will be thrown during the //execution.
}
Comments
Post a Comment