java.lang.IllegalMonitorStateException: object not locked by thread before wait()

Hi,
I have the following piece of code :
_homeListView.post(new Runnable() {
                @Override
                public void run() {
                    _homeListView.setSelection(1);
                        try {
                            wait(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    _alphabetIndexView.highlight('*', 'Z');
                }
            });

Open in new window


This is causing the following exception :
06-04 14:05:13.600  31989-31989/co.riva.vader E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: co.riva.vader, PID: 31989
    java.lang.IllegalMonitorStateException: object not locked by thread before wait()
            at java.lang.Object.wait(Native Method)
            at java.lang.Object.wait(Object.java:422)
            at co.riva.vader.HomeActivity$7.run(HomeActivity.java:243)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Why is this Exception happening ?
I just want to wait for 1second before execution of next line. What can i do to achieve it?

Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
You have to obtain the lock on an object first - you can't just call wait().
krakatoaCommented:
							
	class Waiter implements Runnable{						
					 		
		 					
		static Object belwether = new Object();
						
							
							public static void main(String[] args){
							
							Waiter w1 = new Waiter();
							Waiter w2 = new Waiter();
							
							Thread t1 = new Thread(w1);
							t1.setName("t1");
							t1.start();
							
							try{Thread.sleep(1000);}catch(InterruptedException ix){}
							
							Thread t2 = new Thread(w2);
							t2.setName("t2");
							t2.start();
							
							}
							
							public void run(){
							
							synchronized(belwether){
							
								if(Thread.currentThread().getName().equals("t1")){
								
										System.out.println("This is thread "+Thread.currentThread().getName()+" speaking");
										
										synchronized (belwether) {
										try {
										System.out.println("Thread "+Thread.currentThread().getName()+" is about to go into wait state");
										//belwether.wait(1000);
										belwether.wait();
										System.out.println("Thread "+Thread.currentThread().getName()+" is exiting wait state - object lock released.");
										} catch (InterruptedException exexe) {}
										}
										
								}
								
								else{
										
										System.out.println("This is thread "+Thread.currentThread().getName()+" speaking");
										
										synchronized(belwether){
										System.out.println("Thread "+Thread.currentThread().getName()+" is about to wake up all waiting threads.");
									
										try{Thread.sleep(10000);}catch (InterruptedException iex){}
										belwether.notifyAll();
									
									
										}
								}
							}
															
							}
				}

Open in new window

... and you can either do :
belwether.wait(1000);

or

belwether.wait();

But it might be more appropriate just to call Thread.sleep() in you code, if the other thread only needs 1000ms to do something else.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I just want to wait for 1second before execution of next line.
So the "wait" method, even though it sounds like what you want, it isn't!!

What can i do to achieve it?
As krakatoa, touched on, the way to do this in Java is with...      Thread.sleep(1000);

HOWEVER, I don't think this will do what I am assuming you want. Because this code will run on the main UI thread, then your whole interface will become unresponsive for the second that you are waiting. That might not sound like a long time, but it is best to do this properly. Try something like this...

_homeListView.post(new Runnable() {
                @Override
                public void run() {
                    _homeListView.setSelection(1);
                    _homeListView.postDelayed(new Runnable() {
                           @Override
                           public void run() {
                                 _alphabetIndexView.highlight('*', 'Z');
                           }
                      }, 1000);
                }
            });

Open in new window


Basically, it is still posting the first action to run on the UI thread, but instead of just sleeping for the time period, it then posts the second action to run 1000ms (1s) later, leaving the UI to do other things, etc.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
I admit to only coming in on this one as I saw the question tagged for Java - didn't clock the Android connection properly, sorry guys. Although it's really an EDT issue, as mccarl says.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.