线程怎么关闭? 线程怎么关闭?最好有例子说明。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 class A extends Thread{ public boolean b =true; public void run() { while(true){ if (!b) return; } }}然后在另一个线程里面设置b的值 我的线程里没有 while(true){}循环体怎么结束线程 你让run()里面的东西执行完就自己关闭了,可以不要循环的。run(){ //code}用循环的话,如果要停止,只要跳出循环,把run()里的代码走完就可以了 run(){ System.out.println("hello world");}也是线程 那为什么执行几次(TestThread 是线程类) TestThread testThread = new TestThread ();testThread .start();后系统变得很慢,好像线程没有关闭。 那可能是因为你的线程没有退出,很可能是在run()函数里面死循环了如果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 一个对象了,为什么还要在线程上加锁??? 线程的run函数只要不是死循环,应该执行完都会结束的楼主也没有死循环怎么会没有退出呢?会不会不是这个线程的问题啊? 那为什么执行几次(TestThread 是线程类) TestThread testThread = new TestThread ();testThread .start();后系统变得很慢,好像线程没有关闭。----------------------------------------------楼主可以在系统变慢后打开任务管理器,看看你的java程序是不是占用了100%的CPU资源如果是,说明你的程序里有死循环,找找看哪里死循环了。或者把代码贴上来研究研究 mailUtil.send();方法执行起来比较费时间吧,可能时间都花在这上面了。 呵呵,RUN方法执行完线程就完了呗同意vickcy(逍遥孤云)的方法,看看是不是你有些静态的变量,它可以会占用一些引用,使内存不会被GC收回 怎样让JFrame的标题字变大? 【JMF 】为什么JMF不能播放MP3格式的文件(附代码) 关于xml的dtd验证 如何替换指定jar中的class文件 哪位大虾设计过无人自动回复系统,请进来指点一二 电脑一般能运行多少线程 怎么解析UTF-8格式有BOM EFBBBF 的XML文件 java真的是“一次编写,到处运行吗”?看我的移植: 关于二进制的问题 进程读写优先模拟 类 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收回