Stack-Repo
/
data
/train
/akash-coded
/FSD_Java
/Lesson-3
/Concurrency and Multithreading
/Examples
/CrawledSites.java
// public synchronized void critial() { | |
// some thread critical stuff here | |
// } | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Data structure for a web crawler. Keeps track of the visited sites and keeps | |
* a list of sites which needs still to be crawled. | |
* | |
* @author Akash Das | |
* | |
*/ | |
public class CrawledSites { | |
private List<String> crawledSites = new ArrayList<>(); | |
private List<String> linkedSites = new ArrayList<>(); | |
public void add(String site) { | |
synchronized (this) { | |
if (!crawledSites.contains(site)) { | |
linkedSites.add(site); | |
} | |
} | |
} | |
/** | |
* Get next site to crawl. Can return null (if nothing to crawl) | |
*/ | |
public String next() { | |
if (linkedSites.size() == 0) { | |
return null; | |
} | |
synchronized (this) { | |
// Need to check again if size has changed | |
if (linkedSites.size() > 0) { | |
String s = linkedSites.get(0); | |
linkedSites.remove(0); | |
crawledSites.add(s); | |
return s; | |
} | |
return null; | |
} | |
} | |
} |