线程怎么关闭?最好有例子说明。
解决方案 »
- JAVA中 THIS关键词的作用
- 南京是否有做个jbpm工作流的?有问题请教
- 继承覆盖吗?
- 求助JTable自定义编辑器的问题。
- 新手测试第一个java不成功
- 在运行 Java 程序时, 如何隐藏 dos 背景呢?
- 【帮忙】正则表达式:[*img1 align=left*]转换为<img src='img1' align='left'>
- 分享Java技术学习路线
- 请各位朋友帮忙看下这段程序,谢谢大家了
- 请问有没有人在IBM Visual Age For Java 下面开发,怎么学VAJ4.0呀,有没有相关的文档?
- 类 Hello 是公共的,应在名为 Hello.java 的文件中声明
- 动态加载问题,请哪位大虾指教!!
{ public boolean b =true;
public void run()
{
while(true){ if (!b) return; }
}
}然后在另一个线程里面设置b的值
while(true){}
循环体
怎么结束线程
run(){
//code
}用循环的话,如果要停止,只要跳出循环,把run()里的代码走完就可以了
run(){
System.out.println("hello world");
}
也是线程
TestThread testThread = new TestThread ();
testThread .start();
后系统变得很慢,好像线程没有关闭。
如果run()函数执行完了,线程就自动关闭了。
楼主把代码贴上来看看吧
import java.io.IOException;
import java.util.List;
import java.util.Map;import con.common.mail.util.MailUtil;public class SendMailThread extends Thread { private List mailInfoList; public synchronized void setMailInfoList(List lst) {
this.mailInfoList = lst;
} public void run() { try {
synchronized (this) { this.sendMail(mailInfoList); }
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} private synchronized void sendMail(List mailInfoList)
throws IOException { Map mailInfoMap = null; try { for (int i = 0; i < mailInfoList.size(); i++) { mailInfoMap = (Map) mailInfoList.get(i); mailUtil = new MailUtil((String) mailInfoMap.get("mailUrl")); mailUtil.setCharSet((String) mailInfoMap.get("mailChyaraset")); mailUtil.setFrom((String) mailInfoMap.get("mailFrom")); mailUtil.setTo((String) mailInfoMap.get("supplierMail")); mailUtil.setXslSubject((String) mailInfoMap.get("subjectPath")); mailUtil.setXslMessage((String) mailInfoMap.get("messagePath"));
//send mail
mailUtil.send();
}
} catch (IOException e) {
throw e;
}
}
}
的是
SendMailThread testThread = new SendMailThread ();
testThread .start();
这种启动线程好吗? 已经new 一个对象了,为什么还要在线程上加锁???
楼主也没有死循环
怎么会没有退出呢?会不会不是这个线程的问题啊?
TestThread testThread = new TestThread ();
testThread .start();
后系统变得很慢,好像线程没有关闭。----------------------------------------------
楼主可以在系统变慢后打开任务管理器,看看你的java程序是不是占用了100%的CPU资源
如果是,说明你的程序里有死循环,找找看哪里死循环了。
或者把代码贴上来研究研究
同意vickcy(逍遥孤云)的方法,
看看是不是你有些静态的变量,它可以会占用一些引用,使内存不会被GC收回