Search code examples
javamultithreadingconcurrencywaitnotify

Can some one explain why program doesn't end?


Hi, I'm new to the java thread.I'm trying to figure out what is the wait() and notify() methods.So I wrote simple programme but I can't find the reason for infinity execution.Please, can someone help me to solve this?

public class StairCase {
    public static void main(String[] args) {
        int[] lst = {1,2,3,4,5,6,7};
        Test1 t1 = new Test1(lst);
        t1.setName("Test 1 ");
        Test2 t2 = new Test2(lst);
        t2.setName("Test 2 ");
        t1.start();
        t2.start();
    }
}

class Test1 extends Thread {
    int[] line ;
    public Test1(int[] lst) {
        this.line = lst;
    }
    public void run(){
        synchronized(line) {
            for (int i = 0; i < 5; i++) {
                try{
                    if(i == 2) line.wait();
               } catch (Exception e) {

               }
               System.out.println(Thread.currentThread().getName() + line[i]);
            }
        }
    }

}

class Test2 extends Thread {
    int[] line ;
    public Test2(int[] lst) {
        this.line = lst;
    }
    public void run() {
        synchronized(line) {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + line[i]);
            }
        }
        try {
            line.notify();
        } catch(Exception e) {
        }
    }
}

Solution

  • public class StairCase {
        public static void main(String[] args) {
            int[] lst = {1,2,3,4,5,6,7};
            Test1 t1 = new Test1(lst);
            t1.setName("Test 1 ");
            Test2 t2 = new Test2(lst);
            t2.setName("Test 2 ");
            t1.start();
            t2.start();
        }
    }
    
    class Test1 extends Thread {
        private final int[] line ;
        Test1(int[] lst) {
            this.line = lst;
        }
        public void run(){
            synchronized(line) {
                for (int i = 0; i < 5; i++) {
                    try{
                        if(i == 2) line.wait();
                    } catch (Exception e) {
                      e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + line[i]);
                }
            }
        }
    
    }
    
    class Test2 extends Thread {
        private final int[] line ;
        Test2(int[] lst) {
            this.line = lst;
        }
        public void run() {
            synchronized(line) {
                for (int i = 0; i < 5; i++) {
                    System.out.println(Thread.currentThread().getName() + line[i]);
                }
            }
            try {
                synchronized (line) {
                    line.notify();
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    try this code is this the result you expected? if not let me know what is the expect result.

    I have added e.printStackTrace(); to the catch blocks and checked weather if there was an Exception, and there was an exception, the exception was at line 45 in notify() so I have surrounded the notify with synchronized statement.

            synchronized (line) {
                line.notify();
            }