Solved

Interrupting thread

Posted on 2004-10-15
14
382 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Archiskulkarni
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12317360
you need to call wait from a synchronised block of code.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12317362
You have to call that from a synchronized block
0
 
LVL 35

Expert Comment

by:girionis
ID: 12317363
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 35

Expert Comment

by:girionis
ID: 12317371
0
 

Author Comment

by:Archiskulkarni
ID: 12317550
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
 
LVL 35

Expert Comment

by:girionis
ID: 12317564
Make sure you have synchronized on an object that you hold a lock.
0
 

Author Comment

by:Archiskulkarni
ID: 12317642
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
 

Author Comment

by:Archiskulkarni
ID: 12317658
// 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
 
LVL 35

Expert Comment

by:girionis
ID: 12317816
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
 

Author Comment

by:Archiskulkarni
ID: 12318033
>myObject.notify();
But i don't have that object.. bcoz i create tht object using
new constructor() only and don't store it.
0
 
LVL 35

Accepted Solution

by:
girionis earned 50 total points
ID: 12318160
Store it somewhere and try again. You need to call notify on that object.
0
 

Author Comment

by:Archiskulkarni
ID: 12318677
Hi,

Thanx to all ppl who replied me and provided me with timely help..
0
 
LVL 35

Expert Comment

by:girionis
ID: 12318767
:)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 56
nextBoolean(double p) for Random class 3 40
hibernate example issues from command prompt 10 42
Desingning Refactoring existing code 2 24
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question