Advertisement
tsounakis

Threads (IMDB)

Mar 22nd, 2022 (edited)
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.45 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.concurrent.TimeUnit;
  9.  
  10. public class App {
  11.     public static void main(String[] args) throws Exception {
  12.         int it = 0;
  13.         String[] links = {"https://www.imdb.com/search/title/?title_type=tv_series&release_date=2020-01-01,2021-12-31&countries=us", "https://www.imdb.com/search/title/?title_type=tv_series&release_date=2019-01-01,2019-12-31", "https://www.imdb.com/search/title/?title_type=tv_series&year=2018-01-01,2018-12-31", "https://www.imdb.com/search/title/?title_type=tv_series&year=2017,2017&sort=moviemeter,a"};
  14.         List<String> webpages = new ArrayList<String>();
  15.  
  16.         for (String link : links) {
  17.             webpages.add(scrapeWebpage(link));
  18.         }
  19.  
  20.         System.out.println("Single-threaded program implementation:");
  21.  
  22.         long startTime = System.nanoTime();
  23.  
  24.         for (String webpage : webpages) {
  25.             switch (it){
  26.                 case 0:
  27.                     System.out.printf("%s:\t", "TV Series, Released between 2020-01-01 and 2021-12-31");
  28.                     break;
  29.                 case 1:
  30.                     System.out.printf("%s:\t", "TV Series, Released between 2019-01-01 and 2019-12-31");
  31.                     break;
  32.                 case 2:
  33.                     System.out.printf("%s:\t", "TV Series, Released between 2018-01-01 and 2018-12-31");
  34.                     break;
  35.                 case 3:
  36.                     System.out.printf("%s:\t", "TV Series, Released between 2017-01-01 and 2017-12-31");
  37.                     break;
  38.             }
  39.             System.out.println(searchWebsite(webpage));
  40.             it++;
  41.         }
  42.        
  43.         long endTime = System.nanoTime();
  44.         long duration = (endTime - startTime);
  45.  
  46.         System.out.println("Execution time in nanoseconds: " + duration);
  47.  
  48.         TimeUnit.SECONDS.sleep(1);
  49.  
  50.         runThreads(webpages);
  51.     }
  52.  
  53.     public static String scrapeWebpage(final String URL) throws IOException {
  54.         String l = null, document = null;
  55.         URL url = null;
  56.         BufferedReader in = null;
  57.         try {
  58.             url = new URL(URL);
  59.             in = new BufferedReader(new InputStreamReader(url.openStream()));
  60.             while ((l = in.readLine()) != null) {
  61.                 document += l;
  62.             }
  63.         } finally {
  64.             if (in != null) {
  65.                 in.close();
  66.             }
  67.         }
  68.         return document;
  69.     }
  70.  
  71.     public static HashMap<String, Integer> searchWebsite(String webpage) {
  72.        
  73.         HashMap<String, Integer> genres = new HashMap<String, Integer>();
  74.         genres.put("Comedy", 0);
  75.         genres.put("Drama", 0);
  76.         genres.put("Action", 0);
  77.  
  78.         for (String key : genres.keySet()) {
  79.             //System.out.println(webpage.split(key, -1).length-1);
  80.             genres.put(key, webpage.split(key, -1).length-1);
  81.         }
  82.         return genres;
  83.     }
  84.  
  85.     static void runThreads(List<String> webpages) throws InterruptedException {
  86.         Thread t1 = new Thread(new Runnable() {
  87.             @Override
  88.             synchronized public void run() {
  89.                 System.out.println("TV Series, Released between 2020-01-01 and 2021-12-31\t " + searchWebsite(webpages.get(0)));
  90.                
  91.                 try {
  92.                     Thread.sleep(10);
  93.                 } catch (InterruptedException e) {
  94.                     e.printStackTrace();
  95.                 }
  96.             }
  97.         });  
  98.  
  99.         Thread t2 = new Thread(new Runnable() {
  100.             @Override
  101.             synchronized public void run() {
  102.                 System.out.println("TV Series, Released between 2019-01-01 and 2019-12-31\t" + searchWebsite(webpages.get(1)));
  103.                
  104.                 try {
  105.                     Thread.sleep(10);
  106.                 } catch (InterruptedException e) {
  107.                     e.printStackTrace();
  108.                 }
  109.             }
  110.         });  
  111.  
  112.         Thread t3 = new Thread(new Runnable() {
  113.             @Override
  114.             synchronized public void run() {
  115.                 System.out.println("TV Series, Released between 2018-01-01 and 2018-12-31\t" + searchWebsite(webpages.get(2)));
  116.                
  117.                 try {
  118.                     Thread.sleep(10);
  119.                 } catch (InterruptedException e) {
  120.                     e.printStackTrace();
  121.                 }
  122.             }
  123.         });  
  124.  
  125.         Thread t4 = new Thread(new Runnable() {
  126.             @Override
  127.             synchronized public void run() {
  128.                 System.out.println("TV Series, Released between 2017-01-01 and 2017-12-31\t" + searchWebsite(webpages.get(3)));
  129.                
  130.                 try {
  131.                     Thread.sleep(10);
  132.                 } catch (InterruptedException e) {
  133.                     e.printStackTrace();
  134.                 }
  135.             }
  136.         });  
  137.         long startTime = System.nanoTime();
  138.         System.out.println("Quadruple-threaded program implementation:");
  139.         t1.start();
  140.         t3.start();
  141.         t2.start();
  142.         t4.start();
  143.  
  144.         t1.join();
  145.         t3.join();
  146.         t2.join();
  147.         t4.join();
  148.         long endTime = System.nanoTime();
  149.         long duration = (endTime - startTime);
  150.         System.out.println("Execution time in nanoseconds: " + duration);
  151.        
  152.        
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement