Creation Of Threads

There are two ways to provide code that will run in a different thread:

  1. Subclass Thread class
  2. Implement the Runnable interface
public class DefineExecuteThread
{
    public static class SubThread extends Thread // first way
    {
        public void run()
        {
            System.out.println("this runs in SubThread");
        }
 
    }
 
    public  static class ThreadRunnable implements Runnable //second way
    {
 
        public void run()
        {
            System.out.println("this runs in ThreadRunnable");
 
        }
 
    }
 
    public static void main(String[] args)
    {
        SubThread st = new SubThread();
        st.start();
 
        Thread tr = new Thread(new ThreadRunnable());
        tr.start();
 
    }
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.