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

What are the rules regarding quotation marks?

463


Is jdk a compiler?

461


I want to control database connections in my program and want that only one thread should be able to make database connection at a time. Define how can I implement this logic?

583


What is jersey in java?

466


What do you understand by numeric promotion?

521






What is meant by framework in java?

510


How do I run a project in eclipse?

449


What about javascript? : java security

527


What is a delimiter in java?

490


What is mdb in java?

511


What is persistence xml in java?

501


Who is at risk in java? : java security

498


What is an action class in java?

494


What do you mean by exception handling?

484


What are orm tools in java?

594