Solved

Interrupting thread

Posted on 2004-10-15
14
380 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now