Dear All,
I have an animal class and a thread class. Just to understamd the concept of Object lock in threads. I have used the Animal instance as a lock in my thread class. The program never ends. My question is is there a condition i can put before invoking wait ,,like checking whether that thread is already completed or something like that. How to reach the output statement in my thread class. Thanks in advance.
import java.awt.List;
import java.util.ArrayList;
public class Animal {
public Animal() {
System.out.println("sample");
// TODO Auto-generated constructor stub
}
public ArrayList<?> go(){
return new ArrayList<Object>();
}
public static void main(String args[]) throws InterruptedException{
}
}
public class ThreadClass extends Thread {
private long i =1;
public ThreadClass() {
// TODO Auto-generated constructor stub
}
public void run(){
//System.out.println(Thread.currentThread().getName());
//System.out.println("a");
for (int k=0;k<100000;k++){
i++;
}
System.out.println("The value of i is "+i);
}
public static void main(String args[]) throws InterruptedException{
ThreadClass obj = new ThreadClass();
obj.setName("mine");
ThreadClass obj1 = new ThreadClass();
String s="" ;
Animal n = new Animal();
obj.start();
//Thread.currentThread().join();
synchronized(n){
n.wait();
System.out.println("The vsalue of i is "+obj.i);
//obj1.start();
}
}
}