In how many ways we can the thread? in java

Answer Posted / shaik baji

We can the Thread in two ways as follows

1) By implements the "Runnable" Interface
For Example:
class ThreadDemo implements Runnable
{
public static void main(String[] args)
{
Thread t = new Thread(new ThreadDemo());
t.start();
System.out.println("End of main thread");
}
public void run()
{
for(int i=1; i<=10; i++)
System.out.println(i);

System.out.println("End of child thread");
}

}



2) By extents the "Thread" class
NOTE: Thread is not a abstract class.

For Example:
------------

class ThreadDemo extends Thread
{
public static void main(String[] args)
{
Thread t = new ThreadDemo();
t.start();
System.out.println("End of main thread");
}
public void run()
{
for(int i=1; i<=10; i++)
System.out.println(i);

System.out.println("End of child thread");
}

}

Is This Answer Correct ?    7 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the features of java?

597


Can we start a thread twice in java?

512


Can we call the constructor of a class more than once for an object?

635


Can we assign null to double in java?

552


What is scope & storage allocation of global and extern variables? Explain with an example

583






What is the function of compareto in java?

593


What is the java idl system?

585


What do you mean by global variable?

529


What is the final field modifier?

550


Why collection is called framework in java?

540


Explain the init method?

544


Distinguish between a predicate and a function?

575


Is a method a function?

552


placement papaers of spring computing technology

1026


What was java originally called?

514