开发者

Correct use of wait()/notify() for a Tetris game

开发者 https://www.devze.com 2023-02-23 01:46 出处:网络
I’m writing a Tetris-like game for Android and I’m trying to implement the “real-time part”. I have something which seems to work, but I want to be sure that my implementation is correct.

I’m writing a Tetris-like game for Android and I’m trying to implement the “real-time part”. I have something which seems to work, but I want to be sure that my implementation is correct.

What I want is:

  • The shapes are going down at a fixed rate (say that I want to wait n milliseconds each time the y of the shape is decremented)

  • The player can drop the shape at any time and the timer waiting for the n milliseconds must then be immediately interrupted and start again only for the next shape

  • When the shape is droped or when the shape cannot go down anymore, the game waits m milliseconds before creating another shape

  • The system have to be able to stop the thread at any time

What I am doing is the following (the system can stop the thread with interrupt()):

class TetrisThread extends Thread {
    private int n = 3000; // for testing purposes, in the real game n will be smaller ;)
    private int m = 1000;

    @Override
    public void run() {
        doDraw();
        while(!interrupted())
        {
            try {
                synchronized (this) {
                    wait(n);
                }
                doPhysics();
                doDraw();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    // This method is the one which will drop the shape, it is called from another thread
    synchronized public boolean onTouch([…]) {
        […]
        // The shape has to be dropped
        dropShape();
        notify();
        […]
    }

    private void doPhysics() throws InterruptedException {
        […]
        // The shape cannot go down or has been dropped
        sleep(m);
        createNewShape();
        […]
    }
}

In particular, the part synchronized(this) { wait(n); } looks funny because if I understand correctly this will take a lock on this and release it immediately.

But wait() requires to be used in a synchronized(this) block (why?) and I cannot either sy开发者_如何学JAVAnchronize the whole run() method, because then if I try to drop three times the shape during the sleep(m) call, then the three next shapes will be automatically dropped (which is not what I want).

Does this seem correct to you?

Do you have any correction, advice, or remark?

Thank you :-)


The wait() method is used to make the current running thread to wait the object invoking wait() invoke notify() (in this case this). The synchronized(this) part needed to make sure only one thread at that time accessing this.

You can't synchronize the whole run() method, because the run() is from the parent (Thread) class and the parent didn't use synchonized in the declaration.

I don't know how to solve your other problem because I don't get how your program works right now.

0

精彩评论

暂无评论...
验证码 换一张
取 消