Basic program
Create a Thread object using a class that implements the Runnable interface as an argument.
Override the run method.
class Runnable_A implements Runnable {
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println("Thread "
+ Thread.currentThread().getName());
}
}
}
class Runnable_B implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread "
+ Thread.currentThread().getName());
}
}
}
public class Test {
public st_atic void main(String[] args) {
Runnable_A r_a = new Runnable_A();
Runnable_B r_b = new Runnable_B();
Thread t_a = new Thread(r_a);
Thread t_b = new Thread(r_b);
t_a.setName("A"); // Set a Thread name
t_b.setName("B"); // Set a Thread name
t_a.st_art();
t_b.st_art();
}
}
<Result>
A Thread name to be displayed sometimes changes alternately.