This is the code I have now and unfortunately the inherited class does not run in a seperate thread I want the inherited class to run in a separate thread. Please tell me what is the best way of doing this.
Regards!
Main
public class Main
{
public static void main(String[] args)
{
new Thread(new ClassA(country, category, false, false)).start();
}
}
Class A
ClassA extends Common implements Runnable
{
volatile String country;
volatile String category;
volatile Boolean doNotReset;
volatile Boolean doNotFlash;
public ClassA(String country, String category, Boolean doNotReset, Boolean doNotFlash)
{
this.country = country;
this.category = category;
this.doNotReset = doNotReset;
this.doNotFlash = doNotFlash;
}
@Override
public void run()
{
}
}
Common
public class Common
{
Common()
{
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println("name=" + name);
}
}
Your inherited class actually is running in a separate thread, but your currentThread debugging is done within the thread of the main method, rather than the Runnable ClassA instance. The ClassA instance is constructed (and therefore the Common constructor called) before the thread itself is actually started, so Thread.currentThread(), when called in the constructor, is still the main thread.
To fix the debugging to print the expected results, you can move the body of Common’s constructor to the overriden run() method in either Common or ClassA (as long as it remains the run() method called, as determined by polymorphism).