Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- public class App {
- public static void main(String[] args) throws Exception {
- int it = 0;
- 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"};
- List<String> webpages = new ArrayList<String>();
- for (String link : links) {
- webpages.add(scrapeWebpage(link));
- }
- System.out.println("Single-threaded program implementation:");
- long startTime = System.nanoTime();
- for (String webpage : webpages) {
- switch (it){
- case 0:
- System.out.printf("%s:\t", "TV Series, Released between 2020-01-01 and 2021-12-31");
- break;
- case 1:
- System.out.printf("%s:\t", "TV Series, Released between 2019-01-01 and 2019-12-31");
- break;
- case 2:
- System.out.printf("%s:\t", "TV Series, Released between 2018-01-01 and 2018-12-31");
- break;
- case 3:
- System.out.printf("%s:\t", "TV Series, Released between 2017-01-01 and 2017-12-31");
- break;
- }
- System.out.println(searchWebsite(webpage));
- it++;
- }
- long endTime = System.nanoTime();
- long duration = (endTime - startTime);
- System.out.println("Execution time in nanoseconds: " + duration);
- TimeUnit.SECONDS.sleep(1);
- runThreads(webpages);
- }
- public static String scrapeWebpage(final String URL) throws IOException {
- String l = null, document = null;
- URL url = null;
- BufferedReader in = null;
- try {
- url = new URL(URL);
- in = new BufferedReader(new InputStreamReader(url.openStream()));
- while ((l = in.readLine()) != null) {
- document += l;
- }
- } finally {
- if (in != null) {
- in.close();
- }
- }
- return document;
- }
- public static HashMap<String, Integer> searchWebsite(String webpage) {
- HashMap<String, Integer> genres = new HashMap<String, Integer>();
- genres.put("Comedy", 0);
- genres.put("Drama", 0);
- genres.put("Action", 0);
- for (String key : genres.keySet()) {
- //System.out.println(webpage.split(key, -1).length-1);
- genres.put(key, webpage.split(key, -1).length-1);
- }
- return genres;
- }
- static void runThreads(List<String> webpages) throws InterruptedException {
- Thread t1 = new Thread(new Runnable() {
- @Override
- synchronized public void run() {
- System.out.println("TV Series, Released between 2020-01-01 and 2021-12-31\t " + searchWebsite(webpages.get(0)));
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- Thread t2 = new Thread(new Runnable() {
- @Override
- synchronized public void run() {
- System.out.println("TV Series, Released between 2019-01-01 and 2019-12-31\t" + searchWebsite(webpages.get(1)));
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- Thread t3 = new Thread(new Runnable() {
- @Override
- synchronized public void run() {
- System.out.println("TV Series, Released between 2018-01-01 and 2018-12-31\t" + searchWebsite(webpages.get(2)));
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- Thread t4 = new Thread(new Runnable() {
- @Override
- synchronized public void run() {
- System.out.println("TV Series, Released between 2017-01-01 and 2017-12-31\t" + searchWebsite(webpages.get(3)));
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- long startTime = System.nanoTime();
- System.out.println("Quadruple-threaded program implementation:");
- t1.start();
- t3.start();
- t2.start();
- t4.start();
- t1.join();
- t3.join();
- t2.join();
- t4.join();
- long endTime = System.nanoTime();
- long duration = (endTime - startTime);
- System.out.println("Execution time in nanoseconds: " + duration);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement