English 中文(简体)
正确使用牙齿动物游戏
原标题:Correct use of wait()/notify() for a Tetris game


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.

我想的是:

  • 这些形状按固定速度定下(例如,我想要等到nm>milliseconds,每次打下形状的y)。

  • 参与者可以随时放弃体质,然后必须立即中断等待n<>n><>>m>milliseconds的时间,并只为下一个形状重新开始。

  • 当形状下降或形形形形形形形形形形形色时,游戏在形成另一种形态之前等待m><>m>m>m> milliseconds

  • 该系统必须能够随时停下read。

我正在做的是:

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

特别是,<条码>同步(这一条){等待(n);}看不见,因为如果我正确理解,这将在<条码>上锁,立即予以公布。

But wait() requires to be used in a synchronized(this) block (why?) and I cannot either synchronize 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( methods is used to make the present operation thread to等该物体援引wait( 援引notification( (in this case). The synchized(this) part required to ensure only one thread at that time accessthis/code>.

您可以协调整个操作方法,因为操作方法来自父母(Thread)阶层,而家长则在宣言中使用私刑。

我不知道如何解决你的其他问题,因为现在我看不到你的方案是如何运作的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...