This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ExecutorService; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.ArrayList; | |
public class Par { | |
int parallelism = Runtime.getRuntime().availableProcessors() + 1; | |
ExecutorService executor = Executors.newCachedThreadPool(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WithCallback { | |
Callback callback = null; | |
void done() { | |
callback.run(); | |
} | |
protected abstract void run(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Write a script pardo.sh. It should take a target number of processes N, and a list of commands. The commands should be executed in parallel on up to N processes. | |
Usage: pardo.sh NUM_PROCESSES COMMAND... | |
Test as follows: | |
time /tmp/pardo.sh 2 "sleep 2" "sleep 4" "sleep 4" "sleep 2" | |
This should take 6 seconds. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ -z $1 ]]; then | |
echo "Usage: $(basename $0) NUM_PROCESSES COMMAND..." | |
fi | |
NUM_PROCESSES=$1 | |
shift | |
SEMAPHORE=/tmp/semaphore.$RANDOM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function my_print() { | |
for arg in "$@"; do | |
echo -n "{$arg} " | |
done | |
echo | |
} | |
var1="Two bits" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = r"print \"s = r\\\"\" + s + \"\\\"\"\nprint s.decode(\"string-escape\")" | |
print "s = r\"" + s + "\"" | |
print s.decode("string-escape") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T> | |
struct Maybe { | |
public: | |
Maybe() {} | |
explicit Maybe(T v) : state_(v) {} | |
bool isJust() { | |
return state_; | |
} |
OlderNewer