STIW3054-A182 / Main-Issues

5 stars 1 forks source link

CyclicBarrier (Online Learning) #15

Open zhamri opened 5 years ago

zhamri commented 5 years ago

Instruction

  1. Watch the video from the link below:

https://youtu.be/J3QZ5gfCtAg

  1. Modify and improve your program from Issue 14 (CountDownLatch) using CyclicBarrier.

Submission

  1. Java codes
  2. Screenshot of the output
  3. References
slyee96 commented 5 years ago
  1. package issue14;

import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable { private CyclicBarrier barrier; private String URL; private String matrix;

public DownloadRepo(CyclicBarrier barrier , String URL , String matrix) {
    this.barrier = barrier;
    this.URL = URL;
    this.matrix = matrix;
}

public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
        String command = "git clone "+URL;
        Process p = Runtime.getRuntime().exec(command);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
        barrier.wait();
        return null;            
    }
}

package issue14;

import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors;

public class MainClass { public static void main(String[] args) throws FileNotFoundException, InterruptedException { int coreCount = Runtime.getRuntime().availableProcessors(); long startTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(coreCount); System.out.println("number of thread: " + coreCount); Future future1;

    String [] urls;
    String [] matrix;

    BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Lai Yee\\Pictures\\Issue14\\GitHub.txt"));
    String text = reader.lines().collect(Collectors.joining("\n"));

    String subs = "";
    String regex=  "(\\d{6})";
    Matcher m = Pattern.compile(regex).matcher(text);

    while (m.find()) {
        subs +="\n" + m.group().trim();
    }

    urls = text.split("\n");
    matrix = subs.split("\n");

    CyclicBarrier barrier = new CyclicBarrier(urls.length);

    for (int i = 0; i < urls.length; i++) {
        future1 = executor.submit ( new DownloadRepo(barrier , urls[i].trim(), matrix[i]));
    }

    executor.shutdown();

    try {
        barrier.await();
    } catch (BrokenBarrierException e) {
        e.printStackTrace();
    }
    long endTime =System.currentTimeMillis();
    System.out.println("All download completed ...");
    long timeElapsed =endTime - startTime;
    System.out.println("Execution time in milliseconds: " + timeElapsed);
}

}

1

2

Reference https://youtu.be/J3QZ5gfCtAg

mimiothman commented 5 years ago

Java Code


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
//import java.util.concurrent.Callable;
//import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Apps {

    public static void main(String[] args) throws FileNotFoundException, InterruptedException {
        long start = System.currentTimeMillis();
        CyclicBarrier barrier = new CyclicBarrier (4);
        int processor = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(4);
       System.out.println("CPU cores :" + processor);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\isu14.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

       // CountDownLatch latch = new CountDownLatch(urls.length);
        CyclicBarrier barrier1 = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new file1(barrier1 , urls[i].trim(), matrix[i]));

        }

        executor.shutdown();

        try {
            barrier1.await();
        } //catch (InterruptedException e) 
        catch (BrokenBarrierException e){
            e.printStackTrace();
        }

        System.out.println("All download completed ...");

        long stop = System.currentTimeMillis();
        long elapsedTime = stop - start;
        System.out.println("The execute time is : " +elapsedTime + "ms");

    }

}

import java.util.concurrent.Callable;
//import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

public class file1 implements Callable <Integer> {

        //private CountDownLatch latch;
        private CyclicBarrier cycle;
        private String URL;
        private String matrix;

        public file1(CyclicBarrier latch , String URL , String matrix) {
            this.cycle = latch;
            this.URL = URL;
            this.matrix = matrix;
        }

        @Override
        public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
               // latch.countDown();
                cycle.wait();
                return null;            
        }
}

Output

image

image

Reference

  1. https://youtu.be/J3QZ5gfCtAg
  2. https://dzone.com/articles/waiting-for-another-thread-with-cyclicbarrier
  3. https://www.baeldung.com/java-cyclic-barrier
  4. https://examples.javacodegeeks.com/core-java/util/concurrent/cyclicbarrier/java-util-concurrent-cyclicbarrier-example/
ChanJunLiang commented 5 years ago
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws FileNotFoundException {
        long startTime = System.currentTimeMillis();
        ExecutorService executor = Executors.newFixedThreadPool(4);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("D:\\Studies\\rt\\issue14\\url.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new DownloadRepo(barrier , urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        } 

        System.out.println("All download completed ...");
        long endTime = System.currentTimeMillis();
        long timeElapsed = endTime - startTime;
        System.out.println("Execution time in milliseconds: " + timeElapsed);

    }

}
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable <Integer> {

        private CyclicBarrier barrier;
        private String URL;
        private String matrix;

        public DownloadRepo(CyclicBarrier barrier , String URL , String matrix) {
            this.barrier = barrier;
            this.URL = URL;
            this.matrix = matrix;
        }

        @Override
        public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);

                return null;            
        }
}

CB1 CB2

Reference:

  1. https://www.youtube.com/watch?v=J3QZ5gfCtAg&feature=youtu.be
  2. https://www.youtube.com/watch?v=r1PdmOLrfc4
AhKitt commented 5 years ago

Java Code

Issue15

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Issue15 {

    public static void main(String[] args) throws FileNotFoundException {
        long startTime = System.currentTimeMillis();
        ExecutorService executor = Executors.newFixedThreadPool(4);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Asus\\Desktop\\Issue14.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new DownloadRepo(barrier , urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        System.out.println("All download completed ...");
        long endTime = System.currentTimeMillis();
        long timeElapsed = endTime - startTime;
        System.out.println("Execution time in milliseconds: " + timeElapsed);

    }
}

DownloadRepo

import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable <Integer> {

        private CyclicBarrier barrier;
        private String URL;
        private String matrix;

        public DownloadRepo(CyclicBarrier barrier , String URL , String matrix) {
            this.barrier = barrier;
            this.URL = URL;
            this.matrix = matrix;
        }

        public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);

                return null;            
        }
}

Output

issue14

is14

Reference

prevenkumar commented 5 years ago
package issuse15;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws FileNotFoundException, InterruptedException {
        long startTime = System.currentTimeMillis();
        int coreCount = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(coreCount);
        //ExecutorService executor = Executors.newFixedThreadPool(4);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("D:\\CodeSpace\\issuse14\\URL.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new DownloadRepo(barrier , urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
            barrier.await();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        System.out.println("All download completed ...");

        long endTime = System.currentTimeMillis();
        long executionTime = endTime - startTime;
        System.out.println("\nExecution time in milliseconds: "+ executionTime);
    }

}
package issuse14;

import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable <Integer> {

        private CyclicBarrier barrier;
        private String URL;
        private String matrix;

        public DownloadRepo(CyclicBarrier barrier , String URL , String matrix) {
            this.barrier = barrier;
            this.URL = URL;
            this.matrix = matrix;
        }

        @Override
        public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
              //  barrier.countDown();
                return null;            
        }
}

OUTPUT

issue14

REFERENCE

https://www.baeldung.com/java-cyclic-barrier https://www.geeksforgeeks.org/java-util-concurrent-cyclicbarrier-java/ http://tutorials.jenkov.com/java-util-concurrent/cyclicbarrier.html

limxinyii commented 5 years ago

1. Java codes

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class TestCyclicBarrier{

    public static void main(String[] args) throws FileNotFoundException, BrokenBarrierException {
        long startTime = System.currentTimeMillis();
         int coreCount = Runtime.getRuntime().availableProcessors();
         ExecutorService executor = Executors.newFixedThreadPool(coreCount);
         System.out.println("number of thread  : "+coreCount);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\HP\\Downloads\\text.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new DownloadRepo(barrier, urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("All download completed ...");
        long timeElapsed = endTime - startTime;
        System.out.println("\nExecution time in milliseconds: " + timeElapsed);
    }
}
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable <Integer> {

        private CyclicBarrier barrier;
        private String URL;
        private String matrix;

        public DownloadRepo(CyclicBarrier barrier, String URL , String matrix) {
            this.barrier = barrier;
            this.URL = URL;
            this.matrix = matrix;
        }

        @Override
        public Integer call() throws Exception {
               System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);

                return null;            
        }
}

