hengsokchamroeun / javapns

Automatically exported from code.google.com/p/javapns
0 stars 0 forks source link

NotificationThreads.threadFinished is not thread-safe #92

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,all:

    /**
     * Worker threads invoke this method as soon as they have completed their work.
     * This method tracks the number of threads still running, allowing us
     * to detect when ALL threads have finished.
     * 
     * When all threads are done working, this method fires an AllThreadsFinished
     * event to the attached listener (if one is present) and wakes up any
     * object that is waiting for the waitForAllThreads() method to return.
     * 
     * @param notificationThread
     */
    public void threadFinished(NotificationThread notificationThread) {
        threadsRunning--;  // note this member variable in class NotificationThreads, i don't this this it is threadsafe
        if (threadsRunning == 0) {
            try {
                synchronized (finishPoint) {
                    finishPoint.notifyAll();
                }
            } catch (Exception e) {
            }
        }

    }

ps:

i write a test class, but i encounter a infinite wait, as follows:

public class Main {
    private static int THREAD_NUM = 500;

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        for(int i = 0; i < 500; i ++ ) {
            testCreatThreads();
        }

    }

    /**
     * 
     */
    public static void testCreatThreads() {
        NotificationThreads nt = new NotificationThreads(......, 500);  // 500 is the number of thread to be created
        nt.start();
        try {
            nt.waitForAllThreads();  // sometimes, it waits infinitely boundlessly
        } catch (InterruptedException e) {
        }

        System.out.println("it is ok");
    }
}

maybe add key word "synchronized " before threadFinished(...), is anyone 
encounters this?

thank all of u

Original issue reported on code.google.com by learnman...@gmail.com on 8 Dec 2011 at 4:35

GoogleCodeExporter commented 8 years ago
Indeed, that is not thread-safe.  Thank you for the report, I will fix this 
soon.

Original comment by sype...@gmail.com on 8 Dec 2011 at 4:51

GoogleCodeExporter commented 8 years ago
very many thanks

Original comment by learnman...@gmail.com on 9 Dec 2011 at 1:39

GoogleCodeExporter commented 8 years ago
Fixed in r342.

Original comment by sype...@gmail.com on 13 Dec 2011 at 2:48

GoogleCodeExporter commented 8 years ago
Issue 98 has been merged into this issue.

Original comment by sype...@gmail.com on 11 Jan 2012 at 4:47