Java program on thread, it seems to be alright but there's something wrong at UserThread ut1 = new UserThread(5, "Thread A");
Program:
public class Ch10_2_2 {
class UserThread extends Thread {
private int length;
public UserThread(int length, String name){
super(name);
this.length = length;
}
public void run() {
int temp = 0;
for (int i = 1; i <= length; i++) temp += i;
System.out.println(Thread.currentThread() + "Sum = " + temp);
}
}
public static void main(String[] args) {
System.out.println("Thread: " + Thread.currentThread());
UserThread ut1 = new UserThread(5, "Thread A");
UserThread ut2 = new UserThread(5, "Thread B");
ut1.start(); ut2.start();
}
}
Error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type Ch10_2_2 is accessible. Must qualify the allocation with an enclosing instance of type Ch10_2_2 (e.g. x.new A() where x is an instance of Ch10_2_2). at Ch10_2_2.main(Ch10_2_2.java:21)