Advertisement
NAbdulla

Concurrency Example from Bazlur Sir's Book

Jun 6th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.HttpURLConnection;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.nio.file.Files;
  8. import java.nio.file.StandardCopyOption;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11. public class TwoFileDownloadingWithJoin {
  12.     public static void main(String[] args) {
  13.         Thread beatPrinter = new BeatPrinter();
  14.         Thread down1 = new FileDownloader("https://goo.gl/nqZJn4", "meet1.jpg");
  15.         Thread down2 = new FileDownloader("https://goo.gl/UoSMMt", "meet2.jpg");
  16.         beatPrinter.setName("Beat Printer");
  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.             beatPrinter.start();
  25.  
  26.             down1.join();
  27.             down2.join();
  28.             ((BeatPrinter) beatPrinter).kill();
  29.             beatPrinter.join();
  30.         } catch (InterruptedException e) {
  31.             e.printStackTrace();
  32.         }
  33.         System.out.println("Download Complete");
  34.     }
  35. }
  36.  
  37. class BeatPrinter extends Thread {
  38.  
  39.     private volatile boolean live = true;
  40.  
  41.     @Override
  42.     public void run() {
  43.         while (live) {
  44.             printStar("Downloading ");
  45.         }
  46.     }
  47.  
  48.     void kill() {
  49.         live = false;
  50.     }
  51.  
  52.     private void printStar(String msg) {
  53.         System.out.print(msg);
  54.         char[] signs = {'-', '\\', '|', '/'};
  55.         for (char s : signs) {
  56.             System.out.print(s);
  57.             try {
  58.                 Thread.sleep(300);
  59.                 System.out.print('\b');
  60.             } catch (InterruptedException e) {
  61.                 e.printStackTrace();
  62.             }
  63.         }
  64.         for (int i = 0; i < msg.length(); i++)
  65.             System.out.print('\b');
  66.     }
  67. }
  68.  
  69. class FileDownloader extends Thread {
  70.     private String url;
  71.     private String fileName;
  72.  
  73.     FileDownloader(String url, String fileName) {
  74.         this.url = url;
  75.         this.fileName = fileName;
  76.     }
  77.  
  78.     @Override
  79.     public void run() {
  80.         File destinationFile = new File(fileName);
  81.         try {
  82.             System.out.println("Starting download of " + fileName);
  83.             URL fileUrl = new URL(url);
  84.  
  85.             HttpURLConnection connection = (HttpURLConnection) fileUrl.openConnection();
  86.             if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  87.                 InputStream inputStream = connection.getInputStream();
  88.                 Files.copy(inputStream, destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
  89.                 inputStream.close();
  90.             } else {
  91.                 System.out.println("Error: " + connection.getResponseMessage());
  92.             }
  93.         } catch (MalformedURLException e) {
  94.             e.printStackTrace();
  95.         } catch (IOException e) {
  96.             e.printStackTrace();
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement