关于线程运行与暂停的控制,请大家看看我的代码,总是不能控制线程的运行和暂停
我想通过一个boolean的stats来控制while,实现控制线程的目的,但好像没有用,另外还有别的方法可以控制吗?//getEvent.java
//show how to get the swing eventimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class getEvent extends JApplet{
private JLabel myLabel = new JLabel("LABEL");
private JButton myButton = new JButton("BUTTON");
private boolean stats = false;
//a inner class for get the buttion event
private ActionListener b1 = new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = ((JButton)e.getSource()).getText();
if(stats == false){
myButton.setText("now the Thread is go");
stats = true;
}else{
stats = false;
myButton.setText("nwo the Thread is stopped");
}
System.out.println(stats);
}
};
//a inner class for Thread
private Thread mt = new Thread(){
private int i = 1;
private String ii;
public void run(){
System.out.println(stats);
while(stats){
ii = String.valueOf(i);
myLabel.setText(ii);
try{
sleep(2000);
}catch(Exception e){}
i++;
}
}
};
public void init(){
//reg the ActionListener to the button
myButton.addActionListener(b1);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(myLabel);
cp.add(myButton);
mt.start();
}
public static void main(String[] args){
JApplet applet = new getEvent();
JFrame frame = new JFrame(applet.getClass().toString());
frame.getContentPane().add(applet);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,300);
applet.init();
applet.start();
frame.setVisible(true);
}
}
我想通过一个boolean的stats来控制while,实现控制线程的目的,但好像没有用,另外还有别的方法可以控制吗?//getEvent.java
//show how to get the swing eventimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class getEvent extends JApplet{
private JLabel myLabel = new JLabel("LABEL");
private JButton myButton = new JButton("BUTTON");
private boolean stats = false;
//a inner class for get the buttion event
private ActionListener b1 = new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = ((JButton)e.getSource()).getText();
if(stats == false){
myButton.setText("now the Thread is go");
stats = true;
}else{
stats = false;
myButton.setText("nwo the Thread is stopped");
}
System.out.println(stats);
}
};
//a inner class for Thread
private Thread mt = new Thread(){
private int i = 1;
private String ii;
public void run(){
System.out.println(stats);
while(stats){
ii = String.valueOf(i);
myLabel.setText(ii);
try{
sleep(2000);
}catch(Exception e){}
i++;
}
}
};
public void init(){
//reg the ActionListener to the button
myButton.addActionListener(b1);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(myLabel);
cp.add(myButton);
mt.start();
}
public static void main(String[] args){
JApplet applet = new getEvent();
JFrame frame = new JFrame(applet.getClass().toString());
frame.getContentPane().add(applet);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,300);
applet.init();
applet.start();
frame.setVisible(true);
}
}
解决方案 »
- com.sun这个包路径下的类,到底能不能用?
- Java中服务器端接收到客户端以十六进制发送过来的数据如何转换为可用字符串???
- <s:iterator 的问题
- 关于javascript的问题,请高手指教!
- 我在做东西的时候遇到几个问题,请大家帮忙解决~~
- 谁能给我解释一下?
- java难道真的没有基本类庫的使用说明吗?
- 唉,受不了了,哪位高人帮我添一段和线程有关的代码吧
- 有没有在武汉学JAVA的和做开发的朋友!?
- 请问Inprise Application Server 4.1是干什么用的?
- 急问:在本机器上jsp里怎么连接别人机器上的数据库?有代码实例最好!!
- 基于Log4j的扩展工具包 qLog, 欢迎对log技术感兴趣的朋友下载使用。
* Created on 2005-8-14
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package rmi;/**
* @author slash
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
//getEvent.java
//show how to get the swing event
//getEvent.java
//show how to get the swing event
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class GetEvent extends JApplet {
private JLabel myLabel = new JLabel("LABEL"); private JButton myButton = new JButton("BUTTON"); private boolean stats = false; //a inner class for get the buttion event
private ActionListener b1 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = ((JButton) e.getSource()).getText();
if (stats == false) {
myButton.setText("now the Thread is go");
stats = true;
} else {
stats = false;
myButton.setText("nwo the Thread is stopped");
}
System.out.println(stats);
}
}; //a inner class for Thread
private Thread mt = new Thread() {
private int i = 1; private String ii; public void run() {
System.out.println(stats);
//多线程的概念:必须能跑起来
while (true) {
ii = String.valueOf(i);
if(stats)
myLabel.setText(ii);
try {
sleep(2000);
} catch (Exception e) { }
i++;
}
}
}; public void init() {
//reg the ActionListener to the button
myButton.addActionListener(b1);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(myLabel);
cp.add(myButton);
mt.start();
} public static void main(String[] args) {
JApplet applet = new GetEvent();
JFrame frame = new JFrame(applet.getClass().toString());
frame.getContentPane().add(applet);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
applet.init();
applet.start();
frame.setVisible(true);
}
}