你的flag在第一次
  MyThread mt=new MyThread(flag);
传出,然后再改时是不能传出去的.
试试下法:
  while(TestThread.flag)...

解决方案 »

  1.   

    帮你修改了一下,可以运行import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class TestThread extends JFrame
    {
    static boolean flag;
    JButton jb=new JButton("click"); TestThread()
    {
    Container contentPane=getContentPane();
    contentPane.add(jb);
    jb.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    flag=false;
    }
    });

    }
    public static void main(String[] args)
    {
    flag=true;
    TestThread t=new TestThread();
    t.setBounds(100,100,200,200);
    t.setVisible(true);
    t.setDefaultCloseOperation(EXIT_ON_CLOSE);
    MyThread mt=t.new MyThread();
    mt.start();
    } class MyThread extends Thread
    {
    int i=0;
    MyThread()
    {
    }
    public void run()
    {
    while(flag)
    {
    System.out.println("flag="+flag+" "+i++);
    if(i>3)i=0;
    try{
    Thread.sleep(200);
    synchronized(this){
    while(!flag){
    wait();
    }
    }
    }catch(Exception e){}
    }
    }
    }
    }