Reputation: 253
I have an frameAnimation, i only want it to start for like 3 second or so...therefore i added in a thread to wait for 3 second but then i meet an exception.
im havent done any work with thread before so this is pretty much my first time using it, can anyone help?
this is my code:
public void setTimerImage4Bot() throws Exception {
Thread thread = new Thread();
AnimationDrawable frameAnimation = (AnimationDrawable)bot.getDrawable();
frameAnimation.setCallback(bot);
frameAnimation.setVisible(true, true);
frameAnimation.start();
Log.i("BaoAnh","START");
thread.wait(3000);
frameAnimation.stop();
Log.i("BaoAnh","STOP");
generateRandom();
}
this is the error:
05-21 10:11:55.633: WARN/System.err(740): java.lang.IllegalMonitorStateException: object not locked by thread before wait()
05-21 10:11:55.633: WARN/System.err(740): at java.lang.Object.wait(Native Method)
05-21 10:11:55.633: WARN/System.err(740): at java.lang.Object.wait(Object.java:326)
05-21 10:11:55.633: WARN/System.err(740): at com.example.PaperCissorsRock.game.setTimerImage4Bot(game.java:63)
05-21 10:11:55.633: WARN/System.err(740): at com.example.PaperCissorsRock.game$1.onClick(game.java:44)
05-21 10:11:55.633: WARN/System.err(740): at android.view.View.performClick(View.java:2408)
05-21 10:11:55.633: WARN/System.err(740): at android.view.View$PerformClick.run(View.java:8816)
05-21 10:11:55.633: WARN/System.err(740): at android.os.Handler.handleCallback(Handler.java:587)
05-21 10:11:55.633: WARN/System.err(740): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 10:11:55.633: WARN/System.err(740): at android.os.Looper.loop(Looper.java:123)
05-21 10:11:55.633: WARN/System.err(740): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-21 10:11:55.643: WARN/System.err(740): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 10:11:55.643: WARN/System.err(740): at java.lang.reflect.Method.invoke(Method.java:521)
05-21 10:11:55.643: WARN/System.err(740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-21 10:11:55.643: WARN/System.err(740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-21 10:11:55.643: WARN/System.err(740): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 518
Reputation: 54672
you can use Thread.sleep(3000);
But You better use Handler.postDelayed method also. By using postDelayed you can perform a runnable task after an interval
For example
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
do tasks which will be done after 3 seconds
}
},3000);
Upvotes: 2
Reputation: 881323
The wait
method is for object synchronisation. If you just want to sleep for a bit, use sleep
.
Object
public final void wait (long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires. This method can only be invoked by a thread which owns this object's monitor; see notify() on how a thread can become the owner of a monitor.
You're getting the error because you're not the owner of the monitor, nor should you be since waiting on the object is not what you want to do.
And keep in mind that sleep
is a static function, you don't need to instantiate a thread object to use it. Just use:
Thread.sleep (3000);
Upvotes: 0