Solved

About Destroying Threads

Posted on 1998-08-25
4
210 Views
Last Modified: 2010-03-30
Hi!
i'm using multiple threads in my program. In my program whenever any thread stops then new thread is created.
Problem is that whenever any thread stops it goes into suspend state. When i'm calling Thread.destroy() function
it gives no error at compile time, but at run time it gives error
"NoSuchMethodError". How can i destroy threads?
if possible give some sample codes also.

I'm using VisualCafe 1.1.3 on Windows-95.

Thanx.

pappu
0
Comment
Question by:pappu070298
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
diakov earned 50 total points
ID: 1222556
When you create the thread, you get a reference to it. Keep it in a vector or something. destroy() stops without any cleanup (leaves locked monitors, etc.) Then call stop().
Actually there are two cases. When the thread finishesh work and when you want to stop it from another thread, by using its referens.
- finishes. Upon exit from the run() you can call a parent method to remove the thread reference from the vector (pool of threads you support)
- external stop:
  . if you whant it immediatelly stopped, find it's reference and call stop(). Then assign the refernce null.
  . if not, but whant its token of work to finish, just set its reference to null. The Garbage Collect will do the rest later.

t = new CustomThread("asda");
t.start();
.
//do something
.
t = null; //or t.stop(); even t.destroy();

Hope this helped.


0
 

Author Comment

by:pappu070298
ID: 1222557
Hi Diakov!
I've tried the stop() method and null but it dosen't work.It stops the working of thread and put it in suspend state dosen't destroy
it. When i use destroy() method it dosen't give any error at compile time but gives the error at run time.
0
 
LVL 8

Expert Comment

by:diakov
ID: 1222558
Hi,
Call java.lang.System.gc(); to clean up the memory. You can also try the sequence: java.lang.System.runFinalization(); and then the gc(); In general, you do not need to do this, since JVM will do ot anyway when needed.

I'm sure the threads are NOT in suspended mode, since suspended mode means they can resume, which after stop() is not possible.

Cheers.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1222559
pappu, using the stop may address your problem in the short term, but actually the most reliable solution is one that you may find to be strange. The preferable way to stop a thread would be to notify this thread to stop (using some boolean var), and that the thread would sample that notification at a reasonable time resolution. When the thread is asked to terminate through this notification, it should end it run method. This may sound not efficient and not intuitive but the fact is that the stop method function differently on different OSs and is somewhat unreliable. Checking the end condition yourself will make your program more predictable and more debugable. Sun have deprecated the stop and the suspend methods and you may expect it not to be supported somewhere in the future (look into jdk1.2 docs).

Regards, G.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

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