Open zhamri opened 5 years ago
import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier;
public class DownloadRepo implements Callable
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
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);
}
}
Reference https://youtu.be/J3QZ5gfCtAg
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;
}
}
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;
}
}
Reference:
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);
}
}
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;
}
}
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;
}
}
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
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;
}
}
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;
}
}
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;
}
}
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
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);
}
}
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;
}
}
https://www.youtube.com/watch?v=J3QZ5gfCtAg&feature=youtu.be https://www.baeldung.com/java-cyclicbarrier-countdownlatch https://youtu.be/J3QZ5gfCtAg
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
References https://www.youtube.com/watch?v=J3QZ5gfCtAg&feature=youtu.be https://www.baeldung.com/java-cyclicbarrier-countdownlatch https://youtu.be/J3QZ5gfCtAg
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;
}
}
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;
}
}
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);
}
}
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;
}
}
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);
}
}
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;
}
}
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;
}
}
Instruction
https://youtu.be/J3QZ5gfCtAg
Submission