2. Output

image image

3. Reference

  1. https://www.youtube.com/watch?v=J3QZ5gfCtAg&feature=youtu.be
  2. https://www.baeldung.com/java-cyclic-barrier
  3. https://www.youtube.com/watch?v=wGiwXHB-dAg
muhammadbimo1 commented 5 years ago
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws FileNotFoundException, InterruptedException {
        long start = System.currentTimeMillis();
        CyclicBarrier barrier = new CyclicBarrier (4);
        int processor = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(4);
        System.out.println("CPU cores :" + processor);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("List.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier barrier1 = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new DownloadItems(barrier1 , urls[i].trim(), matrix[i]));

        }

        executor.shutdown();

        try {
            barrier1.await();
        }
        catch (BrokenBarrierException e){
            e.printStackTrace();
        }

        System.out.println("All download completed ...");

        long stop = System.currentTimeMillis();
        long elapsedTime = stop - start;
        System.out.println("The execute time is : " +elapsedTime + "ms");

    }

}
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class DownloadItems implements Callable <Integer> {

    private CyclicBarrier cycle;
    private String URL;
    private String matric;

    public DownloadItems(CyclicBarrier latch , String URL , String matric) {
        this.cycle = latch;
        this.URL = URL;
        this.matric = matric;
    }

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matric);
        String command = "git clone "+URL+" C:\\git";
        Process p = Runtime.getRuntime().exec(command);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matric);
        cycle.wait();
        return null;
    }
}

image

roflinasuha commented 5 years ago
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws FileNotFoundException, InterruptedException{
        int numberOfProcessors = Runtime.getRuntime().availableProcessors();;
        long startTime = System.currentTimeMillis();
        ExecutorService executor = Executors.newFixedThreadPool(numberOfProcessors);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\BCC\\Desktop\\try.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new DownloadRepo(barrier , urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
            barrier.await();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        long endTime = System.currentTimeMillis();
        long timeElapsed = endTime - startTime;
        System.out.println("All download completed ...");
        System.out.println("\nExecution time in milliseconds: " + timeElapsed);
        System.out.println("Number of processors available to this JVM: " + numberOfProcessors);
    }
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable <Integer> {

        private CyclicBarrier barrier;
        private String URL;
        private String matrix;

        public DownloadRepo(CyclicBarrier barrier , String URL , String matrix) {
            this.barrier = barrier;
            this.URL = URL;
            this.matrix = matrix;
        }

        @Override
        public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
                //barrier.await();
                return null;            
        }
}

image

Reference 1) https://www.geeksforgeeks.org/java-util-concurrent-cyclicbarrier-java/ 2) http://tutorials.jenkov.com/java-util-concurrent/cyclicbarrier.html 3) https://www.baeldung.com/java-cyclicbarrier-countdownlatch 4) https://www.youtube.com/watch?v=J3QZ5gfCtAg&feature=youtu.be

SINHUI commented 5 years ago

Java Code

App

package CyclicBarrier;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        int countCore= Runtime.getRuntime().availableProcessors();
        ExecutorService executor= Executors.newFixedThreadPool(countCore);

        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Asus\\Documents\\sem 4\\REAL TIME\\Issue14\\Repo.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new DownloadRepo(barrier , urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
           barrier.await();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        System.out.println("All download completed ...");
        long end = System.currentTimeMillis();
        long executeTime = end - start;
        System.out.println("\n Execution time in milliseconds: "+ executeTime);
    }

}

DownloadRepo

package CyclicBarrier;

import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable <Integer> {

    private CyclicBarrier barrier;
    private String URL;
    private String matrix;

    public DownloadRepo(CyclicBarrier barrier , String URL , String matrix) {
        this.barrier = barrier;
        this.URL = URL;
        this.matrix = matrix;
    }

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
        String command = "git clone "+URL;
        Process p = Runtime.getRuntime().exec(command);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
       barrier.wait();
        return null;
    }
}

Output

image image

Reference

https://www.youtube.com/watch?v=J3QZ5gfCtAg&feature=youtu.be https://www.baeldung.com/java-cyclicbarrier-countdownlatch https://youtu.be/J3QZ5gfCtAg

rahimahsahar commented 5 years ago

Java Code

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws FileNotFoundException, InterruptedException {
        long start = System.currentTimeMillis();
        CyclicBarrier barrier = new CyclicBarrier (4);
        int processor = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(4);
       System.out.println("CPU cores :" + processor);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("C:\\issues15\\issues15.txt"));

String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

       // CountDownLatch latch = new CountDownLatch(urls.length);
        CyclicBarrier barrier1 = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new DownloadRepo(barrier1 , urls[i].trim(), matrix[i]));

        }

        executor.shutdown();

        try {
            barrier1.await();
        } //catch (InterruptedException e) 
        catch (BrokenBarrierException e){
            e.printStackTrace();
        }

        System.out.println("All download completed ...");

        long stop = System.currentTimeMillis();
        long elapsedTime = stop - start;
        System.out.println("The execute time is : " +elapsedTime + "ms");

    }

}
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable <Integer> {

        //private CountDownLatch latch;
        private CyclicBarrier cycle;
        private String URL;
        private String matrix;

        public DownloadRepo(CyclicBarrier latch , String URL , String matrix) {
            this.cycle = latch;
            this.URL = URL;
            this.matrix = matrix;
        }

        @Override
        public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
               // latch.countDown();
                cycle.wait();
                return null;            
        }
}

Output issues15

References https://www.youtube.com/watch?v=J3QZ5gfCtAg&feature=youtu.be https://www.baeldung.com/java-cyclicbarrier-countdownlatch https://youtu.be/J3QZ5gfCtAg

Yvevonwen commented 5 years ago

JavaCode

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws FileNotFoundException, BrokenBarrierException{
        // TODO Auto-generated method stub
        long start = System.currentTimeMillis();
        int coreCount = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(coreCount);
        System.out.println(coreCount);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Lenovo\\OneDrive\\Desktop\\Issues_14.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier latch = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new Issues14(latch , urls[i].trim(), matrix[i]));
        }      
        executor.shutdown();

        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("All download completed ...");
        long end = System.currentTimeMillis();
        long executeTime = end - start;
        System.out.println("\nExecution time in milliseconds: " + executeTime);

    }
    }
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class Issues14 implements Callable <Integer>{
     private CyclicBarrier latch;
        private String URL;
        private String matrix;

        public Issues14(CyclicBarrier latch , String URL , String matrix) {
            this.latch = latch;
            this.URL = URL;
            this.matrix = matrix;
        }

        public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
                latch.wait();
                return null;            
        }
}

Output

image image image

Reference

  1. https://www.baeldung.com/java-cyclicbarrier-countdownlatch
  2. http://tutorials.jenkov.com/java-util-concurrent/cyclicbarrier.html
  3. https://www.geeksforgeeks.org/java-util-concurrent-cyclicbarrier-java/
shyyahcheang commented 5 years ago

1. Java codes

package Issue15;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws FileNotFoundException, InterruptedException {
        long start = System.currentTimeMillis();
        int countCore= Runtime.getRuntime().availableProcessors();
        ExecutorService executor= Executors.newFixedThreadPool(countCore);

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\user\\Desktop\\URK.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            Future <Integer> future1 = executor.submit(new DownloadRepo(barrier, urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
            barrier.await();
        }
        catch (BrokenBarrierException e){
            e.printStackTrace();
        }

        System.out.println("All download completed ...");
        long stop = System.currentTimeMillis();
        long executeTime = stop - start;
        System.out.println("The execute time is : " + executeTime + "ms");
    }
}
package Issue15;

import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable <Integer> {

    private CyclicBarrier cycle;
    private String URL;
    private String matrix;

    public DownloadRepo(CyclicBarrier latch , String URL , String matrix) {
        this.cycle = latch;
        this.URL = URL;
        this.matrix = matrix;
    }

    @Override
    public Integer call() throws  Exception {
        System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
        String command = "git clone "+URL;
        Process p = Runtime.getRuntime().exec(command);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
        cycle.wait();
        return null;
    }
}

