Solved

About Destroying Threads

Posted on 1998-08-25
4
208 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception after setting jdbc session management 2 52
Java array passed to SQL where clause 23 62
diffSum example 4 24
oracle 11g 23 49
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.

910 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

20 Experts available now in Live!

Get 1:1 Help Now