Advertisement
NAbdulla

Concurrency Example Modified from Bazlur Sir's Book

Jun 6th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. package com.company.chapter2;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.util.concurrent.TimeUnit;
  11.  
  12. public class TwoFileDownloadingWithJoin {
  13.     public static void main(String[] args) {
  14.         Thread down1 = new FileDownloader("https://goo.gl/nqZJn4", "meet1.jpg");
  15.         Thread down2 = new FileDownloader("https://goo.gl/UoSMMt", "meet2.jpg");
  16.  
  17.         down1.setName("file1 downloader");
  18.         down2.setName("file2 downloader");
  19.  
  20.         try {
  21.             down1.start();
  22.             down2.start();
  23.             TimeUnit.MILLISECONDS.sleep(100);
  24.  
  25.             down1.join();
  26.             down2.join();
  27.         } catch (InterruptedException e) {
  28.             e.printStackTrace();
  29.         }
  30.         System.out.println("Download Complete");
  31.     }
  32. }
  33.  
  34. class FileDownloader extends Thread {
  35.     private String url;
  36.     private String fileName;
  37.     private double totRead;
  38.     private ProgressUpdater progressUpdater;
  39.  
  40.     FileDownloader(String url, String fileName) {
  41.         this.url = url;
  42.         this.fileName = fileName;
  43.         this.progressUpdater = new ProgressUpdater(this, fileName);
  44.     }
  45.  
  46.     double getTotRead() {
  47.         return totRead;
  48.     }
  49.  
  50.     @Override
  51.     public void run() {
  52.         File destinationFile = new File(fileName);
  53.         try {
  54.             System.out.println("Starting download of " + fileName);
  55.             URL fileUrl = new URL(url);
  56.  
  57.             HttpURLConnection connection = (HttpURLConnection) fileUrl.openConnection();
  58.             if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  59.                 double fileSize = Double.parseDouble(connection.getHeaderField("content-length"));
  60.  
  61.                 InputStream fis = connection.getInputStream();
  62.                 FileOutputStream fos = new FileOutputStream(destinationFile);
  63.  
  64.                 byte[] buffer = new byte[1024];
  65.                 double currRead;
  66.                 double totSize = fileSize / 1024.0;
  67.  
  68.                 progressUpdater.setTotSize(totSize);
  69.                 progressUpdater.start();
  70.  
  71.                 while ((currRead = fis.read(buffer)) != -1) {
  72.                     fos.write(buffer);
  73.                     totRead += currRead / 1024;
  74.                 }
  75.                 fis.close();
  76.                 fos.close();
  77.                 progressUpdater.kill();
  78.                 progressUpdater.join();
  79.             } else {
  80.                 System.out.println("Error: " + connection.getResponseMessage());
  81.             }
  82.         } catch (MalformedURLException e) {
  83.             e.printStackTrace();
  84.         } catch (IOException e) {
  85.             e.printStackTrace();
  86.         } catch (InterruptedException e) {
  87.             e.printStackTrace();
  88.         }
  89.     }
  90. }
  91.  
  92. class ProgressUpdater extends Thread {
  93.     private FileDownloader fileDownloader;
  94.     private volatile boolean killed;
  95.     private String fileName;
  96.     private double totSize;
  97.  
  98.     public ProgressUpdater(FileDownloader fileDownloader, String fileName) {
  99.         this.fileDownloader = fileDownloader;
  100.         this.fileName = fileName;
  101.         killed = false;
  102.     }
  103.  
  104.     public void kill() {
  105.         killed = true;
  106.     }
  107.  
  108.     public void setTotSize(double totSize) {
  109.         this.totSize = totSize;
  110.     }
  111.  
  112.     @Override
  113.     public void run() {
  114.         boolean hundredPercent = false;
  115.         while (!killed || !hundredPercent) {
  116.             double totRead = fileDownloader.getTotRead();
  117.             System.out.println(String.format("%s: %.2fKB of %.2fKB downloaded(%.2f%%)", fileName, totRead, totSize, 100 * totRead / totSize));
  118.             hundredPercent = (100 * totRead / totSize) > 99.00;
  119.             try {
  120.                 TimeUnit.MILLISECONDS.sleep(100);
  121.             } catch (InterruptedException e) {
  122.                 e.printStackTrace();
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement