[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

About Destroying Threads

Posted on 1998-08-25
4
Medium Priority
?
216 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 150 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.
Suggested Courses
Course of the Month19 days, 20 hours left to enroll

872 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