How to code a Thread in JAVA


To create a new Thread, there are two methods are available in Java;

One of them is extending the Thread class and the other is implementing the Runnable interface.

In this post I will explaing how to create a Thread by extending Thread class;

This is the example;

ThreadExample.java

package threadexample;

/**
 *
 * @author tunatore
 */
public class ThreadExample {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException {

        ThreadExample example = new ThreadExample();
        runThread(example);
      
    }
    public static void runThread(ThreadExample te) throws InterruptedException {

        Worker Thread1 = new Worker(“Thread 1 is running…”);
        Thread1.start(); //will call run method of Worker class

        synchronized(Thread1) {
            Thread1.join(); //waits for the Thread1’s executing (finish)
        }
        System.out.println(” thread name –> ” + Thread1.getName() + ” isAlive –> ” + Thread1.isAlive());
    }
}

Worker.java –> Thread

package threadexample;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tunatore
 */
public class Worker extends Thread {

    public Worker(String threadName)
    {
        super(threadName);
    }
    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.println(i + ” thread name –> ” + getName() + ” isAlive –> ” + isAlive());
            try {
                sleep((int) (Math.random() * 100));
            } catch (InterruptedException ex) {
                Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }

        System.out.println(“Program is stopped!”);
  
    }
}

the Program’s output is;

run:
0 thread name –> Thread 1 is running… isAlive –> true
1 thread name –> Thread 1 is running… isAlive –> true
2 thread name –> Thread 1 is running… isAlive –> true
3 thread name –> Thread 1 is running… isAlive –> true
4 thread name –> Thread 1 is running… isAlive –> true
5 thread name –> Thread 1 is running… isAlive –> true
6 thread name –> Thread 1 is running… isAlive –> true
7 thread name –> Thread 1 is running… isAlive –> true
8 thread name –> Thread 1 is running… isAlive –> true
9 thread name –> Thread 1 is running… isAlive –> true
Program is stopped!
 thread name –> Thread 1 is running… isAlive –> false
BUILD SUCCESSFUL (total time: 0 seconds)

Download Source

Advertisements

One thought on “How to code a Thread in JAVA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s