Thread creation

THREAD CREATION METHOD

0  0 2016-10-10 02:29:11

Java provides two ways to create the thread objects which are as follows:
1.By using the Runnable Interface and
2.By using with thread class

1.Thread Creation by using the Runnable Interface :
We can create thread on any object that implements Runnable.
To do this, follow certain steps:
a. Class implements a single method run()
public void run()
An object of this class is Runnable Object.

b. After creation of class we will instantiate an object type thread from within that class.
There are several construction are available for creating new thread instances, which are given below
• Thread()
• Thread(String)
• Thread(Runnable)
• Thread(Runnable)
• Thread(ThreadGroup,String)
• Thread(ThreadGroup,Runnable)
• Thread(ThreadGroup,Runnable,String)
• Thread(ThreadGroup,Runnable,String,long)

c. Finally invoke the Start() method for running the thread . This method is declared within Thread.
void start()

Example
package com.myjava.threads;
class MyRunnableThread implements Runnable{
public static int myCount = 0;
public MyRunnableThread()
{
       
}
public void run()
{
    while(MyRunnableThread.myCount <= 20){
                try
                {
                    System.out.println("Expl Thread: "+(++MyRunnableThread.myCount));
                    Thread.sleep(150);
                }
                catch (InterruptedException iex)
               {
                       system.out.println("Exception in thread: "+iex.getMessage());
               }
        }
   }
}
public class RunMyThread
{
    public static void main(String a[])
               {
        System.out.println("Starting Main Thread...");
                    MyRunnableThread mrt = new MyRunnableThread();
                   Thread t = new Thread(mrt);
                    t.start();
                while(MyRunnableThread.myCount <= 20)
    {
                    try
        {
                        System.out.println("Main Thread: "+(++MyRunnableThread.myCount));
                        Thread.sleep(100);
                    }
         catch (InterruptedException iex)
        {
                        System.out.println("Exception in main thread: "+iex.getMessage());
                    }
            }
            System.out.println("End of Main Thread...");
    }
}

2.Thread Creation By using thread class :
There is second way to create thread, that a class have to extend the Thread Class. For create a thread by this procedure you have to follow these
Steps:
a.Extend the java.lang.Thread Class.
b.Override the run() method in the subclass from the Thread class to define the code executed by the thread.
c.Creation an instance of this subclass. This subclass may call a thread class constructor by subclass constructor.
d.Invoke the start() method on the instance of the class to make the thread eligible for running.

Example
Class MyThread1 extends Thread
{
    MyThread1 (String s)
    {
        Super(s);
        Start();
    }
    public void run()
    {
        for(int i=0;i<5;I++)
        {
            System.out.println(“Thread Name.”+ Thread .currentThread().getName());
            Try
            {
                System.out.println(“Now “+Thread.currentThread(),getName()+”sleep…”);
                Thread.sleep(499);
                System.out.println(“”);
                System.out.println(“Now “+Thread.currentThread().getname()=” wakeup…”);
            }
            Catch(Exception e){}
        }
        System.out.println(“Finally “ + Thread .currentThread().getname()+” exited…”);
    }
    public class RunThread
    {
        Public static void main (String thre[])
        {
            System.out.println(“ Thread Name:”+Thread.currentThread ().getName());
            MyThread1 m1=new MyThread1(“ten”);
            MyThread1 m1=new MyThread1(“second”);
            MyThread1 m1=new MyThread1(“Third”);
            MyThread1 m1=new MyThread1(“Forth”);
            MyThread1 m1=new MyThread1(“Fifth”);
            MyThread1 m1=new MyThread1(“sixth”);
        }
    }

)

Comments (0)

Post Your Comments

Headquarter



Hong Kong Office



Find Us On

Website Development Company
SEO
Digital Marketing
website development company in Hong Kong
website designer in pune