The default implementation of java.lang.Thread 's run will only perform a task passed as a Runnable. If no
Runnable has been provided at construction time, then the thread will not perform any action.
When extending java.lang.Thread, you should override the run method or pass a Runnable target to the
constructor of java.lang.Thread.
public class MyThread extends Thread { // Noncompliant
public void doSomething() {
System.out.println("Hello, World!");
}
}
To fix this issue, you have 2 options:
run method
public class MyThread extends Thread {
@Override
public void run() {
System.out.println("Hello, World!");
}
}
Runnable at construction time
public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Hello, World!");
}
}
public class MyThread extends Thread {
public MyThread(Runnable runnable) {
super(runnable);
}
}
public class Main() {
public static void main(String [] args) {
Runnable runnable = new MyRunnable();
Thread customThread = new MyThread(runnable);
Thread regularThread = new Thread(runnable);
}
}