what is use of threads how many ways to create thread
Answer Posted / madhusudhan
use of thread:1.concurrent execution of thread.
2.Program security.
Thread can be create in 2 ways
1.using Thread class
2.using Runnable interface
Example:
//first way of creating thread
class Thread1 extends Thread
{
public void run()
{
for(int i=0;i<1000;i++)
{
System.out.println(hashcode()+":"+i);
}
}
}
//second way of creating thread
class Thread2 implements Runnable
{
public void run()
{
for(int i=1000;i<2000;i++)
{
System.out.println(hashcode()+":"+i);
}
}
}
public class M
{
public static void main(String args[])
{
Thread1 t1=new Thread1();
t1.start();
Thread2 t2=new Thread2();
Thread t=new Thread(t2);
t.start();
}
}
| Is This Answer Correct ? | 22 Yes | 1 No |
Post New Answer View All Answers
What is a nested list?
Explain method overloading?
What is the program compilation process?
Can you pass functions in java?
What are actual parameters?
What does it mean to be immutable?
Explain the difference between comparator and comparable in java?
Tell some latest versions in JAVA related areas?
What is a local block?
What is abstraction in java?
What is a byte string?
What does %4d mean in java?
What is class array in java?
Is object a data type?
How do I type unicode?