First PR to Android (small but helpful change)

Am I just a magnet encountering issues right away first time I launch something in a while? This time, Intel HAXM failed to install during Android Studio setup and links suggested for manual installation were broken (outdated?).

Anyway, today I’m proud of fixing these 3 lines of code https://android-review.googlesource.com/c/platform/tools/adt/idea/+/1407727

Hopefully this will help someone later on.

Also, I’ve used Gerrit for the first time. Not bad – I love it!

Java Concurrency for JavaScript Developers pt. 1 – Basic async/await Promise functions alternatives

Although Promises are closer to Java futures, this “cheat sheet” might give JavaScript developers a better idea of what concurrency implementation might work to match the JS variant of code.

How would async/await might look like implemented in “Threads” concept

Simple Async Function Execution

JavaScript

async function hello() { console.log("hello"); }

(function() {
  hello();
}());

Java

public class ConcurrencyExperiments {
  public static void main(String[] args) {
    Thread hello = new Thread(() -> { System.out.println("Hello");});
    hello.start();
  }
}

await hello();

JavaScript

async function hello() { console.log("hello"); }

(function() {
  await hello();
}());

Java

public class ConcurrencyExperiments {
  public static void main(String[] args) {
    Thread hello = new Thread(() -> { System.out.println("Hello");});
    hello.start();
    // .join() is equal to "await"
    hello.join();
  }
}

await Promise.all()

JavaScript

async function hello(i) {
  return new Promise((resolve) => {
    console.log("hello " + i);
    resolve();
  });
}

(async function () {
  const promises = [];
  for (let i = 0; i < 10; i++) {
    promises.push(hello(i));
  }
  await Promise.all(promises);
})();

Java using Parallel streams

import java.util.stream.IntStream;

public class ConcurrencyExperiments {
  public static void main(String[] args) {
    IntStream.range(0, 10).parallel().forEach(i -> {
      System.out.println(i);
    });
  }
}

// Tip:
// poolsize for .parallel() stream can be tuned via System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "12");

Java using ExecutorService

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ConcurrencyExperiments {
  public static void main(String[] args) {
    ExecutorService executorService = Executors.newCachedThreadPool();

    for (int i = 0; i < 10; i++) {
      executorService.execute(() -> {
        System.out.println("Thread" + Thread.currentThread().getName());
      });
    }

    executorService.shutdown();

    // Optional, wait for all tasks to be finished or time has been reached
    try {
      executorService.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}