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

Help closing a program  RSS feed

 
Shai Elnekave
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem closing a program. When I run it debug mode I c that there is a thread left in memory and maybe this is the reason that the program doesn't close.
I have 3 files.
package frametest;
import javax.swing.WindowConstants;
public class FrameTestApp
{
public Frame1 frame;
public MyThread myThread;
public FrameTestApp()
{
frame = new Frame1( this);
myThread = new MyThread( this);
}
public void terminate()
{
myThread.interrupt();
myThread.terminate();
frame.dispose();
}

public static void main(String[] args)
{
FrameTestApp frameTestApp1 = new FrameTestApp();
frameTestApp1.frame.show();
System.out.println( "after show");
frameTestApp1.myThread.start();
try
{
frameTestApp1.myThread.join();
}
catch (InterruptedException e)
{
System.out.println( "main InterruptedException");
}
System.out.println( "after join");
}
}
--------------------------------------------
package frametest;
import java.awt.*;
import javax.swing.JFrame;
import java.awt.event.*;
public class Frame1 extends JFrame
{
private FrameTestApp owner;
public Frame1( FrameTestApp owner)
{
super();
this.owner = owner;
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
this_windowClosing(e);
}
});
}
void this_windowClosing(WindowEvent e)
{
try
{
owner.terminate();
}
catch ( Throwable ex)
{
ex.printStackTrace();
}
}
}
---------------------------------------
package frametest;
public class MyThread extends Thread
{
private boolean terminate;
private FrameTestApp owner;
public MyThread( FrameTestApp owner)
{
super();
this.owner = owner;
}
public void run()
{
while (!terminate)
{
System.out.println( "myThread.run");
try
{
Thread.sleep( 10*1000);
}
catch( InterruptedException e)
{
System.out.println( "MyThread.run InterruptedException");
}
}
}
public void terminate()
{
terminate = true;
}
}
---------------------------------
This is the printout:
after show
myThread.run
MyThread.run InterruptedException
after join
but then the program hangs and doesn't close???!!!
Help ;-)
Shai
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Shai -
Call System.exit() at the end of your main(); that should take care of it.
Whenever you invoke a visual component, such as java.awt.Frame, the run-time engine loads an AWTThread that manages event screen drawing. This is a daemon-thread that runs as long as no one hangs up on it.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!