Oleg G
BAN USERpublic class ProduceConsume1 {
static int countProduce = 0;
// ----------------------
public static class Producer implements Runnable {
private final BlockingQueue<URI> queue;
private boolean run = true;
public Producer(BlockingQueue<URI> q) {
queue = q;
}
public void run() {
try {
while (run) {
queue.put(produce());
if (countProduce % 10 != 0) {
System.out.println("sleep 0.1c for fast requests "
+ countProduce);
Thread.sleep(100);
} else {
System.out.println("sleep 2c for slow requests "
+ countProduce);
Thread.sleep(2000);
}
countProduce++;
Thread.sleep(10);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public URI produce() throws URISyntaxException {
URI url = new URI("someurl/webhp?#q=" + countProduce);
System.out.println("generate url " + url);
return url;
}
}
// ---------------------
public static class Consumer implements Runnable {
private final BlockingQueue<URI> queue;
private boolean run = true;
private ExecutorService executor;
private List<FutureTask<String>> futures;
private List<URI> urls;
public Consumer(BlockingQueue<URI> q) {
queue = q;
futures = new ArrayList<FutureTask<String>>();
ThreadFactory threadFactory = Executors.defaultThreadFactory();
executor = Executors.newFixedThreadPool(3, threadFactory);
urls = new ArrayList<URI>();
}
public void run() {
try {
while (run) {
urls.clear();
queue.drainTo(urls);
if (!urls.isEmpty()) {
final URI lastURI = urls.get(urls.size() - 1);
// cancel all
if (!futures.isEmpty()) {
for (int i = 0; i < futures.size(); i++) {
System.out.println("cancel request "
+ futures.get(i).toString());
if (!futures.get(i).isCancelled()) {
futures.get(i).cancel(true);
}
}
futures.clear();
}
// create new task for rq
FutureTask<String> future = new FutureTask<String>(
new Callable<String>() {
public String call()
throws URISyntaxException {
System.out.println("call request "
+ lastURI);
// call rq and wait for answer
try {
System.out
.println("wait for answer 0.5sec "
+ lastURI);
Thread.sleep(500);
System.out
.println("+++ got response from "
+ lastURI);
return "response from " + lastURI;
} catch (Exception e) {
System.out
.println("--- canceled / interrupted request "
+ lastURI);
return "canceled / interrupted request "
+ lastURI;
}
}
@Override
public String toString() {
return lastURI.toString();
}
});
futures.add(future);
executor.submit(future);
}
Thread.sleep(10);
}
} catch (Exception ex) {
ex.printStackTrace();
}
executor.shutdown();
}
}
// ----------------------
public static void main(String[] args) {
BlockingQueue<URI> q = new LinkedBlockingQueue<URI>();
Producer pr = new Producer(q);
Consumer c = new Consumer(q);
Thread prt = new Thread(pr);
Thread ct = new Thread(c);
prt.start();
ct.start();
try {
Thread.sleep(10000);
pr.run = false;
c.run = false;
prt.join();
ct.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void arrangeLettersToWordNoRepeat(char[] letters) {
int n = (int) Math.pow(2, letters.length);
for (int number = n - 1; number >= 0; number--) {
if (number == 0) {
System.out.print("empty string");
break;
}
for (int position = letters.length - 1; position >= 0; position--) {
if ((number & (1 << position)) != 0) {
System.out.print(letters[letters.length - 1 - position]);
}
}
System.out.println();
}
}
private static int count = 0;
private static void arrange(String[] product, String[][] parameters,
int wInd) {
if (wInd == 0) {
// System.out.println("" + ++count + " " + Arrays.toString(prod));
System.out.print(++count + " ");
for (String p : product) {
System.out.print(p);
}
System.out.println();
} else {
for (int k = 0; k < parameters[wInd - 1].length; k++) {
product[wInd - 1] = parameters[wInd - 1][k];
arrange(product, parameters, wInd - 1);
}
}
}
public static void main(String[] args) {
String[] productName = new String[] { "shoes", "boot" };
String[] style = new String[] { "001", "002" };
String[] color = new String[] { "red", "blue" };
String[] size = new String[] { "7", "8", "9" };
String[] gender = new String[] { "m", "f" };
String[][] parameters = new String[][] { productName, style, color,
size, gender };
String[] product = new String[parameters.length];
arrange(product, parameters, product.length);
}
public static boolean nextArrangementWithRepeatsChars(char[] chars,
char[] charsOriginal, char letter) {
for (int i = 0; i < chars.length; i++) {
if (chars[i] != letter
&& !(i < chars.length - 1 && chars[i + 1] == letter)) {
chars[i] = letter;
while (--i >= 0) {
chars[i] = charsOriginal[i];
}
return true;
}
}
return false;
}
public static void main(String[] args) {
int number = 12345;
String numberStr = String.valueOf(number);
char[] charsOriginal = numberStr.toCharArray();
char[] chars = numberStr.toCharArray();
do {
System.out.println(new String(chars));
} while (nextArrangementWithRepeatsChars(chars, charsOriginal, 'a'));
}
in terms of MR it could be
public List<T> findTopK(List<T> inputData, int k) {
List<T> result = new ArrayList<T>();
// key - inputDataObject, value - its count
Map<T, Integer> map = mapInput(inputData);
// sort map by count values
List<Map.Entry<T, Integer>> sortedEntries = sortMapByValue(map);
// check top size
if (k > sortedEntries.size())
k = sortedEntries.size();
// get top
for (int i = sortedEntries.size() - k; i < sortedEntries.size(); i++) {
result.add(sortedEntries.get(i).getKey());
}
return result;
}
public Map<T, Integer> mapInput(List<T> inputData) {
Map<T, Integer> map = new HashMap<T, Integer>();
for (T t : inputData) {
if (map.containsKey(t)) {
int count = map.get(t);
map.put(t, ++count);
} else {
map.put(t, 1);
}
}
return map;
}
public List<Map.Entry<T, Integer>> sortMapByValue(Map<T, Integer> map) {
List<Map.Entry<T, Integer>> sortedEntries = new ArrayList<Map.Entry<T, Integer>>();
sortedEntries.addAll(map.entrySet());
Collections.sort(sortedEntries, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
return sortedEntries;
}
public static void arr1(int[] word, int[] letters, int wordLength, int t) {
if (t == 0) {
for (int i = 0; i < wordLength; i++) {
System.out.print(word[i]);
}
System.out.println();
} else {
for (int j = 0; j < word.length; j++) {
if (t == wordLength
|| (letters[j] != word[t] && letters[j] < word[t])) {
word[t - 1] = letters[j];
arr1(word, letters, wordLength, t - 1);
}
}
}
}
public static void main(String[] args) {
int[] letters = { 1, 2, 3 };
int[] word = new int[3];
for (int wordLength = 1; wordLength < word.length + 1; wordLength++) {
arr1(word, letters, wordLength, wordLength);
}
}
I am afraid that if itinerary contains loops ( like 1-2-3-1-4-5-1-6) then your map will loose 1-2 and 1-4 ...)
- Oleg G May 19, 2015