Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

destroy() question  RSS feed

 
Patrick Lentz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
someone asked me a question the other day and I got curious myself.
The problem he has is this: he's running Netscape to try out an applet he made, but when he closes Netscape he found out that the system isn't giving him back all the resources. He has to kill the process with Ctrl-Alt-Del (he's running 95).
Looks like a thread is still active. Now this should be handled with stop() and destroy() I think.
Could anyone give me an example in the form of a small very simple applet where explicitly destroy() is used to make sure all threads are killed (sounds kinda violent eh?).
I appreciate it!
Patrick
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a thread t, whose run method has a loop
while(true)
{
.............
}
then the thread will keep running.A now depreciated way is to put t.stop() in the stop or destroy method of the applet.But since stop() method of the thread is now depreciated, instead you can have the while loop in the run method as
while(active)
{
/.....................
}
where active is a boolean type instance variable of the applet initially set to true.In stop()or destroy() of applet method put active=false;
Another way of which I am not sure is to do something like this. In the run():
while(!this.isInterrupted())
{
.............
}
and in the applet destroy() method put t.interrupt();
hope this helps
regards
Tanveer
 
Patrick Lentz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Tanveer,
I do get your drift in case you have a thread defined. However, how would you explicitly stop an applet like the simple one I added below? You can hardly just put "while (active)"
around it I think.
I hope you can give me some more comment and I appreciate you already doing so!
Patrick

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MoveMe extends JApplet
{
Container theContainer;
JButton theButton;
public void init()
{
theContainer = this.getContentPane();
theContainer.setLayout(null);
theButton = new JButton("Move Me With the Mouse");
theContainer.addMouseMotionListener(new Chaser());
theContainer.add(theButton);
theButton.setBounds(0, 0, 150, 35);
}
public class Chaser extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
theButton.setSize( 150 + me.getX(), 35 + me.getY() );
theButton.setLocation(me.getX()+3, me.getY()+3);
}
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!