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


Please Help Members By Posting Answers For Below Questions

Can I have multiple main methods in the same class?

477


What is crud operations in java?

466


Why is javac not recognized?

489


What's the difference between authentication and authorization? : java security

498


What does @override mean in java?

476






Can java program run without jdk?

504


What is jpa used for?

476


Does ms edge support java?

465


Do I need to install jre if I have jdk?

472


What is the use of entity class in java?

452


How do I run a java project?

484


what is the package for freshers in valuelabs.

5694


What is the java virtual machine (jvm)?

595


What is meant by java se?

515


What is exe file in java?

493