Skip to content

Instantly share code, notes, and snippets.

BJ Peter DeLaCruz bjpeterdelacruz

Block or report user

Report or block bjpeterdelacruz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bjpeterdelacruz
bjpeterdelacruz / bug_patrol1.java
Last active Aug 29, 2015
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();
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 Feb 18, 2014
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),
View date.java
public void setDate(Date date) {
if (date == null) {
throw new IllegalArgumentException("date is null");
}
this.date = new Date(date.getTime());
}
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 Aug 29, 2015
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 Feb 18, 2014
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);
You can’t perform that action at this time.