package com.moto.amtg.oos.instant;import java.io.*;public class AThread extends Thread { boolean stopFlag;
public AThread() {
}
public void run() {
int i = 0;
while (!stopFlag) {
i++;
System.out.println(i);
try {
BufferedReader br = GetBufferedReader();
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("Athread 's run method ");
System.out.println(line);
}
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
public void stopCurrentThread() {
stopFlag = true;
} private static BufferedReader GetBufferedReader() {
String strFile =
System.getProperty("user.home")
+ File.separatorChar
+ "data"
+ File.separatorChar
+ "fm"
+ File.separatorChar
+ "ev20040616.log";
System.out.println(strFile);
BufferedReader br = null;
try {
FileReader fr = new FileReader(strFile);
br = new BufferedReader(fr);
} catch (IOException ioe) {
System.out.println(ioe);
}
return br;
}
}
public AThread() {
}
public void run() {
int i = 0;
while (!stopFlag) {
i++;
System.out.println(i);
try {
BufferedReader br = GetBufferedReader();
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("Athread 's run method ");
System.out.println(line);
}
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
public void stopCurrentThread() {
stopFlag = true;
} private static BufferedReader GetBufferedReader() {
String strFile =
System.getProperty("user.home")
+ File.separatorChar
+ "data"
+ File.separatorChar
+ "fm"
+ File.separatorChar
+ "ev20040616.log";
System.out.println(strFile);
BufferedReader br = null;
try {
FileReader fr = new FileReader(strFile);
br = new BufferedReader(fr);
} catch (IOException ioe) {
System.out.println(ioe);
}
return br;
}
}
stopFlag = true;
}必须要写成public sychronized void stopCurrentThread() {
stopFlag = true;
}还有boolean stopFlag;
写成
volatile boolean stopFlag;
interrupt是让阻塞状态的线程恢复执行,不要误人子弟好不好
stop()终止线程
另一种是run()完后自动终止线程,就是接受ThreadDeath异常