Open zhamri opened 5 years ago
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.rmi.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class URLReader implements Runnable{
public static void main(String[] args) throws UnknownHostException {
long startTime = System.nanoTime();
BufferedReader reader;
ExecutorService service = Executors.newFixedThreadPool(4);
try {
reader = new BufferedReader(new FileReader("/Users/trina/Desktop/URL.txt"));
String text = reader.readLine();
int a = 0;
while(text!=null) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(text).openConnection();
con.setRequestMethod("HEAD");
int responseCode = con.getResponseCode();
if(responseCode == 200) {
System.out.println("\nYes" );
text = reader.readLine();
a++;
service.execute(new URLReader());
}
}catch(Exception e) {
System.out.println("\nNot Exist");
text = reader.readLine();
continue;
}
}
reader.close();
}catch(IOException e) {
e.printStackTrace();
}
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + timeElapsed / 1000000);
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
}
import java.io.*;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.*;
public class Issue11{
public static void main(String[]args) throws IOException{
long startTime = System.currentTimeMillis();
File file = new File("C:\\Users\\Asus\\Desktop\\URL.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(coreCount);
String url;
while ((url = br.readLine()) != null) {
Thread t = new Thread(new MyThread(url));
service.execute(t);
}
br.close();
service.shutdown();
while(!service.isTerminated()){}
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
}
import java.net.*;
public class MyThread implements Runnable{
String web;
public MyThread(String web){
this.web=web;
}
public void run(){
try {
if(exists(web)){
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Yes)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Not Exist)");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean exists(String URLName) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Issue9{
public static void main(String[]args) throws IOException{
long startTime = System.currentTimeMillis();
File file = new File("D:\\Studies\\rt\\issue 11\\url.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(coreCount);
String st;
while ((st = br.readLine()) != null) {
Thread checkurl = new Thread(new CheckURL(st));
service.execute(checkurl);
}
br.close();
service.shutdown();
while(!service.isTerminated()){}
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
}
import java.net.*;
public class CheckURL implements Runnable{
String url;
public CheckURL(String url){
this.url = url;
}
public void run(){
if(exists(url)){
System.out.println(Thread.currentThread().getName()+ " - " + url + " (Yes)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " + url + " (Not Exist)");
}
}
public static boolean exists(String URLName) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class issue11URL {
public static void main(String[]args) throws IOException{
long start = System.currentTimeMillis();
int countCore= Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(countCore);
File file = new File("C:\\Users\\Asus\\Documents\\sem 4\\REAL TIME\\Issue11\\URL.txt");
BufferedReader scan = new BufferedReader(new FileReader(file));
String read;
int i=1;
while ((read= scan.readLine()) != null) {
Thread mythread = new Thread(new Thread1(read), "Thread"+i);
service.execute(mythread);
i++;
}
scan.close();
service.shutdown();
while (!service.isTerminated()) {}
long end = System.currentTimeMillis();
long executeTime = end - start;
System.out.println("\n Execution time in milliseconds: "+ executeTime);
}
}
import java.net.*;
import java.io.IOException;
public class Thread1 implements Runnable{
String link;
public Thread1(String link){
this.link=link;
}
public void run(){
try {
if(valid(link)) {
System.out.println("\n"+Thread.currentThread().getName()+"\n Yes: "+link);
}
else{
System.out.println("\n"+Thread.currentThread().getName() +"\n Not Exist: "+link);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean valid(String url) throws MalformedURLException, IOException {
try {
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection checkURL= (HttpURLConnection) new URL(url).openConnection();
checkURL.setRequestMethod("HEAD");
checkURL.connect();
return (checkURL.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class issues11 {
public static void main(String[]args) throws IOException{
String Urltxt;
long start = System.currentTimeMillis();
File file = new File("C:\\Users\\Lenovo\\OneDrive\\Desktop\\URL.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
int count = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(count);
while ((Urltxt = reader.readLine()) !=null) {
Thread thread = new Thread(new Thread_1(Urltxt));
service.execute(thread);
}
reader.close();
service.shutdown();
while(!service.isTerminated()) {}
long end = System.currentTimeMillis();
long executeTime = end - start;
System.out.println("\nExecution time in milliseconds is "+executeTime);
}
}
import java.net.HttpURLConnection;
import java.net.URL;
public class Thread_1 implements Runnable{
String web;
public Thread_1(String web) {
this.web = web;
}
public void run() {
try {
if(exists(web)){
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Yes)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Not Exist)");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean exists(String URLName) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class issue11 {
public static void main(String arg[]) throws IOException {
long startTime = System.currentTimeMillis();
int corecount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(corecount);
Path path = Paths.get("C:\\Users\\User\\Desktop\\lol.txt");
List<String> lines = null;
lines = Files.readAllLines(path);
for(int i = 0; i < lines.size(); i++) {
service.execute(new checkURL(lines.get(i)));
}
shutdownAndAwaitTermination(service);
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
static class checkURL implements Runnable {
String line;
checkURL(String line){
this.line = line;
}
@Override
public void run() {
try {
HttpURLConnection check= (HttpURLConnection) new URL(line).openConnection();
check.getResponseCode();
System.out.println( Thread.currentThread().getName()+" : "+ line +"\t Yes");
} catch (IOException e) {
System.out.println(Thread.currentThread().getName()+" : "+line +"\t Not Exist");
}
}
}
static void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
}
package issue11;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Issue11 {
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\user\\Desktop\\URLLink.txt"));
String url;
int coreCount= Runtime.getRuntime().availableProcessors();
ExecutorService service =Executors.newFixedThreadPool(coreCount);
while ((url = reader.readLine()) != null){
Thread t1=new Thread (new test(url));
service.execute(t1);
}
reader.close();
service.shutdown();
while(!service.isTerminated()){}
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
}
package issue11;
import java.net.*;
public class test implements Runnable {
String url;
public test(String url){
this.url=url;
}
@Override
public void run(){
try{
if (isValid(url)) {
System.out.println(Thread.currentThread().getName()+ " : " + url +" Yes"); }
else{
System.out.println(Thread.currentThread().getName()+ " : " + url +" No Exist"); }
}
catch (Exception e) {
e.printStackTrace();
}
}
public static boolean isValid(String url)
{
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class issue11 {
public static void main(String[]args) throws IOException{
long startTime = System.currentTimeMillis();
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(coreCount);
File file = new File("test.txt");
BufferedReader read = new BufferedReader(new FileReader(file));
String scan;
while ((scan=read.readLine())!=null) {
Thread myThread = new Thread(new checkUrl(scan));
service.execute(myThread);
}
read.close();
service.shutdown();
while(!service.isTerminated()) {}
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: "+ executionTime);
}
}
import java.net.HttpURLConnection;
import java.net.URL;
public class checkUrl implements Runnable {
String url;
public checkUrl(String url) {
this.url = url;
}
@Override
public void run() {
try {
if (checkIfURLExists(url)) {
System.out.println("\n"+Thread.currentThread().getName()+ "\n" + url + " (Yes)");
}
else {
System.out.println("\n"+Thread.currentThread().getName()+ "\n" + url + " (Not Exist)");
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static boolean checkIfURLExists (String urlString)
{
HttpURLConnection httpUrlConn;
try {
HttpURLConnection.setFollowRedirects(true);
httpUrlConn = (HttpURLConnection) new URL (urlString).openConnection();
httpUrlConn.setRequestMethod("HEAD");
httpUrlConn.setConnectTimeout(1000);
httpUrlConn.setReadTimeout(1000);
return (httpUrlConn.getResponseCode()== HttpURLConnection.HTTP_OK);
}catch (Exception e) {
return false;
}
}
}
package executorService;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ReadUrl {
public static void main (String [] args) {
long start = System.currentTimeMillis();
int count = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(count);
BufferedReader reader;
String line;
int x = 1;
try {
reader = new BufferedReader(new FileReader("C:\\Users\\user\\Desktop\\Url.txt"));
line = reader.readLine();
while((line = reader.readLine()) != null) {
service.execute(new UrlValidator(line));
x++;
}
reader.close();
service.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
while (!service.isTerminated()) {}
long end = System.currentTimeMillis();
long executeTime = end - start;
System.out.println("\nExecution time in milliseconds: "+ executeTime);
}
public static class UrlValidator implements Runnable {
String line;
public UrlValidator (String line) {
this.line = line;
}
@Override
public void run() {
try {
if (urlValidator(line)) {
System.out.println(Thread.currentThread().getName() + "\n" + line + " --> Yes");
} else {
System.out.println(Thread.currentThread().getName() + "\n" + line + " --> Not Exist");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean urlValidator(String url) {
try {
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection check= (HttpURLConnection) new URL(url).openConnection();
check.setRequestMethod("HEAD");
check.connect();
return (check.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
}
Issue11
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Issue11 {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
long startTime = System.currentTimeMillis();
File file = new File("url.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int count = Runtime.getRuntime().availableProcessors();
ExecutorService services = Executors.newFixedThreadPool(count);
String url;
while ((url = br.readLine()) != null) {
Thread t1 = new Thread(new Thread1(url));
services.execute(t1);
}
br.close();
services.shutdown();
while(!services.isTerminated()){}
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
}
Thread1
import java.net.*;
public class Thread1 implements Runnable {
String link;
public Thread1(String link) {
this.link=link;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
if(exists(link)){
System.out.println(Thread.currentThread().getName()+ " - " +link + " ---> <Yes>");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " +link + " ---> <Not Exist>");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean exists(String link) {
// TODO Auto-generated method stub
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(link).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
Output
Java code
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class trying {
public static void main(String arg[]) throws IOException {
long startTime = System.currentTimeMillis();
int counting = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(counting);
Path path = Paths.get("C:\\Users\\User\\Desktop\\url.txt");
List<String> lines = null;
lines = Files.readAllLines(path);
for(int i = 0; i < lines.size(); i++) {
service.execute(new runURL(lines.get(i)));
}
shutdownAndAwaitTermination(service);
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
static class runURL implements Runnable {
String lineURL;
runURL(String line){
this.lineURL = line;
}
@Override
public void run() {
try {
HttpURLConnection check= (HttpURLConnection) new URL(lineURL).openConnection();
check.getResponseCode();
System.out.println( Thread.currentThread().getName()+" : "+ lineURL +"\t Yes");
} catch (IOException e) {
System.out.println(Thread.currentThread().getName()+" : "+lineURL +"\t Not Exist");
}
}
}
static void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown();
try {
if (!pool.awaitTermination(40, TimeUnit.SECONDS)) {
pool.shutdownNow();
if (!pool.awaitTermination(40, TimeUnit.SECONDS))
System.out.println("");
}
} catch (InterruptedException ie) {
pool.shutdownNow();
Thread.currentThread().interrupt();
}
}
}
Output
package issues11;
import java.net.HttpURLConnection;
import java.net.URL;
public class anotherclass extends Thread{
String URL;
public anotherclass(String w ) {
this.URL = w;
}
@Override
public void run() {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URL).openConnection();
con.setRequestMethod("HEAD");
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
int responseCode = con.getResponseCode();
if(responseCode== HttpURLConnection.HTTP_OK)
System.out.println(Thread.currentThread().getName()+" ----"+URL+" IS EXIST");
}catch(Exception e) {
System.out.println(Thread.currentThread().getName()+" ----"+URL+" NOT EXIST");
}
}
}
package issues11;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class MainClass {
static String [] urls ;
public static void main (String[]args) throws IOException, InterruptedException {
long start = System.currentTimeMillis();
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(coreCount);
BufferedReader reader = new BufferedReader(new FileReader("E:\\Project GITHUB\\Issues11\\URL.txt"));
String text =reader.lines().collect(Collectors.joining("\n"));
urls = text.split("\n");
anotherclass [] t = new anotherclass[urls.length];
for (int i = 0; i < urls.length; i++) {
t[i] = new anotherclass(urls[i]);
service.execute(t[i]);
}
service.shutdown();
while(!service.isTerminated()){}
long end = System.currentTimeMillis();
long executeTime = end - start;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
}
import java.io.*;
import java.util.concurrent.*;
public class mainClass extends Thread {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
long executeTime = 0;
try {
BufferedReader read = new BufferedReader(new FileReader("C:\\Users\\Tyjia\\Desktop\\url.txt"));
String line;
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(coreCount);
while ((line = read.readLine()) != null) {
Thread t = new Thread(new url(line));
service.execute(t);
}
read.close();
service.shutdown();
while (!service.isTerminated()) {
}
long endTime = System.currentTimeMillis();
executeTime = endTime - startTime;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
}
import java.net.*;
public class url extends Thread {
private String link;
public url(String l) {
link = l;
}
public boolean checkURL(String url) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
connection.setRequestMethod("HEAD");
return (connection.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
@Override
public void run() {
if (checkURL(link)) {
System.out.println(Thread.currentThread().getName() + ": Exist");
} else {
System.out.println(Thread.currentThread().getName() + ": Not Exist");
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ReadURL {
public static void main(String[]args) throws IOException{
long startTime = System.currentTimeMillis();
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(coreCount);
File file = new File("D:\\UUM\\SEM 4\\Real-time\\URL.txt");
BufferedReader read = new BufferedReader(new FileReader(file));
String scan;
while ((scan=read.readLine())!=null) {
Thread myThread = new Thread(new verification(scan));
service.execute(myThread);
}
read.close();
service.shutdown();
while(!service.isTerminated()) {}
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: "+ executionTime);
}
}
import java.net.HttpURLConnection;
import java.net.URL;
public class verification implements Runnable {
String Url;
public verification(String Url) {
this.Url = Url;
}
@Override
public void run() {
try {
if (checkIfURLExists(Url)) {
System.out.println("\n"+Thread.currentThread().getName() + Url + " (Yes)");
}
else {
System.out.println("\n"+Thread.currentThread().getName() + Url + " (Not Exist)");
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static boolean checkIfURLExists (String urlString)
{
HttpURLConnection httpUrlConn;
try {
HttpURLConnection.setFollowRedirects(true);
httpUrlConn = (HttpURLConnection) new URL (urlString).openConnection();
httpUrlConn.setRequestMethod("HEAD");
httpUrlConn.setConnectTimeout(1000);
httpUrlConn.setReadTimeout(1000);
return (httpUrlConn.getResponseCode()== HttpURLConnection.HTTP_OK);
}catch (Exception e) {
return false;
}
}
}
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.io.*;
public class issue9{
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
ExecutorService executorService = Executors.newFixedThreadPool(10);
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\URL.txt"));
String line;
while ((line = reader.readLine()) != null) {
Thread checkurl = new Thread(new checkURL(line));
executorService.execute(checkurl);
}
reader.close();
executorService.shutdown();
while(!executorService.isTerminated()) {
}
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
}
import java.net.HttpURLConnection;
import java.net.URL;
public class checkURL implements Runnable{
String url;
public checkURL(String url){
this.url = url;
}
public void run(){
if(real(url)){
System.out.println(Thread.currentThread().getName()+ " - " + url + " (Yes)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " + url + " (Not Exist)");
}
}
public static boolean real(String address) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(address).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.net.MalformedURLException;
public class ExecutorSrvcTest{
public static void main (String []args) {
String fileName = "url.txt";
String fileLink = null;
int linecount = 0;
long startTime = System.currentTimeMillis();
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(coreCount);
System.out.println("Available core count: "+ coreCount +"\n");
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((fileLink = bufferedReader.readLine()) != null) {
linecount++;
service.execute(new CheckURL(fileLink.toString()));
}
}catch(MalformedURLException ex) {
System.out.println(ex);
}catch(IOException ex) {
System.out.println(ex);
}
service.shutdown();
while(!service.isTerminated()){}
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("Total line in text file: "+linecount);
System.out.println("Execution time in milliseconds: " + executeTime);
}
}
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class CheckURL implements Runnable{
String fileLink;
int lineCount=0;
public CheckURL(String filelink) {
fileLink=filelink;
}
public void run() {
URLchecker1(fileLink);
}
public synchronized void URLchecker1(String line) {
try {
new URL(line).openStream().close();
System.out.println(Thread.currentThread().getName()+" URL: "+line+" [Yes]\n");
}catch(MalformedURLException ex) {
System.out.println(Thread.currentThread().getName()+" URL: "+line+" [No]");
System.out.println(ex+"\n");
}catch(IOException ex) {
System.out.println(Thread.currentThread().getName()+" URL: "+line+" [No]");
System.out.println(ex+"\n");
}
}
}
package issues11;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class issuesURL
{
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
ExecutorService executorService = Executors.newFixedThreadPool(10);
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\BCC\\Desktop\\url.txt"));
String line;
while ((line = reader.readLine()) != null) {
Thread checkurl = new Thread(new thread(line));
executorService.execute(checkurl);
}
reader.close();
executorService.shutdown();
while(!executorService.isTerminated()) {
}
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
}
package issues11;
import java.net.HttpURLConnection;
import java.net.URL;
public class thread implements Runnable {
String web;
public thread(String web){
this.web=web;
}
public void run(){
if(exists(web)){
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Yes)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Not Exist)");
}
}
public static boolean exists(String URLName) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
Code
executorService.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class executorService extends Thread {
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService executorservice = Executors.newFixedThreadPool(coreCount);
Path filePath = Paths.get("C:\\Users\\USER\\Desktop\\Sem 4\\realtime\\Issue\\Issue-ExecutorService\\executoer_service.txt");
List<String> myURLArrayList = Files.readAllLines(filePath);
myURLArrayList.forEach((String url) -> {
Thread checkurl = new Thread(new CheckURL(url));
executorservice.execute(checkurl);
});
executorservice.shutdown();
while (!executorservice.isTerminated()) {
}
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + executeTime);
}
}
CheckURL.java
import java.net.HttpURLConnection;
import java.net.URL;
public class CheckURL extends Thread{
String Url;
CheckURL( String URL){
this.Url=URL;
}
public boolean checkURL( String url) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
connection.setRequestMethod("HEAD");
return (connection.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
@Override
public void run() {
if (checkURL(Url)) {
System.out.println(Thread.currentThread().getName()+"-"+Url + ": Exist");
} else {
System.out.println(Thread.currentThread().getName() +"-"+Url+ ": Not Exist");
}
}
}
Output
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class issuse10 {
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
ExecutorService executorService = Executors.newFixedThreadPool(10);
BufferedReader reader = new BufferedReader(new FileReader("D:\\\\课件\\\\mp\\\\checkurl.txt"));
String link;
while((link = reader.readLine()) != null) {
Thread t =new Thread(new checkurl(link));
executorService.execute(t);
}
reader.close();
executorService.shutdown();
long end = System.currentTimeMillis();
long execute = end - startTime;
System.out.println("\nExecution time in milliseconds: " + execute);
}
}
import java.net.HttpURLConnection;
import java.net.URL;
public class checkurl implements Runnable{
String link;
public checkurl(String link){
this.link = link;
}
public void run(){
if(r(link)){
System.out.println(Thread.currentThread().getName()+ " - " + link + " (EXIST)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " + link + " (Not Exist)");
}
}
public static boolean r(String address) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(address).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
import java.net.HttpURLConnection;
import java.net.URL;
public class CheckURL extends Thread{
String URL;
public CheckURL(String w ) {
this.URL = w;
}
@Override
public void run() {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URL).openConnection();
con.setRequestMethod("HEAD");
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
int responseCode = con.getResponseCode();
if(responseCode== HttpURLConnection.HTTP_OK)
System.out.println(Thread.currentThread().getName()+" ----"+URL+" EXISTS");
}catch(Exception e) {
System.out.println(Thread.currentThread().getName()+" ----"+URL+" DOES NOT EXIST");
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class ReadText {
static String [] urls ;
public static void main (String[]args) throws IOException, InterruptedException {
long start = System.currentTimeMillis();
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(coreCount);
BufferedReader reader = new BufferedReader(new FileReader("urls.txt"));
String text =reader.lines().collect(Collectors.joining("\n"));
urls = text.split("\n");
CheckURL [] t = new CheckURL[urls.length];
for (int i = 0; i < urls.length; i++) {
t[i] = new CheckURL(urls[i]);
service.execute(t[i]);
}
service.shutdown();
while(!service.isTerminated()){}
long end = System.currentTimeMillis();
long executeTime = end - start;
System.out.println("\nProcess executed in " + executeTime + "Milliseconds.");
}
}
"C:\Program Files\Java\jdk1.8.0_121\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.5\lib\idea_rt.jar=50573:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;C:\Users\beemo\Documents\NetBeansProjects\issue11\out\production\issue11" ReadText
pool-1-thread-4 ----http://chess-results.com/tnr424277.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-2 ----http://chess-results.com/tnr424275.aspx?lan=1&art=1&rd=7 EXISTS
pool-1-thread-3 ----http://chess-results.com/tnr424276.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-1 ----http://chess-results.com/tnr424274.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-1 ----http://chess-results.coom DOES NOT EXIST
pool-1-thread-4 ----http://chess-results.com/tnr424278.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-4 ----http://chess-results.my DOES NOT EXIST
pool-1-thread-3 ----http://chess-results.com/tnr424280.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-1 ----http://chess-results.com/tnr424280.aspx?lan=1&art=1&rd=15 EXISTS
pool-1-thread-4 ----http://chess-results.com/tnr424281.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-3 ----http://chess-results.com/tnr424282.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-3 ----http://chess-results.com/tnr424285.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-2 ----http://chess-results.com/tnr424279.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-3 ----http://chess-results.com/tnr424286.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-3 ----http://chess-results.coom DOES NOT EXIST
pool-1-thread-1 ----http://chess-results.com/tnr424283.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-1 ----http://chess-results.my DOES NOT EXIST
pool-1-thread-4 ----http://chess-results.com/tnr424284.aspx?lan=1&art=1&rd=8 EXISTS
pool-1-thread-3 ----http://chess-results.com/tnr424286.aspx?lan=1&art=1&rd=17 EXISTS
pool-1-thread-1 ----http://chess-results.com/tnr424290.aspx?lan=1&art=1&rd=17 EXISTS
pool-1-thread-2 ----http://chess-results.com/tnr424287.aspx?lan=1&art=1 DOES NOT EXIST
Process executed in 2311Milliseconds.
Process finished with exit code 0
package issues11;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.rmi.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class URLRead implements Runnable{
public static void main(String[] args) throws UnknownHostException {
long startTime = System.nanoTime();
BufferedReader reader;
ExecutorService service = Executors.newFixedThreadPool(4);
try {
reader = new BufferedReader(new FileReader("C:\\Users\\Rahimah\\Desktop\\URL.txt"));
String text = reader.readLine();
int a = 0;
while(text!=null) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(text).openConnection();
con.setRequestMethod("HEAD");
int responseCode = con.getResponseCode();
if(responseCode == 200) {
System.out.println("\nYes" );
text = reader.readLine();
a++;
service.execute(new URLRead());
}
}catch(Exception e) {
System.out.println("\nNot Exist");
text = reader.readLine();
continue;
}
}
reader.close();
}catch(IOException e) {
e.printStackTrace();
}
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + timeElapsed / 1000000);
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
}
Instruction:
Watch the video from the link below:
https://www.youtube.com/watch?v=6Oo-9Can3H8&t=232s
Write a Java program using
ExecutorService
to:pool size
based on the number of processors in your machine.Submission: