在 AsyncTask 中使用等待

发布于 2024-11-09 18:20:06 字数 714 浏览 4 评论 0原文

AsyncTask 中使用 wait 时,出现 ERROR/AndroidRuntime(24230): Caused by: java.lang.IllegalMonitorStateException: object not lock by thread before wait ()

是否可以使用 Asynctask 只是为了等待?如何?

谢谢

class WaitSplash extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        try {
            wait(MIN_SPLASH_DURATION);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }       

    protected void onPostExecute() {
        waitSplashFinished = true;
        finished();
    }
}  

When using a wait in an AsyncTask, I get ERROR/AndroidRuntime(24230): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()

Is it possible to use an Asynctask just for waiting? How?

Thanks

class WaitSplash extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        try {
            wait(MIN_SPLASH_DURATION);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }       

    protected void onPostExecute() {
        waitSplashFinished = true;
        finished();
    }
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

何以心动 2024-11-16 18:20:06

使用 Thread.sleep() 而不是等待()

Use Thread.sleep() instead of wait().

楠木可依 2024-11-16 18:20:06

您可以使用 Thread.sleep 方法

    try {
        Thread.sleep(1000);         
    } catch (InterruptedException e) {
       e.printStackTrace();
    }

You can use Thread.sleep method

    try {
        Thread.sleep(1000);         
    } catch (InterruptedException e) {
       e.printStackTrace();
    }
落在眉间の轻吻 2024-11-16 18:20:06
@Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                Thread.currentThread();
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;

        }
@Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                Thread.currentThread();
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;

        }
审判长 2024-11-16 18:20:06

如果您只想将方法的执行推迟一段时间,一个不错的选择是 Handler.postDelayed()< /a>

定义处理程序和可运行...

private Handler handler = new Handler();
private Runnable runnable = new Runnable() {        
    finished();
};

并延迟执行...

handler.postDelayed(runnable, MIN_SPLASH_DURATION);

If you're looking to just postpone execution of a method for a set amount of time, a good option is Handler.postDelayed()

define the handler and runnable...

private Handler handler = new Handler();
private Runnable runnable = new Runnable() {        
    finished();
};

and execute with delay...

handler.postDelayed(runnable, MIN_SPLASH_DURATION);
强者自强 2024-11-16 18:20:06

在触摸事件上使用线程

public class SplashActivity extends Activity{

int splashTime = 5000;
private Thread splashThread;
private Context mContext;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.mContext = this;
    setContentView(R.layout.splash_layout);
    splashThread = new Thread(){
        public void run() {
            try{
                synchronized (this) {
                    wait(splashTime);
                }
            }catch(InterruptedException ex){
                ex.printStackTrace();
            }finally{
                Intent i = new Intent(mContext,LocationDemo.class);
                startActivity(i);
                stop();
            }
        }
    };

    splashThread.start();
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        synchronized (splashThread) {
            splashThread.notifyAll();
        }
    }
    return true;
}

,线程得到通知..可以根据您的需要进行更改。

Use threads for this

public class SplashActivity extends Activity{

int splashTime = 5000;
private Thread splashThread;
private Context mContext;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.mContext = this;
    setContentView(R.layout.splash_layout);
    splashThread = new Thread(){
        public void run() {
            try{
                synchronized (this) {
                    wait(splashTime);
                }
            }catch(InterruptedException ex){
                ex.printStackTrace();
            }finally{
                Intent i = new Intent(mContext,LocationDemo.class);
                startActivity(i);
                stop();
            }
        }
    };

    splashThread.start();
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        synchronized (splashThread) {
            splashThread.notifyAll();
        }
    }
    return true;
}

on touch event, thread get notified.. can change according to your need.

且行且努力 2024-11-16 18:20:06

您可以通过这种方式使用 asynctask 和 wait();

public class yourAsynctask extends AsyncTask<Void, Void, Void> {
    public boolean inWait;
    public boolean stopWork; 

    @Override
    protected void onPreExecute() {
        inWait = false;
        stopWork = false;
    }

    @Override
    protected Void doInBackground(Void... params) {
        synchronized (this) {
            while(true) {
                if(stopWork) return null;
                if(youHaveWork) {
                    //make some
                } else {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }

    public void mynotify() {
        synchronized (this) {
            if(inWait) {
                notify();
                inWait = false;
            }
        }
    }

    public void setStopWork() {
        synchronized (this) {
            stopWork = false;
            if(inWait) {
                notify();
                inWait = false;
            }
        }
    }
}

You have this way to work with asyntask and wait();

public class yourAsynctask extends AsyncTask<Void, Void, Void> {
    public boolean inWait;
    public boolean stopWork; 

    @Override
    protected void onPreExecute() {
        inWait = false;
        stopWork = false;
    }

    @Override
    protected Void doInBackground(Void... params) {
        synchronized (this) {
            while(true) {
                if(stopWork) return null;
                if(youHaveWork) {
                    //make some
                } else {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }

    public void mynotify() {
        synchronized (this) {
            if(inWait) {
                notify();
                inWait = false;
            }
        }
    }

    public void setStopWork() {
        synchronized (this) {
            stopWork = false;
            if(inWait) {
                notify();
                inWait = false;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文