A A A i

billet précédent :: billet suivant

Re: One thing I've never understood with Java threads

As promised, a code sample showing the behaviour of wait() and notify(). Several of them exist already on the net, but sometimes, if you don't look by yourself, you don't understand. The code is a bit long (90 lines).

package sandbox;
public class TimeOut implements Runnable {
    private boolean forceStop;
    private int timeToRun;
    private boolean finished;

    public TimeOut() {
        forceStop = false;
        finished = false;
    }
    
    public void demo(int p_timeToWait, int p_timeToRun) {
        timeToRun = p_timeToRun;
        Thread t = new Thread(this); 
        forceStop = false;
        finished = false;
        t.start();

        try {
            synchronized(t) {
                trace("Monitor acquired");
                t.wait(p_timeToWait);
                trace("Monitor re-acquired");
            }
        } catch (InterruptedException ie) {
            trace("Interrupted while waiting for time-out");
        }

        if (t.isAlive() && !finished) {
            trace("Time out!");
            forceStop = true;
            try {
                trace("Waiting for effective end of work");
                t.join();
                trace("End of work reached");
            } catch (InterruptedException ie) {
                trace("Interrupted while waiting for effective end of work");
            }
        }
    }

    public void run() {
        trace("Starting to work");
		// détermination du port
        for(int i = 0; i < 10 && !forceStop; ++i) {
            try {
                Thread.sleep(timeToRun / 10);
                trace("Working...");
            } catch (InterruptedException ie) {
                trace("Interrupted");
            }
        }
        
        int remaining = timeToRun - (timeToRun / 10) * 10;
        if(remaining > 0) {
            try {
                Thread.sleep(remaining);
            } catch (InterruptedException ie) {
                trace("Interrupted remaining");
            }
        }
        
        finished = true;

        if(!forceStop) {
            forceStop = false;
            trace("Notifying end of work");
            synchronized (Thread.currentThread()) {
                Thread.currentThread().notify();
            }
        }
        
        trace("End of work");
    }

    private static void trace(String s) {
        System.out.println(System.currentTimeMillis()+": "+s);
    }

    public static void main(String[] argv) {
        TimeOut to = new TimeOut();
        trace("Demo 15, 20");
        to.demo(15, 20);
        trace("===");
        trace("Demo 20, 15");
        to.demo(20, 15);
    }
}

A remark, the test "t.isAlive()" is not sufficient, because there is no guarantee that run() finishes before wait() is released. Here is a sample output:

javaw.exe -classpath . sandbox.TimeOut

1057702792318: Demo 15, 20
1057702792318: Monitor acquired
1057702792318: Starting to work
1057702792318: Working...
1057702792318: Working...
1057702792318: Working...
1057702792328: Working...
1057702792328: Working...
1057702792328: Working...
1057702792328: Monitor re-acquired
1057702792328: Time out!
1057702792328: Waiting for effective end of work
1057702792328: Working...
1057702792328: End of work
1057702792328: End of work reached
1057702792328: ===
1057702792328: Demo 20, 15
1057702792328: Monitor acquired
1057702792328: Starting to work
1057702792338: Working...
1057702792338: Working...
1057702792338: Working...
1057702792338: Working...
1057702792338: Working...
1057702792338: Working...
1057702792338: Working...
1057702792338: Working...
1057702792338: Working...
1057702792338: Working...
1057702792348: Notifying end of work
1057702792348: End of work
1057702792348: Monitor re-acquired

Enjoy!(Cyberpunk, 2003/07/08 18:26) lien permanent

Les commentaires pour ce billet sont fermés.


qui est

nom : Damien Bonvillain
courriel : kame à cinemasie.com
bloggercode:
B9 D+ T+ K S F I- O X+ E- L- C-- Y1 R+ W- P+ M5 N-- N+
un peu plus : Google Whoring tortue

Messagerie instantanée

    les koms

    m'enfin

    Quant à mes invectives imaginaires, je vous laisse chercher un endroit adéquat pour les ranger. Elles craignent la lumière, si vous voulez une piste.
    Lien associé
    Eolas - « Aimez moi, c'est un ordre. »

    les bons vieux


    archives

    « juillet 2003 »
    lunmarmerjeuvensamdim
    123456
    78910111213
    14151617181920
    21222324252627
    28293031

    XML RSS 2.0 XML RSS 2.0 commentaires A A A i

    liens

    allégeance

    Blog sans chat

    colophon

    Propulsé par pointClairMerci à la caféineDevelopment with EmacsBadges from GTMcKnightFreeListed on BlogSharesGeoURL