I was doing my workshop and the question appear as the following:
Reverse hello
Write a program called ReverseHello.java that creates a thread (let’s call it Thread 1).
Thread 1 creates another thread (Thread 2); Thread 2 creates Thread 3; and so on, up to Thread 50.
Each thread should print “Hello from Thread !”, but you should structure your program such that the threads print their greetings in reverse order.
my code is like this:
public class Task2 implements Runnable {
static int threadNo = 1;
@Override
public void run() {
if (threadNo <= 50) {
Thread reverse = new Thread(new Task2());
reverse.setName("Thread "+ threadNo);
threadNo++;
reverse.start();
try {
reverse.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("hello from " + Thread.currentThread().getName());
}
public static void main(String[] args) throws InterruptedException {
Thread t2 = new Thread(new Task2());
t2.start();
}
}
the output is like this:
hello from Thread 50
hello from Thread 49
hello from Thread 48
hello from Thread 47
hello from Thread 46
hello from Thread 45
hello from Thread 44
hello from Thread 43
hello from Thread 42
hello from Thread 41
hello from Thread 40
hello from Thread 39
hello from Thread 38
hello from Thread 37
hello from Thread 36
hello from Thread 35
hello from Thread 34
hello from Thread 33
hello from Thread 32
hello from Thread 31
hello from Thread 30
hello from Thread 29
hello from Thread 28
hello from Thread 27
hello from Thread 26
hello from Thread 25
hello from Thread 24
hello from Thread 23
hello from Thread 22
hello from Thread 21
hello from Thread 20
hello from Thread 19
hello from Thread 18
hello from Thread 17
hello from Thread 16
hello from Thread 15
hello from Thread 14
hello from Thread 13
hello from Thread 12
hello from Thread 11
hello from Thread 10
hello from Thread 9
hello from Thread 8
hello from Thread 7
hello from Thread 6
hello from Thread 5
hello from Thread 4
hello from Thread 3
hello from Thread 2
hello from Thread 1
hello from Thread-0
I could not figure out how this last one hello from Thread-0 is from and how to get rid of this one as the question is asking only from thread 1 to 50.
The very first thread that you start from your main method never gets a thread name set, so it will have a default name assigned by the JVM, which turns out to be "Thread-0".
A simple solution is to set the thread name at the beginning of the run method on the current thread, instead of on the started reverse thread:
@Override
public void run() {
Thread.currentThread().setName("Thread "+ threadNo);
if (threadNo <= 50) {
This code should solve your problem.
public class Task implements Runnable {
private final int threadNo;
public Task(int threadNo) {
this.threadNo = threadNo;
}
@Override
public void run() {
if (threadNo <= 50) {
Thread reverse = new Thread(new Task(threadNo + 1));
reverse.setName("Thread " + threadNo);
reverse.start();
try {
reverse.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (threadNo > 1) {
System.out.println("hello from " + Thread.currentThread().getName());
}
}
public static void main(String[] args) {
Thread t2 = new Thread(new Task(1));
t2.start();
}
}
Thread-0 is the name of the initial thread created and started from main method without incrementing the thread counter in Task2.
So to fix this issue it is sufficient to start the initial thread as:
new Thread(new Task2(), "Thread "+ Task2.threadNo++).start();