Counter.java
public class Counter implements Runnable{
public void run(){
System.out.println("I am Counter Run Method");
}
}
Main.java
public class Main {
public static void main(String[] args) {
Counter objCounter = new Counter();
Thread objThread = new Thread(objCounter);
objThread.start();
}
}
I am Counter Run Method
Main.java(Using Lambda Expression)
public class Main {
public static void main(String[] args) {
Thread objThread = new Thread(() -> {
System.out.println("I am lambda Counter method");
});
objThread.start();
}
}
I am lambda Counter method
- When you create thread using new Operator it would be in “new” state
- Once you call start method over thread it would Transend to “runnable” state
- When run method completed execution it would Transend to “terminated” state
Thread can be invoked using start or run method. If you use the myThread.start() method it initiates execution of new thread, jvm takes care of execution and scheduling of run method in separate concurrent context. Calling the run method directly myThread.run() will not start a new thread; it will execute the run method in the current thread (ie main thread).
start method of thread class is implemented as when it is called a new Thread is created and code inside run() method is executed in that new Thread. While if run method is executed directly than no new Thread is created and code inside run() will execute on current Thread and no multi-threading will take place
public class Main {
public static void main(String[] args) {
Thread objThread = new Thread(() -> {
System.out.println(Thread.currentThread().getName());
});
objThread.start();
}
}
Output
Thread-0
public class Main {
public static void main(String[] args) {
Thread objThread = new Thread(() -> {
System.out.println(Thread.currentThread().getName());
});
objThread.run();
}
}
Output
main
- – Initiates the execution of the thread, causing the run method to be called
myThread.start();
- – Contains the code that will be executed by the thread. This method needs to be
overridden when extending the Thread class or implementing the Runnable interface.
Usage: Defined by the user based on the specific task.
- – Causes the thread to sleep for the specified number of milliseconds,
pausing its execution.
Thread.sleep(1000);
- Waits for the thread to complete its execution before the current thread continues. It
is often used for synchronization between threads.
myThread.join();
- Interrupts the thread, causing it to stop or throw an InterruptedException. The thread
must handle interruptions appropriately.
myThread.interrupt();
- Returns true if the thread has been started and has not yet completed its execution,
otherwise returns false.
boolean alive = myThread.isAlive();
- Sets the name of the thread.
myThread.setName("MyThread");
- Returns the name of the thread.
String threadName = myThread.getName();
- Returns the name of the thread.
String threadName = myThread.getName();
- Sets the priority of the thread. Priorities range from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY.
myThread.setPriority(Thread.MAX_PRIORITY);
- Returns the priority of the thread.
int priority = myThread.getPriority();
- Returns a reference to the currently executing thread object.
Thread currentThread = Thread.currentThread();
Thread Implementation Using Runnable Interface
public class Threads2 implements Runnable
{
public static void main(String[] args)
{
Threads2 objThreads2 = new Threads2();
Thread t1 = new Thread(new Threads2());
t1.start();
}
public void run()
{
System.out.println("Thread Started");
for(int i=0;i<=5;i++)
{
System.out.println(i);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("Thread Stopped");
}
}
There are Four Constructors we can use in Runnable Method of Implementation of Threads.
Thread() – Calls Thread Version of run Method
Thread(Runnable Target)
Thread(Runnable Target, String Name)
Thread(String Name)
If we dont Pass the Instance of Class To thread Constrctor then it will call its own Run Method
Thread t = new Thread(new MyThread());