?
Solved

Interrupting thread

Posted on 2004-10-15
14
Medium Priority
?
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

770 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