Greetings!
It is easy to create a thread in Java using 2 well known methods.
In Java 8;
New - when we create an instance of Thread class it is in New state.
Running - when we call start()
Suspended -
Blocked -
Terminated -
It is easy to create a thread in Java using 2 well known methods.
- extends Thread class.
- implement Runnable interface.
public class ExtendThread extends Thread {
@Override
public void run() {
System.out.println("hello from extended thread");
}
}
Thread thread = new ExtendThread();
thread.start();
public class ImplmentedRunnable implements Runnable {
@Override
public void run() {
System.out.println("hello from runnable implementation");
}
}
Thread thread = new Thread(new ImplmentedRunnable());
thread.start();
In Java 8;
Thread thread = new Thread(() -> System.out.println("hello from java 8"));
thread.start();
- It is not a good practice to extend the Thread class.
Thread states.
A thread exist in several states.New - when we create an instance of Thread class it is in New state.
Running - when we call start()
Suspended -
Blocked -
Terminated -
Comments
Post a Comment