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 a singleton in java?
Are the actual permissions for the application defined at run-time or compile-time? : java security
What is web container in java?
How do I run a project in netbeans?
What is meant by annotation in java?
What is gui in java with examples?
Write a program using call by refernce for two different classes to explain to print whether a given number is automorphic or not.
What is java lang noclassdeffounderror?
Why is class forname used in java?
Is java secure? : java security
What does persist mean in java?
What is hql in java?
Write a program for the problem: the array of inetegers indicating the marks of the students is given, U have to calculate the percentile of the students aaccording to this rule: the percentile of a student is the %of no of student having marks less then him. For eg: suppose Student Marks A 12 B 60 C 80 D 71 E 30 F 45 percentile of C = 5/5 *100 = 100 (out of 5 students 5 are having marks less then him) percentile of B = 3/5*100 = 60% (out of 5, 3 have markses less then him) percentile of A = 0/5*100 = 0%.
Why doesn't lsdou work under windows nt? : java security
In java what is the difference between sleep() and wait() .