Interrupting thread

Hi,
i am running a long task in a separate thread(using the SwingWorker) class.. When user clicks on the abort i want to call wait stop that thread temporarily.. and then depending on user's choice, i have to either stop the thread completely or resume it.. How do i achieve this..? I m calling wait on the thread but it's giving me IllegalMonitorStateException..

Anticipating help,
- A
ArchiskulkarniAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
girionisConnect With a Mentor Commented:
Store it somewhere and try again. You need to call notify on that object.
0
 
objectsCommented:
you need to call wait from a synchronised block of code.
0
 
CEHJCommented:
You have to call that from a synchronized block
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
girionisCommented:
IF you want to stop it completely then set a flag in the while loop of the run method to true/false (depends on your design) and the thread will exit. If you want your thread to continue then do it the wait/notify way: http://java.sun.com/docs/books/tutorial/essential/threads/waitAndNotify.html
0
 
girionisCommented:
0
 
ArchiskulkarniAuthor Commented:
Hi,
i tried the setting flags kind of thing.. and it's correctly stopping the thread.. but not resuming it..
i think it's because of the same exception IllegalMonitorstate..
when we specify synchronized(obj)..  which object do we need to pass in place of obj?

p.s. Thanx Venabili for ur kind gesture.
0
 
girionisCommented:
Make sure you have synchronized on an object that you hold a lock.
0
 
ArchiskulkarniAuthor Commented:
ok,
here is how i m doin it

// in actual task
while(flag)
{

         wait();
}


// in ui thread
on action event ()
{
            flag = true;
}

// after user tries
0
 
ArchiskulkarniAuthor Commented:
// when the thread has to be run gain
{
      flag = false;
       notifyAll();
}


now, do i need to call resume on the thread in the catch block where i catch the InterruptedException..
0
 
girionisCommented:
Assuming that the relevant blocks of code are synchronized you need to call notify on the object that waits. So if you call this

while(flag)
{

         wait();
}

on an object, lets say, "myObject" then you need to call notify on that object:

myObject.notify();
0
 
ArchiskulkarniAuthor Commented:
>myObject.notify();
But i don't have that object.. bcoz i create tht object using
new constructor() only and don't store it.
0
 
ArchiskulkarniAuthor Commented:
Hi,

Thanx to all ppl who replied me and provided me with timely help..
0
 
girionisCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.