Solved

About Destroying Threads

Posted on 1998-08-25
4
212 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
[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
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

730 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