Asynctask等待函数导致错误

时间:2013-04-02 17:50:19

标签: java android android-asynctask udp

我试图运行此代码

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

为什么会发生这种情况?

帆布

2 个答案:

答案 0 :(得分:1)

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

Thread.sleep(25); // 25是毫无休眠的时间

代替。这将使thread睡眠(显然)在您继续

之前声明的时间

Thread

Wait

答案 1 :(得分:0)

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