public static void main(String[] args) {
Thread t1=new Thread() {
public void run() {
try {
File f=new File("D:\\dd\\hello.txt");
FileInputStream fis=new FileInputStream(f);
int read;
int index=0;
while((read=fis.read())!=-1) {//没到最后继续读
System.out.println("========文件总长度"+f.length()+"个字节");
System.out.println("======读取了"+(++index)+"个字节");
//线程休眠
Thread.sleep(1000);
}
fis.close();
}catch (IOException e) {
}catch (InterruptedException e) {
System.out.println("我被中断了");
}
}
};
创建倒计时线程
Thread t2=new Thread() {
public void run() {
for (int i = 2; i <=6; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
t1.interrupt();
System.out.println("时间倒计时中断,我要结束了");
}
};
t1.start();
t2.start();
}