Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Thead Question

Q1.

How many way to define and instantiate a thread ? And which one is better
way and why ?

Ans:-

We can define and instantiate a thread by two way

1. Extending the java.lang.Thread class.


2. Implement the Runnable interface.

Extending the thread class is easiest way but it is not good approach,
because when you extending the java.lang.Thread class in your class,
you can’t extend any other class in your class in future. So object-
oriented perspective, suggesting for implementing Runnable interface.

Q2.

What is role of run() method ?

Ans:-

Any action that you want to execute in a separate thread as the job to
do in run() method.

Every thread in java executes in new call stack. When run() method is
called then a new stack is created and all things that are in run()
method will execute in new call stack.

public void run() {

// your code here

}
Q3.

What will be the output of the following code? It will gives compile time error
or run time ?

package org.com.core.thread;

public class TestThreads extends Thread {

public static void main(String[] args) {


Thread thread = new Thread();
System.out.println("TestThread");
thread.run();
}
}

Ans :-

The above code will execute successfully. Because if you extends the
thread and you have not override run() method then Thread will
execute its own run method.

Q4.

Can you overload run() method in your class ?

Ans:-

Yes, we are free to overload the run() method in our thread. By


example,

package org.com.core.thread;

public class MyThread extends Thread {

public void run() {


System.out.println("Thread's run method ");
}

public void run(int n) {


for (int i = 0; i < n; i++) {
System.out.println("Execute [ " + i + " ]");
}
}
}

The overloaded run(int n) method not see by the Thread class. Thead
class will expect only run() method with no arguments. And when it
will find the run() method with no argument, thread will executed in
separate call stack after the thread has been started. The method
run(int n) is just like a other normal method.

Q5.

What will be the output of the following code ?

package org.com.core.thread;

public class TestThreads extends Thread {

public static void main(String[] args) {


Thread thread = new Thread();
System.out.println("TestThread");
thread.run();
}

public void run() {


System.out.println("In run method ");
}
}

Ans.

Output :-

TestThread

This is because of Overriding concept.

Thread thread = new Thread();

thread instance of Thread() class so it will execute the Thread’s run()


method.
If you want to run the TestThread’s run() method then you have to
change in the line like as –
Thread thread = new TestThreads();

Q6.

What is role of isAlive() method ?

Ans.

isAlive() is method of Thread class. Its return type is Boolean. This


method is used to find any thread is in alive or not ?

Q7.

What will be the output of the following code?

package org.com.core.thread;

public class MyFirstRunnable implements Runnable {

public void run() {


System.out.println("This is my first Runnable method ");
}
public static void main(String[] args) {
MyFirstRunnable mfr = new MyFirstRunnable();
mfr.run();
}
}

Ans.

Output:-

This is my first Runnable method

In above code, mfr is an instance of MyFirstRunnable class and


through this mfr calling run() method. It will execute the run method
of this class but in same stack. Above code will not start the new
thread of execution?

Same as for –

Thread t = new Thread();

t.run();

or

Runnable r = new Runnable();

r.run();

Q8.

What is the role of getName() and setName() method?

Ans.

Working of these methods can be understand by following example

package org.com.core.thread;

class MyRunnable implements Runnable {

public void run() {


System.out.println("My Runnable's run method is running");
System.out.println("By [ " + Thread.currentThread().getName()
+ " ]");
}

public class MyThread {


public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
Thread th = new Thread(mr);
th.setName("Pawan");
th.start();
}

Output:

My Runnable's run method is running


By [ Pawan ]

Q9.

What is the role of start() method ?

Ans.

Every thread will start by calling of thread’s start() method. When we


call this method then a new stack is created and all things of started
thread’s run method is executed in created stack.

Example-
package org.com.core.thread;

class MyRunnable implements Runnable {

public void run() {


System.out.println("My Runnable's run method is
running");
System.out.println("By [ " +
Thread.currentThread().getName() + " ]");
}

public class MyThread {


public static void main(String[] args) {
1-------- MyThread mt = new MyThread();
2-------- mt.fun1();
3-------- mt.thFun();
}

public void fun1() {


2.1------ System.out.println("In function 1");
}

public void thFun() {


3.1------ MyRunnable mr = new MyRunnable();
3.2------ Thread t = new Thread(mr);
3.3------ t.setName("Pawan");
3.4------ t.start();
}
}

The above code will execute from main that is itself a thread so it will
run in separate stack.

mt.fun1(); is called it will go in same stack i.e stack 1, fun1()


method will executed in same stack.
Again, mt.thFun(); is called it will go in same stack 1 as –

While thFun() executing it will reach t.start(); this line then it will
starts new thread and now it will create a new stack like as
Q10.

Once a thread is started than it can be start again() ?

Ans.

NO, once a thread has been started, it can never be start again. Still
you do this; it will give RuntimeException name as
IllegalThreadStateException. In any case, either thread’s run() method
has completed or not, means a runnable thread or dead thread cannot
be restarted.

Example –
package org.com.core.thread;

public class TestThreads extends Thread {

public static void main(String[] args) {


TestThreads thread = new TestThreads();
System.out.println("TestThread");
thread.start();
thread.start();

public void run() {


System.out.println("In run method ");
}
}
Output –
TestThread
In run method
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at org.com.core.thread.TestThreads.main(TestThreads.java:9)

Q11.

What is Thread Scheduler?

Ans.

Thread scheduler is a part of JVM, that decides which thread will go in


running state or out of the running state at any given movement.

Q12.

What is Runnable pool?

Ans.

When a thread is started then it will go in runnable state. It may be at


any time more than one thread in runnable state. All the threads in
runnable state places in queue. This queue is called Runnable pool.
Any thread will be go for running state from Running pool.
Q13.

Define the Life cycle of thread?

Ans.

A Thread can be only in one of five states.

 New
 Runnable
 Running
 Waiting/blocked/sleep
Q14.

What is role of Priority of Threads while any thread is running ?

Ans.

A thread that is in running state, their priority either should be equal


or greater than to priority of thread that are in runnable pool.

Every thread has some priority that are represented by a some


number from 1 to 10. Even main is also has a priority main thread has
5. 5 is default priority. Thread class has three constants for defining
the range of thread priority.
Thread.MIN_PRIORITY(1)

Thread.NORM_PRIORITY(5)

Thread.MAX_PRIORITY(10)

MyRunnable r = new MyRunnable();

Thread t = new Thread(r);

t.setPriority(5);

t.start();

Q15.

What is role yield() method?

Ans.

yield() method is static method of Thread class. The job of the method
is to make a chance for other thread that are having same priority in
Runnable pool. And running thread will go back in Runnable pool.
Q16.

What is role join() method?

Ans.

join() method is non-static method of Thread class. If you have a


thread B that work is depending of the work of Thread A means Thread
B cannot do its work until Thread A has completed its work.

For this, Thread B will join Thread A, After that Thread B will not be in
runnable until Thread A has finishd.

You might also like