How to create a thread and start it running?

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();
How does thread’s stop method work
How does thread’s stop method work - Stop() method of thread stops the thread execution...
How do we specify pause times in my program?
How do we specify pause times in my program? - Using the sleep function in Java, the thread’s execution can be put on hold. During this pause session....
Multithreaded program and importance of thread synchronization
Multithreaded program and importance of thread synchronization - A multithreaded program involves multiple threads of control in a single program. Each thread has its own stack...
Post your comment