// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class FunkyThread extends Thread {
    private Runnable[] workunits;
    private volatile boolean paused;
    public FunkyThread(Runnable... workunits) {
        this.workunits = workunits;
        paused = false;
    }
    public void run() {
        for(Runnable wu : workunits) {
            while(paused) {
                synchronized(this) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            wu.run();
        }
    }
    public void pause() {
        paused = true;
    }
    public void restart() {
        paused = false;
        synchronized(this) {
            this.notify();
        }
    }
    public static void main(String[] args) {
        FunkyThread t = new FunkyThread(new Runnable() {
                                        public void run() {
                                           System.out.println(System.currentTimeMillis());
                                           try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    },
                                    new Runnable() {
                                        public void run() {
                                           System.out.println(System.currentTimeMillis());
                                           try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
        System.out.println(System.currentTimeMillis());
        t.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.pause();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.restart();
    }
}
