Explain how to create a thread and start it running.
Creating a thread: Declare a class as a sub class of Thread
Class SampleThread extends Thread
{
Long minSample
{
SampleThread( Long minSample);
}
Public void run()
{
Program goes here
}
}
Run the thread: An instance is created to start the thread.
SampleThread s = new SampleThread(100);
s.start();
Explain how to create a thread and start it running.
There are 2 ways in which a thread can be created.
By extending the Thread class wherein the subclass needs to override the run method. Then just an instance of that class needs to be created and [classname].start() would start it running.
The other way is to declare a class that implements the runnable interface. Then the run() method needs to be implemented.
Then with following code the thread can be created and run.
SubThread a = new SubThread (143);
new Thread(a).start();