簡體   English   中英

如何實現線程等待通知?

[英]How do I implement thread wait notify?

這是我寫的代碼。 我嘗試了很多方法,但是沒有用。 請幫助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
                + '}';
    }
}

您正在等待對象t ,因此要喚醒您也需要通知對象t

如果您嘗試執行此類操作 ,則不要忘記在這種情況下,通知是 b對象中執行的。

我一直在對“可暫停的可運行對象”使用信號量 ,我發現它更易於維護。 這是我使用的模式:

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();
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM