一个线程里有一个BufferedReader正在等待read,此时要结束这个线程怎么做?我试过直接stop,destroy,都不行...也试过BufferedReader的close方法也不行..高手指教
解决方案 »
- finalize()方法的使用问题
- Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: db/test)
- 在eclipse中转换编码的问题?请赐教
- 命令按钮setActionCommand();作用是什么?
- 一个小问题:四舍五入输出结果,具体请看程序,谢谢!
- 关于JTable 水平滚动条的问题~~~~
- java 连接Oracle 9i数据库出错。请急救~~~
- 如何得到Jdbtable表中某行某列的值并对其进行修改
- 求大神帮忙。一直没搞明白怎么错了
- 新手一枚 哪位大神能解答下
- (在线等待)家庭作业!一个超简单的问题.可是我...再不帮我就笨死了!
- 一个多线程的问题
private BufferedReader in_log = null;
private boolean Login=false; public void login(){
....
out_log.print("(用户名:密码):");
String[] tmp = in_log.readLine().trim().split(":");
.....
}
public void run(){
....
login();
...
}
public void stopTransfer() {
if (!Login) {
this.stop();
}
}
}in_log和out_log封装的是一个console的inputStream和outputStream外部程序启动这个线程,然后在做了一系列操作之后再调用stopTransfer()
www.blogjava.net/huabingl
在debug模式下,程序调用stopTransfer的时候,程序会在readLine的地方停止,线程并没有推出
在run模式下,程序不会停止,但是线程还是没有退出..不知道为什么?怎样才能让这个线程退出?
Runtime.exit不是用来结束线程的吧..
不知道楼上的说法哪里看到的?楼主,stopTransfer()这个函数在哪里调用的?
stop不好用,可以试试调用线程中的interrupt()方法来中断线程,或者直接关闭输入流
我做了个简单的测试
package com.test;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class ThreadTest extends Thread{
public BufferedReader br;
public void run(){
br=new BufferedReader(new InputStreamReader(System.in));
String hello="";
try {
hello = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String A=hello+hello;
}
}package com.test;import java.io.IOException;public class ThreadMain { /**
* @param args
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadTest tt=new ThreadTest();
tt.start();
for(int i=0;i<10000000;i++);
tt.interrupt();
tt.suspend();
tt.stop();
}}
发现即使这样,线程还是没有被销毁..
我想关键在这里吧,此时内部线程的状态应该是wait();所以是你的设计思想有问题,当用户输入结束时,再来关闭线程,不就OK了
路过,路过。
import java.lang.*;public class JavaThread implements java.lang.Runnable{
public DataInputStream m_dataInput;
public JavaThread()
{
try
{
m_dataInput = new DataInputStream(new BufferedInputStream(new FileInputStream("a.txt")));
}
catch(Exception e)
{
}
}
public void run()
{
try{
for(int i=0;i<10;i++)
{
System.out.print("In thread\t");
Thread.sleep(1000);
String sContent = m_dataInput.readLine();
System.out.println(sContent);
}
}
catch(java.lang.InterruptedException e)
{
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<10;i++)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("After stop");//即使stop已经调用,但是这里还会输出
}
}
public void stop()
{
try {
this.m_dataInput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String [] args)
{
JavaThread jt = new JavaThread();
Thread t = new Thread(jt);
t.start();
try
{
Thread.sleep(5000);
jt.stop();
}
catch(java.lang.InterruptedException e)
{
}
}
}
忘记了一句t.stop();这样停止了
import java.io.IOException;
import java.io.InputStreamReader;class ThreadTest extends Thread{
public BufferedReader br;
public void run() {
br=new BufferedReader(new InputStreamReader(System.in));
String hello=""; try {
hello = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Your input: " + hello);
}
}public class B { /**
* @param args
*/
@SuppressWarnings("deprecation") public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadTest tt=new ThreadTest();
tt.start();
tt.stop();
}}
不是正常退出了吗?
即使是:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;class ThreadTest extends Thread{
public BufferedReader br;
public void run() {
br=new BufferedReader(new InputStreamReader(System.in));
String hello=""; while(true) {
try {
hello = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Your input: " + hello);
} //System.out.println("Thread exit.");
}
}public class B { /**
* @param args
*/
@SuppressWarnings("deprecation") public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadTest tt=new ThreadTest();
tt.start();
tt.stop();
}}
也是胜利退出!我记得前几天,碰到一个Socket的receive 到是真的结束不了,不过既然结束不了,就可以用其他方法实现啊!
干吗非得用 stop() 况且可能你用其它方法实现不会比 stop()差.