0
public class test {

    public static void main(String[] args) {
        frame tmp;
        tmp=new frame();
        ExecutorService executor;
        executor=Executors.newCachedThreadPool();
        executor.execute(tmp);
        executor.shutdown();
    }
}





  public class frame extends JFrame implements Runnable{

        int lis;
       public frame()
      {
        setVisible(true);
        lis=1;
        addWindowListener(
          new WindowAdapter() 
          {              
              public void windowClosing(WindowEvent event)
              {
                  lis=0;                 
              }
          }
        );
       }
       @Override
       public void run() {
        while(lis==1)
        {
        }
        JOptionPane.showMessageDialog(null,"close");
      }    
}

the sequence of steps which i thought will happen after running class test

1) jframe gets displayed and runs in a seperate thread

2) after closing the jframe,the thread completes its execution and executor shut's down

3)class test terminate's since all the statements are executed.

but when i run the class test

after closing the jframe class test is not terminating

any explanation or reference would be helpful. thank you

EDIT:suppose i created two frames.i want the program to exit only after all the frames are closed not just one jframe.

Avijit
  • 3,834
  • 4
  • 33
  • 45
  • got the answer from this thread http://stackoverflow.com/questions/13360430/jframe-dispose-vs-system-exit. adding dispose() will solve the problem – user1802910 Jan 25 '14 at 05:33

2 Answers2

0

You are referencing to lis variable from different threads. According to Java Memory Model, it should be declared as volatile.

Alexander Tokarev
  • 2,743
  • 2
  • 20
  • 21
0

You need to set the default close operation. In your frame constructor, add:

setDefaultCloseOperation(EXIT_ON_CLOSE);
Josh M
  • 11,611
  • 7
  • 39
  • 49
  • suppose i added this statement to constructor and if i create two frames,and i closed one frame,then the entire program is exiting.i want the program to exit only after all the frames are closed. – user1802910 Jan 25 '14 at 05:05
  • You can dispose the one you want to close. – Josh M Jan 25 '14 at 05:14