Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday, 18 April 2017

Java Thread

 How to create thread in java

In java multi-threading we can create a thread in two ways, these are

  1. By extending Thread class
  2. By implementing Runnable interface

Java Thread


Thread class in Java

Thread is a class in java programming language which provides certain constructors and methods so that we can easily create thread and perform many operations on a thread in java.

By default Thread class extends Object(Super class in java) class and implements runnable interface.


Constructors of Thread class

There are mostly used constructors in Thread class.
  • Thread()
  • Thread(String s)
  • Thread(Runnable r)
  • Thread(Runnable r, String s)


Methods of Thread class

There are many methods in thread class. Let's see

public void run()

This method is used to perform action for a thread and this is the entry point of the thread.

public void start(long milliseconds)

This method is used to start a thread for execution and JVM  calls run() method on the thread.

public void sleep()

This method is used to suspend or sleep a thread for specified number of milliseconds.

public void join()

This thread class method waits for a thread to terminate or die.

public void join(long milliseconds)

This thread class method waits for a thread to terminate or die for the specified milliseconds.

public String getName()

This method is used to obtains the name of the thread.

public int getPriority()

This method returns the priority of the thread.

public int setPriority(int priority)

This method returns the priority of the thread

public void setName(String name)

This method of thread class changes the name of the thread.

public Thread currentThread()

This method is used to get the details of currently executing thread like thread's name, thread's group name and priority.

public void suspend()

This method is used to suspend the thread(depricated method)

public void resume()

This method  is use to resume the suspended thread

public void stop()

This method is used to stop the thread(depricated method)

public int getId()

This method return the id of the thread


public boolean isAlive()

It is is used to checks if thread is still running

public boolean isDaemon()

Checks if the thread is daemon thread or not

public void setDaemon(boolean b)

This method marks the thread as daemon or user thread

public void interrupt()

It is used to interrupts the thread

public boolean isInterrupted()

Checks if the thread has been interrupted

public static boolean interrupted()

Checks if the current thread has been interrupted


Runnable Interface

Runnable is a interface which is available in java.lang package. Runnable interface containing only one method called run i.e  public void run() method.


Some rules when creating a thread using Runnable interface

  • create any user define class and implements runnable interface.
  • Override run() method within the user define class.
  • call start() method to execute run() method of thread class.

Thread Example by extending Thread class

For example :

class MyThread extends Thread
{
public void run()
{
System.out.println("My thread is running");
}
public static void main(String args[])
{
MyThread mt = new MyThread();
mt.start();
}
}

output : My thread is running


Thread Example by implementing Runnable Interface

For example :

class MyThread1 implements Runnable
{
public void run()
{
System.out.println("My thread is running using Runnable interface");
}
public static void main(String args[])
{
MyThread1 mt = new MyThread1();
Thread t = new Thread(mt);//Thread class object and passing object
t.start();
}


output : My thread is running using Runnable interface

If we are not extending Thread class , our class object would not be treated as thread class object. So we need to explicitly created thread class object and we are passing the object of our class that implements Runnable interface so that our class run() method may execute.

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate