• In  java we can create user defined threads in two ways 
  1. Implementing Runnable interface
  2. extending Thread class.

  • These are the two different ways to create thread in java
  • In these two ways first step we need to override run() method and place corresponding logic that should be executed concurrently.
  • The second thing is to call the start() method on Thread class object to create a thread of execution in Java Stack area.

1. Create thread in java using Runnable:

  • Simple and easiest way to create a thread is create a class that implements Runnable interface.
  • If you implement Runnable interface then our class instances will be executed by a thread.
  • Runnable interface having only one method called run().
  • After implementing the Runnable interface we need to override run() method of Runnable interface.
  1. public void run(){
  2. //logic
  3. }
  • This run(0 method introduces concurrent thread in to our program
  • The execution of thread will end when it reaches end of the run method.
  • run() method returns nothing.

  1. package com.instanceofjava;
  2.  
  3. class UserThread implements Runnable{
  4.  
  5.  public void run()
  6. {
  7.  
  8.  for (int i = 0; i < 5; i++) {
  9.       System.out.println(i);
  10.  
  11. }
  12. }
  13.  
  14. public static void main(String [] args){ 

  15.    
  16.       UserThread userth= new UserThread();
  17.  
  18.       Thread t = new Thread(userth);
  19.       t.start();
  20.  
  21. }
  22. }


Output:

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4

 


2. Java extends thread example


  • The second way to create object in java is by extending Thread class
  • Thread class having constructors and methods to create Thread
  • Thread class extends Object class and implements Runnable interface.

  1. public class Thread
  2. extends Object
  3. implements Runnable 

  1. package com.instanceofjava;
  2.  
  3. class UserThread extends Thread{
  4.  
  5.  public void run()
  6.  {
  7.       System.out.println("thread started running..");
  8.  }
  9.  
  10. public static void main(String [] args){ 

  11.    
  12.       UserThread userth= new UserThread();
  13.  
  14.       Thread t = new Thread(userth);
  15.       t.start();
  16.  
  17. }
  18. }


Output:

  1. thread started running..




Thread class Constructors:

1.Thread()
  • Allocates a new Thread object.
2.Thread(Runnable target)
  • Allocates a new Thread object.
3.Thread(Runnable target, String name)
  • Allocates a new Thread object.
4.Thread(String name)
  • Allocates a new Thread object.
5.Thread(ThreadGroup group, Runnable target)
  • Allocates a new Thread object.
6.Thread(ThreadGroup group, Runnable target, String name)
  • Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.

7.Thread(ThreadGroup group, Runnable target, String name, long stackSize)
  • Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group, and has the specified stack size.

Thread Class Methods:

  1. public void run(): method used to perform action for a thread. it is the initial point of custom thread execution.
  2. public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
  3. public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
  4. public void join(): waits for a thread to die.
  5. public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
  6. public int getPriority(): returns the priority of the thread.
  7. public int setPriority(int priority): changes the priority of the thread.
  8. public boolean isAlive(): tests if the thread is alive.
  9. public void yield(): This method execution causes the currently executing thread object to temporarily pause and allow other threads to execute.
  10. public void suspend(): method will be used to suspend the thread(depricated method).
  11. public void resume(): method used to resume the suspended thread(depricated method).
  12. public void stop(): method used to stop the thread(depricated method).
  13. public boolean isDaemon(): tests if the thread is a daemon thread.
  14. public void setDaemon(boolean b): marks the thread as daemon or user thread.
  15. public void interrupt(): interrupts the thread.

Java Example program on extends thread

  1. package com.instanceofjava;
  2.  
  3. class UserThread extends Thread{
  4.  
  5.  public void run()
  6.  {
  7.       System.out.println("thread started running..");
  8.  }
  9.  
  10. public static void main(String [] args){ 

  11.    
  12.       UserThread userth= new UserThread();
  13.  
  14.       userth.start();
  15.  
  16. }
  17. }

Output:

  1. thread started running..

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu