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);}
});
}
}
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);}
});
}
}
解决方案 »
- 问一下java中多态的问题?
- JAVA大牛们 请教一个问题 小伟谢谢你们了
- 一个关与equals()的小问题
- 如何学习JAVA(推荐)
- 有没有谁看过《java核心技术》VolumeII ?给点意见吧,我好矛盾~~~(来者有分!)
- 请问如何在用APPLET在网页上打开显示CAD图纸?急!!
- 谁帮助我解释一下事件处理中的掩码概念?我刚学java详细一些,谢谢!
- 辽大计算机专业怎么样?
- 请问这样在技术上是否可行?(我自己写一个程序来监听我的系统上的端口,有些端口已经被别的应用使用,我能够使用什么方法再去监听这个端
- 自学请教!!请问如何将txttfiled里面的字符转化成double类型数组
- class的访问控制???
- jbuilder的 快捷键
但当run方法没有包含while()循环语句的时候,调用是没有问题的,不知道是什么原因?
以下是源代码
{
stop=false;
run();
}
显然是不可以的。
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的状态就可以了。