如何实现线程等待通知?

时间:2014-03-21 20:58:00

标签: java multithreading

这是我写的代码。我尝试了很多方法,但它没有用。请帮助pause()方法有效但continue()没有。

public abstract class Figure implements Runnable {

    public static final Color DEFAULT_COLOR = Color.ORANGE;
    public static final int DEFAULT_X = 30;
    public static final int DEFAULT_Y = 30;
    public static final int DEFAULT_WIDTH = 50;
    public static final int DEFAULT_HEIGHT = 50;
    /**
     *
     */
    private int x;
    private int y;
    private int width;
    private int height;
    private Color color;
    /**
     * speed per OX projection
     */
    private int xS;
    /**
     * speed per OY projection
     */
    private int yS;
    private Thread t;
    private boolean isRunning;
    private boolean isPaused;
    private FigureCanvas panel;

    // START CONSTRUCTORS
    // WE can add more constructor with other parameter group if it is necessary
    protected Figure() {
        this(DEFAULT_X, DEFAULT_Y, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_COLOR);

    }

    protected Figure(int x, int y, int width, int height) {
        this(x, y, width, height, DEFAULT_COLOR);
    }

    protected Figure(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;


    }
// END OF CONSTRUCTORS

    //Start getters and setters block
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        if (width < 0) {
            System.out.println("Incorrect parameter error: width can not be negative ");
            return;
        }
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        if (height < 0) {
            System.out.println("Incorrect parameter error: height can not be negative ");
            return;
        }
        this.height = height;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
//End of getters and setters block

    public abstract boolean isBelong(int x, int y);

    public abstract void draw(Graphics g);

    @Override
    public void run() {
        System.out.println("In RUN method");
        while (isRunning) {
            System.out.println("running");
        }
    }

    public void move(int xS, int yS) {
        x += xS;
        y += yS;
    }

    public void move() {
        xS = 1;
        yS = 1;
        move(xS, yS);
    }

    public void stop() {
        // TODO stop thread
    }

    public void continueRun() {
//      ToDO notify
        synchronized (t) {
            isRunning = true;

            t.notify();
        }
    }

    public void pause() {
        // TODO organize wait
        synchronized (t) {
            try {
                isRunning = false;
                t.wait();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void start() {
//  TODO    start new thread and callits start method
        t = new Thread(this);
        isRunning = true;
        t.start();
    }

    @Override
    public String toString() {
        return "Figure{"
                + "x=" + x
                + ", y=" + y
                + ", width=" + width
                + ", height=" + height
                + ", color=" + color
                + '}';
    }
}

2 个答案:

答案 0 :(得分:0)

您正在等待对象t,所以要唤醒您还需要对对象t进行通知!

如果您尝试执行this之类的操作,则不应忘记在这种情况下通知 b对象中执行。

答案 1 :(得分:0)

我一直在使用Semaphore作为“可暂停的可运行程序”,我觉得它更容易维护。这是我使用的模式:

public class Q22569411 implements Runnable {

private volatile boolean paused;
private volatile boolean stop;
private final Semaphore pauseLock = new Semaphore(0);

@Override
public void run() {

    while (!stop) {
        work();
        if (paused) {
            System.out.println("Pausing work.");
            try { 
                pauseLock.acquire();
            } catch (InterruptedException ie) {
                System.out.println("Pause interrupted.");
            }
        }
    }
    System.out.println("Stopped running");
}

protected void work() {

    System.out.println("Working");
    try {
        Thread.sleep(100);
    } catch (InterruptedException ie) {
        System.out.println("Work interrupted.");
    }
}

public boolean isPaused() {
    return paused;
}

public void setPaused(boolean pause) {

    if (this.paused == pause) {
        return;
    }
    if (this.paused) {
        paused = false;
        pauseLock.release();
    } else {
        // Remove previous release in case this method is called repeatedly
        pauseLock.tryAcquire();
        paused = true;
    }
}

public void stopRunning() {

    stop = true;
    pauseLock.release();
}

}