cant we call run() method directly instead of calling indirectly through the start()
method ? if we do so then what is the problem ?
Answer Posted / srinu
Calling run() without calling start() will effectively
execute run() in the current thread.then that time only one
thread it will be created.but n't achiving the mulithreading
concepts.
ex:-
class Sample extends Thread
{
Sample()
{
System.out.println("hai how are u");
}
public void run()
{
System.out.println("run method will be called");
}
}
public class ThreadExample
{
public static void main(String k[])
{
Sample s=new Sample();
s.run();
int k1=Sample.activeCount();
System.out.println(k1);
}
}
OUTPUT:
hai how are u
run method will be called
1
IN this program only one thread will be created.
start()--->
Calling start() will kick off a seperate thread,from your
current thread, which will then call run().
EX:-
class Sample extends Thread
{
Sample()
{
System.out.println("hai how are u");
}
public void run()
{
System.out.println("run method will be called");
}
}
public class ThreadExample
{
public static void main(String k[])
{
Sample s=new Sample();
s.start();
int k1=Sample.activeCount();
System.out.println(k1);
}
}
OUTPUT:
hai how are u
run method will be called
2
In this program 2 threads will be started.
| Is This Answer Correct ? | 7 Yes | 2 No |
Post New Answer View All Answers
What is an interceptor in java?
Can a dead thread be started again?
Which framework is best for rest api java?
What is a driver in java?
Why are command line arguments passed as a string?
What if I write static public void instead of public static void?
Which method is used to create the daemon thread?
How do I run a java project?
Describe life cycle of thread?
How do I open the java console in windows 10?
What is dao in java?
What is the meaning of loosely coupled in java?
How do you run an executable jar file?
How do I open java console?
What is jep in java?