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


Please Help Members By Posting Answers For Below Questions

Is java same as core java?

597


How do constructors use this() and super()?

553


how to run ecllipse with jettyserver for windows environment using batch file

1505


What is arrays sort in java?

578


What is the base class of all exception classes in java?

576






Explain all java features with real time examples

1212


What is difference between iterator and enumeration in java?

527


What is the SimpleTimeZone class?

1802


Where is const variable stored?

540


What are the advantages of functions?

525


What is bubble sorting in java?

600


What is the maximum size of array in java?

522


What is string data?

561


How concurrent hashmap works?

615


How do I get 64 bit java?

538