Solved

Interrupting thread

Posted on 2004-10-15
14
384 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
Industry Leaders: 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 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 73
JavaFX TableView not displaying correctly 3 103
Convert from a json string array to a Java object 3 63
swing controls 2 21
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

740 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