0

我试图运行这段代码

protected String doInBackground(String... params) {
    InetAddress client = null;
    boolean run = true;
    String data = "";
    DatagramPacket packet = null;
    while( run )
    {
        if( data.equalsIgnoreCase( "" ) )
        {

        }

        //Send some data
        if( data.equalsIgnoreCase( "connect" ) )
        {

        }






        //Log.d(TAG, "Data received was :" + data);

        try 
        {
            this.wait( 25 );
        } 
        catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            Log.d(TAG, "Error with trying to sleep");
            e.printStackTrace();
        }
    }
    Log.d(TAG, "Error with while run value");
    return "finished";
}

一旦它到达 this.wait(25); 我收到这个错误

04-02 18:49:25.070: D/gameObject(9065): New game object
04-02 18:49:25.085: W/dalvikvm(9065): threadid=12: thread exiting with uncaught exception (group=0x40015560)
04-02 18:49:25.089: E/AndroidRuntime(9065): FATAL EXCEPTION: AsyncTask #2
04-02 18:49:25.089: E/AndroidRuntime(9065): java.lang.RuntimeException: An error occured while executing doInBackground()
04-02 18:49:25.089: E/AndroidRuntime(9065):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.lang.Thread.run(Thread.java:1019)
04-02 18:49:25.089: E/AndroidRuntime(9065): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.lang.Object.wait(Native Method)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.lang.Object.wait(Object.java:395)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at com.example.gelorph_v1.gameServer.doInBackground(gameServer.java:116)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at com.example.gelorph_v1.gameServer.doInBackground(gameServer.java:1)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-02 18:49:25.089: E/AndroidRuntime(9065):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-02 18:49:25.089: E/AndroidRuntime(9065):     ... 4 more

为什么会发生这种情况?

帆布

4

2 回答 2

1

我不认为wait这是你想要的。thread这告诉它等待另一个通过调用来通知它notify()。尝试

Thread.sleep(25); // 25 是以毫秒为单位的睡眠时间

反而。这将使thread睡眠(显然)达到您在继续之前声明的时间

线

等待

于 2013-04-02T17:54:06.403 回答
0

您确定要等待而不是睡觉吗?如果您希望它始终暂停 25 毫秒,请使用睡眠。如果您确实想使用等待,则需要在您正在等待的对象的同步块中这样做。

于 2013-04-02T17:54:21.250 回答