多线程学习之五超时模式Timer

timed【超时模式】案例:一个线程提供下载数据,另一个线程执行下载,如果有5秒钟以上,提供下载的线程没有提供数据,下载线程因超时异常,停止下载线程运行。

超时异常类

/**
 * 
 */
package com.benxq.thread6;

import java.util.concurrent.ExecutorService;

/**
 * 超时异常类
 * Created by qucf on 2015年10月22日. 
 */
public class TimeOutException extends InterruptedException{

    public TimeOutException(String msg) {
        super(msg);
    }
}
View Code

下载数据类

/**
 * 
 */
package com.benxq.thread6;

/**
 * 下载数据类
 * Created by qucf on 2015年10月23日. 
 */
public class FileData {
    //提供下载的数据
    private String data;
    //有数据下载标示
    private boolean flag;
    //超时时间
    private long timeout;
    
    public FileData(String data,boolean flag,long timeout){
        super();
        this.data=data;
        this.flag=flag;
        this.timeout=timeout;
    }
    
    //修改状态,唤醒其他线程
    public synchronized void changeStuatus(String data){
        this.data=data;
        this.flag=true;
        notify();
    }

    //下载操作,如果等待1000秒没有数据下载就报超时异常
    public synchronized void execu() throws InterruptedException {
        //开始执行的时间
        long start=System.currentTimeMillis();
        int i=0;
        System.out.println("FileData.execu()下载开始");
        //如果没有数据
        while(!flag){
            //现在的时间
            long now=System.currentTimeMillis();
            long reset=timeout-(now-start);
            if(reset<=0){
                throw new TimeOutException("已经等候"+timeout+"时间了,还没有数据下载");
            }
            wait(reset);
        }
        //下载操作
        download();
    }
    //真正下载操作
    private void download(){
        System.out.println("顺利下载数据===>:"+data);
        this.flag=false;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }
}
View Code

提供数据线程

/**
 * 
 */
package com.benxq.thread6;

/**
 * 提供数据的线程类
 * Created by qucf on 2015年10月23日. 
 */
public class GiveDataThread implements Runnable{

    //公共数据
    private FileData fileData;
    
    public GiveDataThread(FileData fileData) {
        super();
        this.fileData=fileData;
    }
    
    @Override
    public void run() {
        
        //制造数据线程,
        for (int i = 0; i < 10; i++) {
            fileData.changeStuatus("制造数据"+i);
            System.out.println("[提供数据线程池提供数据]=》制造数据"+i);
            
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
    }

}
View Code

下载数据线程

/**
 * 
 */
package com.benxq.thread6;

/**
 * Created by qucf on 2015年10月23日. 
 */
public class DownThread implements Runnable{
    private FileData fileData;
    private boolean flag=true;
    public DownThread(FileData fileData){
        this.fileData=fileData;
    }

    @Override
    public void run() {
        System.out.println("开始下载。。。。。");
        while(flag){
            try {
                fileData.execu();
            } catch ( TimeOutException e) {
                e.printStackTrace();
                flag=false;
            }catch (InterruptedException e) {
                System.out.println("下载超时");
            }
        }
        System.out.println("下载线程超时执行完毕");
    }

}
View Code

主线程

/**
 * 
 */
package com.benxq.thread6;

/**
 * Created by qucf on 2015年10月23日. 
 */
public class Test {

    public static void main(String[] args) {
        
        FileData file=new FileData("hah", true, 5000);
        Thread give=new Thread(new GiveDataThread(file));
        Thread down =new Thread(new DownThread(file));
        
        give.start();
        down.start();
    }
}
View Code
原文地址:https://www.cnblogs.com/quchengfeng/p/4904392.html