Solved

About Destroying Threads

Posted on 1998-08-25
4
207 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

11 Experts available now in Live!

Get 1:1 Help Now