线程休眠为什么会多循环一次

这篇博客演示了一个Java程序,该程序创建了两个线程:一个用于读取文件,另一个进行倒计时。文件读取线程逐个打印出读取的字节,并在每次读取后暂停一秒。倒计时线程在完成倒计时后中断文件读取线程,展示了线程间的交互和中断机制。
摘要由CSDN通过智能技术生成

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();                                                                              
                                                                                           
                                                                                           
}                                                                                          
                                                                                           
                                                                                           
                                                                                           

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值