cant we call run() method directly instead of calling indirectly through the start()
method ? if we do so then what is the problem ?
Answers were Sorted based on User's Feedback
Answer / will
Calling run() without calling start() will effectively
execute run() in the current thread.
Calling start() will kick off a seperate thread,from your
current thread, which will then call run().
| Is This Answer Correct ? | 12 Yes | 4 No |
Answer / 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 |
Answer / neeraj_passion2001
yes you can call run() as ordinary method.
but while calling run() explicitly u can not achieve multi
threading.
| Is This Answer Correct ? | 8 Yes | 5 No |
Answer / damodar narayan
Calling run() method is just like calling a method in java. So when called, it just gets executed but "we do not get the thread functionality by calling the run() method". But calling start() method registers your program with the thread scheduler and hence it achieves the threading functionality. Start in turn calls the runs method.
Hope its clear.
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / rajkiran bande
@Answer 5 : Yes. But you'll eventually end up in getting
IllegealThreadStateException when run() is invoked for the
second time.
| Is This Answer Correct ? | 2 Yes | 0 No |
What is a driver in java?
What is java flatmap?
How do I run a project in eclipse?
What are the authentication modes in asp.net? : java security
public class Dog { private int weight; public int getweight(){ return weight; } public void SetWeight(int newWeight){ if (newWeight > 0){ weight = newWeight; } } } public class TestDog { public static void main(String[] args) { Dog d = new Dog(); System.out.println("Dog d's weight is " + d.getWeight()); d.setWeight(42); System.out.println("Dog d's weight is " + d.getWeight()); d.setweight(-42); System.out.println("Dog d's weight is " + d.getWeight()); } } class dog is compiled but there is an error in class TestDog when compiled and the error is with dot notations. I want to kmow why there is error in testdog class when compiled.
How common are security breaches? : java security
What is static class in java?
When a thread is created and started, what is its initial state?
program A and B are analysed and found to have worst case running time greater than 150NlogN and N*N respectively. which program has the better guarantee after the running time for the large values of N(N>10000)? which program has the better guarantee for the running time of small program N (N<100)? which program will run faster on average for N=1000?
0 Answers St. Pauls University,
What is jdk for netbeans ide?
Which is better openjdk or oracle jdk?
What is serializable in java?