- In java we can create user defined threads in two ways
- Implementing Runnable interface
- 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.
- public void run(){
- //logic
- }
- 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.
- package com.instanceofjava;
- class UserThread implements Runnable{
- public void run()
- {
- for (int i = 0; i < 5; i++) {
- System.out.println(i);
- }
- }
- public static void main(String [] args){
- UserThread userth= new UserThread();
- Thread t = new Thread(userth);
- t.start();
- }
- }
Output:
- 0
- 1
- 2
- 3
- 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.
- public class Thread
- extends Object
- implements Runnable
- package com.instanceofjava;
- class UserThread extends Thread{
- public void run()
- {
- System.out.println("thread started running..");
- }
- public static void main(String [] args){
- UserThread userth= new UserThread();
- Thread t = new Thread(userth);
- t.start();
- }
- }
Output:
- thread started running..
Thread class Constructors:
1.Thread()- Allocates a new Thread object.
- Allocates a new Thread object.
- Allocates a new Thread object.
4.Thread(String name)
- Allocates a new Thread object.
- 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:
- public void run(): method used to perform action for a thread. it is the initial point of custom thread execution.
- public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
- public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
- public void join(): waits for a thread to die.
- public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
- public int getPriority(): returns the priority of the thread.
- public int setPriority(int priority): changes the priority of the thread.
- public boolean isAlive(): tests if the thread is alive.
- public void yield(): This method execution causes the currently executing thread object to temporarily pause and allow other threads to execute.
- public void suspend(): method will be used to suspend the thread(depricated method).
- public void resume(): method used to resume the suspended thread(depricated method).
- public void stop(): method used to stop the thread(depricated method).
- public boolean isDaemon(): tests if the thread is a daemon thread.
- public void setDaemon(boolean b): marks the thread as daemon or user thread.
- public void interrupt(): interrupts the thread.
Java Example program on extends thread
- package com.instanceofjava;
- class UserThread extends Thread{
- public void run()
- {
- System.out.println("thread started running..");
- }
- public static void main(String [] args){
- UserThread userth= new UserThread();
- userth.start();
- }
- }
Output:
- thread started running..
No comments