如何结束线程啊? jdk1.3以后就不提倡使用强行结束线程的方法,你应该尽量使线程正常退出run方法,如果想调整他们的顺序,可以通过调整线程的优先级来控制 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用RUN,设置个FLAG标志,当检测到FLAG为FALSE的时候就让程序退出 其实正常的情况下,要这样去实现import javax.swing.*;import java.awt.event.*;public class Aaa extends JFrame implements ActionListener,Runnable { Thread one; boolean flag=false; JButton jb1,jb2; JPanel jp; public Aaa() { jb1=new JButton("stop"); jb2=new JButton("start"); jp=new JPanel(); jb1.addActionListener(this); jb2.addActionListener(this); jp.add(jb1); jp.add(jb2); getContentPane().add(jp); setSize(300,300); setVisible(true); one=new Thread(this); one.start(); } public void actionPerformed(ActionEvent evt) { Object obj=evt.getSource(); if(obj==jb1) { flag=true; } else if(obj==jb2) { flag=false; Thread th=new Thread(this); th.start(); } } public void run() { while(true) { System.out.print("1"); if(flag==true) break; try{Thread.currentThread().sleep(500);} catch(InterruptedException ie){} } } public static void main(String args[]) { new Aaa(); }} 其实,线程的run方法运行结束,就是线程的自然终结。别的就没有什么好讲的了,呵呵。 java如何实现随机生成迷宫地图 求思路 关于javabean 一个棘手sql问题 项目的构建,编译,发布 JAVA群交流 散分 求大神 一个小游戏 运行一次后第二次就出错了! 从数据库里面取出来的是0x******(16进制码)怎么办 在java applet中如何操作服务器上的数据库,而客户单不用配置任何环境? 怎样学JAVA ConsoleReader.java下载下来应该放在哪里??求救!! 请问:我想作一个简单的线程池,但是我不能确定一个事情,请对线程有研究朋友看看
import javax.swing.*;
import java.awt.event.*;
public class Aaa extends JFrame implements ActionListener,Runnable
{
Thread one;
boolean flag=false;
JButton jb1,jb2;
JPanel jp;
public Aaa()
{
jb1=new JButton("stop");
jb2=new JButton("start");
jp=new JPanel();
jb1.addActionListener(this);
jb2.addActionListener(this);
jp.add(jb1);
jp.add(jb2);
getContentPane().add(jp);
setSize(300,300);
setVisible(true);
one=new Thread(this);
one.start();
}
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==jb1)
{
flag=true;
}
else if(obj==jb2)
{
flag=false;
Thread th=new Thread(this);
th.start();
}
}
public void run()
{
while(true)
{
System.out.print("1");
if(flag==true)
break;
try{Thread.currentThread().sleep(500);}
catch(InterruptedException ie){}
}
}
public static void main(String args[])
{
new Aaa();
}
}
别的就没有什么好讲的了,呵呵。