next up previous
Next: Sincronizzazione nell'accesso alla lista Up: Note e Commenti Previous: Note e Commenti


La classe MessageBoxWithThreadLIL

Nella piattaforma Java 2 i metodi stop, suspend e resume sono stati dichiarati deprecati. Il seguente schema della classe MessageBoxWithThreadLIL è ispirata a quanto mostrato in [2, pagina 47 vecchia edizione, pagina 67 nuova edizione] e presenta una soluzione per permettere ad un thread di interrompere un altro thread senza ricorso al metodo stop.

public class MessageBoxWithLIL extends MessageBox implements Runnable {

    private boolean stopRequested;
    private Thread thisThread;
    private ObjectOutput out;

    public MessageBoxWithLIL(User user, ObjectOutput out) {
        super(user);
        this.out = out;
        stopRequested = false;
        thisThread = new Thread(this);
        thisThread.setDaemon(true);
        thisThread.start();
    }

    public void requestedStop() {
        stopRequested = true;
        thisThread.interrupt();
    }

    public void run() {
        while (!stopRequested)
            try {
               // gestione del ciclo LIL 
            }
            catch (InterruptedException err) {
                if (stopRequested)
                    return;
            }
    }
}



Baldoni Matteo 2003-05-08