2. Screenshot of the output

output - issue15 - 1

output - issue15 - 2

3. References

lancelot9927 commented 5 years ago

App

package com.huang.issues14;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws FileNotFoundException, BrokenBarrierException {

        long startTime = System.currentTimeMillis();
        int coreCount = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(coreCount);
        System.out.println("number of thread  : "+coreCount);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\URL.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

      //  CountDownLatch latch = new CountDownLatch(urls.length);
        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
              future1 = executor.submit ( new DownloadRepo(barrier , urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("All download completed ...");
        long endTime = System.currentTimeMillis();
        long executeTime = endTime - startTime;
        System.out.println("\nExecution time in milliseconds: " + executeTime);
    }
}

DownloadRepo

package com.huang.issues14;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable <Integer> {

        private CyclicBarrier barrier;
        private String URL;
        private String matrix;

        public DownloadRepo(CyclicBarrier barrier , String URL , String matrix) {
            this.barrier = barrier;
            this.URL = URL;
            this.matrix = matrix;
        }

        public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
             //   barrier.countDown();
                return null;            
        }
}

Output

issues15

lishaobin commented 5 years ago

App.java

package com.lsb.issuse15;

/**
 * Hello world!
 *
 */
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) throws FileNotFoundException, BrokenBarrierException {

        long startTime = System.currentTimeMillis();
        int coreCount = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(coreCount);
        System.out.println("number of thread  : "+coreCount);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader reader = new BufferedReader(new FileReader("D:\\url2.txt"));
        String text = reader.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

      //  CountDownLatch latch = new CountDownLatch(urls.length);
        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
              future1 = executor.submit ( new DownloadRepo(barrier , urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("All download completed ...");
        long endTime = System.currentTimeMillis();
        long executeTime = endTime - startTime;
        System.out.println("\nExecution time in milliseconds: " + executeTime);
    }
}

DownloadRepo.java

package com.lsb.issuse15;

import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class DownloadRepo implements Callable {
private CyclicBarrier barrier;
private String matrix;

public DownloadRepo(CyclicBarrier barrier , String URL , String matrix) {
    this.barrier = barrier;
    this.matrix = matrix;
}

public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
        barrier.wait();
        return null;            
    }
}

image

alfmrza commented 5 years ago

MAIN

package COM.STIW3054.Test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class issue14 {

    public static void main(String[] args) throws FileNotFoundException, BrokenBarrierException {

        long start = System.currentTimeMillis();

        int coreCount = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(coreCount);
        System.out.println("number of thread  : "+coreCount);
        Future <Integer> future1;

        String [] urls;
        String [] matrix;

        BufferedReader read = new BufferedReader(new FileReader("F:\\URK.txt"));
        String text = read.lines().collect(Collectors.joining("\n"));

        String subs = "";
        String regex=  "(\\d{6})";
        Matcher m = Pattern.compile(regex).matcher(text);

        while (m.find()) {
            subs +="\n" + m.group().trim();
        }

        urls = text.split("\n");
        matrix = subs.split("\n");

        CyclicBarrier barrier = new CyclicBarrier(urls.length);

        for (int i = 0; i < urls.length; i++) {
            future1 = executor.submit ( new issue14DownRepo(barrier , urls[i].trim(), matrix[i]));
        }

        executor.shutdown();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("All download completed ...");
long end = System.currentTimeMillis();
        long executeTime = end - start;
        System.out.println("\nExecution time in milliseconds: " + executeTime);

    }

}

DOWNLOADREPO

package COM.STIW3054.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;

public class issue14DownRepo implements Callable <Integer> {

        private CyclicBarrier barrier;
        private String URL;
        private String matrix;

        public issue14DownRepo(CyclicBarrier barrier , String URL , String matrix) {
            this.barrier = barrier;
            this.URL = URL;
            this.matrix = matrix;
        }

        public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " Start download repo ..."+matrix);
                String command = "git clone "+URL;
                Process p = Runtime.getRuntime().exec(command);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " Finish download repo ..."+matrix);
                return null;            
        }
}

image