Skip to content

Instantly share code, notes, and snippets.

Avatar

BJ Dela Cruz bjpeterdelacruz

View GitHub Profile
@bjpeterdelacruz
bjpeterdelacruz / bug_patrol1.java
Last active August 29, 2015 13:56
Bug Patrol #1. There's a bug in this code snippet. Can you find it?
View bug_patrol1.java
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final ExecutorService executor = Executors.newFixedThreadPool(1);
private final Set<String> serialsSet = new HashSet<String>();
private static final int NUM_SECONDS = 3;
// ...
private void scheduleTask(final String serial) {
// Assume this method is called from a thread in executor.
if (!this.serialsSet.contains(serial)) {
View starting_processes1.java
StringBuffer buffer = new StringBuffer();
for (URL url : ((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs()) {
buffer.append(new File(url.getPath()));
buffer.append(System.getProperty("path.separator"));
}
String classpath = buffer.toString();
int toIndex = classpath.lastIndexOf(System.getProperty("path.separator"));
classpath = classpath.substring(0, toIndex);
View starting_processes2.java
ProcessBuilder builder = new ProcessBuilder("java", "-classpath", classpath, "com.a.b.c.TestProgram", "hello", "world");
// java -classpath [classpath] com.a.b.c.TestProgram hello world
Process process = builder.start();
// Process is now running.
// Block until the process terminates, and then get its exit value.
int exitValue = process.waitFor();
@bjpeterdelacruz
bjpeterdelacruz / bug_patrol1_solution.java
Created February 18, 2014 00:53
Solution to Bug Patrol #1
View bug_patrol1_solution.java
// ...
if (!this.serialsSet.contains(serial)) { // if (!flag) {
this.serialsSet.add(serial); // flag = true;
Runnable task = new Runnable() {
/** {@inheritDoc} */
@Override
public void run() {
// Use try-finally. Before the thread dies, it will reset the boolean variable.
try {
@bjpeterdelacruz
bjpeterdelacruz / jvm_warmup.java
Created February 18, 2014 04:55
This code snippet shows you how to warm up the JVM before performing any benchmarks.
View jvm_warmup.java
private static final int NUM_ITERATIONS = 100;
// ...
System.out.println("Warming up...");
for (int i = 1; i <= NUM_ITERATIONS; i++) {
for (Node node : nodes) {
Band<Float> band = createBand(Float.class);
Random random = new Random();
statsMap.put(band, new Stats(band, random.nextInt(200),
@bjpeterdelacruz
bjpeterdelacruz / date.java
Created February 18, 2014 04:24
Setting a date.
View date.java
public void setDate(Date date) {
if (date == null) {
throw new IllegalArgumentException("date is null");
}
this.date = new Date(date.getTime());
}
@bjpeterdelacruz
bjpeterdelacruz / service_example1.java
Created February 18, 2014 08:08
An example service
View service_example1.java
public class ExampleClientService implements ExampleListener {
private final List<ExampleListener> listeners = new ArrayList<ExampleListener>();
private final List<Example> examples = new ArrayList<Example>();
public void addExampleListener(ExampleListener listener) {
listeners.add(listener);
}
public void removeExampleListener(ExampleListener listener) {
@bjpeterdelacruz
bjpeterdelacruz / service_example2.java
Last active August 29, 2015 13:56
Sample usage (see service_example1.java).
View service_example2.java
ExampleListener listener = new ExampleListener();
ExampleClientService service = new ExampleClientService();
service.addExampleListener(listener);
@bjpeterdelacruz
bjpeterdelacruz / service_example3.java
Created February 18, 2014 08:17
Revised example of a service that hides implementation details by using a private inner class.
View service_example3.java
public class ExampleClientService {
private final List<ExampleListener> listeners = new ArrayList<ExampleListener>();
private final List<Example> examples = new ArrayList<Example>();
private final ExampleListenerImpl listener = new ExampleListenerImpl();
public ExampleClientService(ExamplesManager examplesManager) {
examplesManager.addExampleListener(this.listener);
}
View unlock_test.java
/**
* Tests the locking mechanism in this class.
*
* @param args None.
* @throws Exception If there are problems.
*/
public static void main(String... args) throws Exception {
ExecutorService threadPool = Executors.newFixedThreadPool(5);