单线程多线程分段下载

public class Demo03 {
    public static void main(String[] args) {
        String path = "http://softforspeed.51xiazai.cn/down/BaiduNetdisk_6.9.7.4.exe";
 
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
 
          int  responseCode = conn.getResponseCode();
            System.out.println("responseCode = " + responseCode);
            //如果响应正确 200
            if (responseCode==200){
                //下载
                InputStream in = conn.getInputStream();
                String headerField = conn.getHeaderField("Content-Disposition");
                String field = headerField.split(";")[1];
                String fileName = field.substring(field.indexOf("\"")+1,field.lastIndexOf("\""));
 
                File file = new File(fileName);
                //读一组 写一组
                RandomAccessFile raf =new RandomAccessFile(file,"rwd");
 
 
                int len = 0;
                byte[]bytes=new byte[1024];
                while ((len = in.read(bytes))!=-1){
                    raf.write(bytes,0,len);
                }
                raf.close();
                in.close();
                System.out.println();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
 
 
 
 

class  DownloadThread extends  Thread{
    private  String path;//网速
    private  long start;//开始的位置
    private long end;//结束的位置
 
    /**
     * 下载一段
     */
    @Override
    public void run() {
        String path = "http://softforspeed.51xiazai.cn/down/BaiduNetdisk_6.9.7.4.exe";
        //三个线程
 
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
 
            conn.setRequestProperty("Range","bytes="+start+"-"+end);
 
            int  responseCode = conn.getResponseCode();
            System.out.println("responseCode = " + responseCode);
            //如果响应正确 200
            if (responseCode==206){
                //下载
                int contentLength = conn.getContentLength();
                System.out.println("文件总大小 = " + contentLength);
 
                //切割为三分,计算每份的大小
                int size = contentLength/3;
                /*
                假如 contentLength=10 zize = 3
                线程1:
                线程2:
                线程3: size*2-contentLength
                 */
                for (int i = 0; i < 3; i++) {
                    DownloadThread dt = new DownloadThread();
                    dt.setName("线程");
                    dt.setPath(path);
                    dt.setStart(i*size);
                    if (i==2){
                        dt.setEnd(contentLength);
                    }else {
                        dt.setEnd((i*1)*size);
                    }
                    dt.start();
                }
                }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public String getPath() {
        return path;
    }
 
    public long getStart() {
        return start;
    }
 
    public long getEnd() {
        return end;
    }
 
    public void setPath(String path) {
        this.path = path;
    }
 
    public void setStart(long start) {
        this.start = start;
    }
 
    public void setEnd(long end) {
        this.end = end;
    }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HashMap和ConcurrentHashMap是两种常见的Map实现类,它们在单线程多线程环境下遍历并修改的行为存在一些异同。 1. 单线程环境下: - HashMap:在单线程环境下遍历HashMap并修改元素是安全的。这是因为HashMap不是线程安全的,但在单线程环境下不存在竞争条件,所以不会出现问题。 - ConcurrentHashMap:在单线程环境下遍历ConcurrentHashMap并修改元素同样是安全的。ConcurrentHashMap在设计时考虑了并发修改的情况,使用了锁分段技术(Segment),保证了在单个Segment内的修改操作是线程安全的。 2. 多线程环境下: - HashMap:在多线程环境下同时遍历和修改HashMap可能会导致ConcurrentModificationException异常。这是因为HashMap不是线程安全的,并发修改可能导致迭代器的快速失败机制触发。为了在多线程环境下安全地遍历和修改HashMap,可以使用同步机制如显式锁(synchronized)或并发容器(例如ConcurrentHashMap)。 - ConcurrentHashMap:在多线程环境下遍历和修改ConcurrentHashMap是安全的。ConcurrentHashMap使用了锁分段技术,可以同时支持多个线程的并发读写操作。它提供了强大的线程安全性,能够在多线程环境下高效地进行并发操作。 总结:在单线程环境下,HashMap和ConcurrentHashMap的遍历和修改都是安全的。而在多线程环境下,遍历和修改HashMap可能会导致并发修改异常,而ConcurrentHashMap则提供了线程安全的并发操作支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值