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() {


        // 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(){

        for (int k=0;k<100000;k++){


        System.out.println("The value of i is "+i);


    public static void main(String args[]) throws InterruptedException{

        ThreadClass obj = new ThreadClass();
        ThreadClass obj1 = new ThreadClass();

        String s="" ;
        Animal n = new Animal();



        System.out.println("The vsalue of i is "+obj.i);


I don't know what your overall intention is for how this code should execute, but
1. You have a single block of code synchronised on a single instance of Animal. That's legal but useless - for it to be of any value you need two synchronised blocks or two threads executing the same block sychronised on the same lock object.
2. You have a wait, but you don't have a notify or notifyAll anywhere to end the wait.

At a guess you may want to suspend the main thread until the run method finishes in its own thread? In which case join is a good solution - your conmented out version is wrong - you don't call it with the current thread, you call it using the thread you want to "join" ie the that you want to wait for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.