import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class Blocker extends Thread
{
JTextField state=new JTextField(32);
Peekers    peekers;
int i;

public Blocker(Container c)
{ c.add(state);
peekers=new Peekers(this,c);
}

public void run()
{ while(true)
{ synchronized (this)
{ i++;
update();
}
try{ sleep(1000);} catch( InterruptedException e) {}
}
}
public synchronized void change()
{ i++;
update();
}

public synchronized int read(){ return i;}
public synchronized void update()
{ state.setText(getClass().getName()+" state:i = " +i );
}
public void stopPeekers()
{ peekers.terminate();
}
public void restartPeekers()
{ peekers.reterminate();
}
}
class Peekers extends Thread
{
JTextField state=new JTextField(32);
Blocker   blocker;
int j=0;
int session;
boolean stop=false;
public Peekers(Blocker b,Container c)
{
c.add(state);
blocker=b;
start();
}
public void terminate()
{
stop=true;
}
public void reterminate()
{
stop=false;
run();
}
//run()方法的问题,当我点击stop后在再点击Restart按钮,程序就死掉了,难道在线程的run()方法不能被多次调用吗?
//当我将while(!stop)循环语句注释调了,点击restart按钮没有问题阿,真的很奇怪.
public synchronized void run()
{ while(!stop)
{
state.setText(" Peeker "+(++session)+" ; value= "+blocker.read());
try{sleep(1000);
}catch(InterruptedException e){ }
}
state.setText("a"+(j++));
System.out.println("true");
}
}public class Block extends JFrame
{
private JButton
         start=new JButton("start"),
         stop=new JButton("stop"),
         restart=new JButton("restart");
boolean started=false;
Blocker b;

Block()
{
b=new Blocker(this.getContentPane())
start.addActionListener(new startL());
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(start);
stop.addActionListener(new stopL());
this.getContentPane().add(stop);
restart.addActionListener(new restartL());
this.getContentPane().add(restart);
}

class startL implements ActionListener {
public void actionPerformed(ActionEvent e)
{
if(!started)   b.start();
}
}
class stopL implements ActionListener {
public void actionPerformed(ActionEvent e)
{ b.stopPeekers();
}
}
class restartL implements ActionListener {
public void actionPerformed(ActionEvent e)
{ b.restartPeekers();
}
}
public static void  main(String[] args)
{
Block bl=new Block();
bl.setSize(350,500);
bl.show();
bl.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});
}
}

解决方案 »

  1.   

    多线程类有是先运行start方法,start方法实际上是调用run方法.我不明白的地方是run()方法能否被多次调用呢?我发现当run方法里面包含有while()之类循环语句的时候,run方法不能被多次调用
    但当run方法没有包含while()循环语句的时候,调用是没有问题的,不知道是什么原因?
    以下是源代码
      

  2.   

    run只能调用一次,run结束则线程结束,这就是为什么在run中用while(true)的原因。
      

  3.   

    这个方法public void reterminate()
    {
    stop=false;
    run();
    }
    显然是不可以的。
      

  4.   

    你可以将
    public synchronized void run()
    { while(!stop)
    {
    state.setText(" Peeker "+(++session)+" ; value= "+blocker.read());
    try{sleep(1000);
    }catch(InterruptedException e){ }
    }
    state.setText("a"+(j++));
    System.out.println("true");
    }
    更改成
    public synchronized void run()
    {              while (true)
                 {
                      try
                      {
                          Thread.sleep(1000);
                      }
                      catch(Exception e){}
    while(!stop)
    {
    state.setText(" Peeker "+(++session)+" ; value= "+blocker.read());
    try{sleep(1000);
    }catch(InterruptedException e){ }
    }
    state.setText("a"+(j++));
    System.out.println("true");
    }
    }
    可能会实现你想要的功能,你只要改变stop的状态就可以了。