// @topic T11764 Multithreading -- wait-notify-interrupt example 02
// @brief main thread calls <tt>notify()</tt> and <tt>interrupt()</tt> on a waiting worker thread

package mth;

public class AppMain {
    // This sample is using a shared resource:
    public static SynchronizedCounter count = new SynchronizedCounter();

    public static void main(String[] args) {
        Thread ct = Thread.currentThread();
        System.out.println( "[" + ct.getName() + "] started");

        // create the IO thread
        Thread worker = new Thread( new IOThread() );

        // start the IO thread
        worker.start();

        System.out.println(
                "[" + ct.getName() + "] started "
                + "[" + worker.getName() + "]");

        try { // Thread.sleep() throws InterruptedException
            while( count.getValue() < 10 ) {
                count.increment();
                synchronized( worker ) {
                    worker.notify();
                }
                System.out.println( "[" + ct.getName() + "] count==" + count.getValue() );
                //Thread.yield();  // try yield instead (does not throw InterruptedException)
                Thread.sleep( 0 ); // try sleep zero or non-zero milliseconds
            }
        } catch( InterruptedException ex ) {
        }

        worker.interrupt();
        System.out.println("[" + ct.getName() + "] finished");
    }
}//class AppMain