Answer Posted / shankar reddy
A thread executes a series of instructions. Every line of
code that is executed is done so by a thread. Some threads
can run for the entire life of the applet, while others are
alive for only a few milliseconds.
Multithreading is the ability to have various parts of
program perform program steps seemingly at the same time.
Java let programs interleave multiple program steps through
the use of threads. For example,one thread controls an
animation, while another does a computation. In Java,
multithreading is not only powerful, it is also easy to
implement.
You can implement threads within a java program in two
ways – Creating an object that extends Class Thread or
implementing the interface Runnable.
The key difference between the two is that Thread class has
a strart() method which your program simple calls whereas
the Runnable class does not have a start method and you
must create a Thread object and pass your thread to its
constructor method. You never call run() method directly;
the start method calls it for you.
Example:
Class MyProcess extends Thread{
Public void run(){
}
public static void main(String args[]){
MyProcess mp = new MyProcess();
mp.start();
}
}
Class MyProcess implements Runnable {
Public void run(){
}
public static void main(String args[]){
Thread mp = new Thread(new MyProcess());
mp.start();
}
}
| Is This Answer Correct ? | 2 Yes | 1 No |
Post New Answer View All Answers
why not override thread to make a runnable? : Java thread
What is method in research paper?
What does @param args mean in java?
What is sleep method?
How variables are declared?
Can a vector contain heterogenous objects?
How would you convert bytes to string?
Is there is any difference between a scrollbar and a scrollpane?
Is null an object java?
Which method you will use to create a new file to store some log data. Each time a new log entry is necessary, write string to the file in java ?
Can java inner class be static?
Why is java so important?
How do you find the absolute value?
What is a marker interface?
What is the use of using enum to declare a constant?