Monday, January 11, 2010

How to Wake Up Sleeping Java Thread ?


1) MasterThread:
public class MasterThread implements Runnable {

    private final static int SLEEP_TIME = 5000; //ms
    private final Thread SLAVE;
 
    public MasterThread(Thread aSlave) {
        SLAVE = aSlave;
    }
 
    public void run() {
        System.out.println("MasterThread: run.");
        while (true) {
            System.out.println("MasterThread: call to slave.");
            SLAVE.interrupt(); //wake up
            try {
                Thread.sleep(SLEEP_TIME);
            }
            catch (InterruptedException e) {
                System.err.println("MasterThread: abnormal wake up.");
                return;
            }
        }
    }

}

2) SlaveThread:
public class SlaveThread implements Runnable {

    private final static int SLEEP_TIME = 2000; //ms

    public void run() {
        System.out.println("SlaveThread: run.");
        while (true) {
            System.out.println("SlaveThread: next iteration.");
            try {
                Thread.interrupted(); //clear
                Thread.sleep(SLEEP_TIME);
            }
            catch (InterruptedException e) {
                System.out.println("SlaveThread: wake up.");
            }
        }
    }

}

3) Main:
public class Main {
 
    public static void main(String[] args) {
        Thread slave = new Thread(new SlaveThread());
        Thread master = new Thread(new MasterThread(slave));
        slave.start();
        master.start();
        try {
            slave.join();
            master.join();
        }
        catch (InterruptedException e) {
            System.err.println(e);
        }
        System.out.println("done.");
    }

}




%%

4 comments:

Saro said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
rpa training in bangalore
best rpa training in bangalore
RPA training in bangalore
rpa course in bangalore
rpa training in chennai
rpa online training

Mounika said...

Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
python training Course in chennai
python training in Bangalore
Python training institute in bangalore

Mounika said...

I simply want to give you a huge thumbs up for the great info you have got here on this post.
python course institute in bangalore
python Course in bangalore
python training institute in bangalore

Mounika said...

Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me
python course institute in bangalore
python Course in bangalore
python training institute in bangalore