Skip to content

Instantly share code, notes, and snippets.

View vishalratna-microsoft's full-sized avatar

Vishal Ratna vishalratna-microsoft

  • Microsoft
  • Bangalore
View GitHub Profile
@vishalratna-microsoft
vishalratna-microsoft / PCExample.java
Created January 22, 2023 07:02
Demonstrate produce-consumer problem using volatile
public class PCExample {
private volatile boolean shouldProduce = true; // Make this non-volatile, it will not work.
private int value = -1; // As we write the value before a write to volatile, this does not need to be volatile. All values
// will be flushed to main m/m along with volatile. Case of "happens before" guarantee.
public void startProducer() {
Runnable producer = () -> {
for (int i = 0; i < 10; i++) {
while (!shouldProduce) { }
@vishalratna-microsoft
vishalratna-microsoft / DemoClass.java
Created September 9, 2022 17:23
How covariant list allows the blocked operations.
package com.tutorials.vishal;
import com.tutorials.vishal.hierarchy.BaseWork;
import com.tutorials.vishal.hierarchy.RxWork;
import com.tutorials.vishal.hierarchy.Scheduler;
import java.util.ArrayList;
import java.util.List;
public class DemoClass {
@vishalratna-microsoft
vishalratna-microsoft / DemoClass.java
Created September 9, 2022 16:59
Submission of work is easy if work is submitted alone.
package com.tutorials.vishal;
import com.tutorials.vishal.hierarchy.BaseWork;
import com.tutorials.vishal.hierarchy.RxWork;
import com.tutorials.vishal.hierarchy.Scheduler;
public class DemoClass {
void driver() {
BaseWork work = new BaseWork();
@vishalratna-microsoft
vishalratna-microsoft / SingularWebViewPool.java
Last active December 6, 2022 14:32
Pool with single instance of web view
// Has a single web view, and crashes if a request to obtain comes in and the
// cached instance is already allocated.
public static class SingularWebViewPool implements WebViewPool {
private WebView mCachedInstance;
private volatile int mBorrower;
private final int mSignature;
private final Context mAppCtx;
public SingularWebViewPool(Context appCtx) {
@vishalratna-microsoft
vishalratna-microsoft / ViewBasedLazyLifecycleManager.kt
Last active December 6, 2022 14:32
ViewBased implementation of LazyLifecycleManager
import android.util.Log
import android.view.ViewTreeObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import ************.Barrier
import javax.inject.Inject
/**
* ViewBasedLazyLifecycleManager is small construct to ensure that we initialise not so necessary
@vishalratna-microsoft
vishalratna-microsoft / MultiTriggerBomb.kt
Created June 18, 2022 19:20
MultiTriggerBomb implementation
import android.os.Handler
import android.os.Looper
import android.util.Log
import javax.annotation.concurrent.ThreadSafe
/**
* [MultiTriggerBomb] is a construct that executes a piece of code represented as [charge], only if
* 'k' triggers are down('k' conditions become true) or the timeout has happened(in case 'k' conditions do not become true), which ever happens first.
* To plant a bomb, call [MultiTriggerBomb.plant] on the object. Before [MultiTriggerBomb.plant] is called, bomb is in inert state, pressing down the triggers
* has no impact. The timeout counter starts the moment the [MultiTriggerBomb.plant] is called.
import java.util.concurrent.atomic.AtomicInteger
/**
* Construct that helps to restrict the code execution frequency to 1. It will not allow the snippet passed through the close to be executed more than once.
* The first client to execute run() will execute this and other calls to run() will not be respected till reset is called.
*/
class Once {
private val mCounter = AtomicInteger(0)
@vishalratna-microsoft
vishalratna-microsoft / LazyLifecycleManager.kt
Last active June 28, 2022 07:27
Code for LazyLifecycleManager base class
import android.app.Activity
import android.os.Handler
import android.os.Looper
import androidx.annotation.MainThread
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import *******.Closure
import *******.Once
import *******.LazyLifecycleManagerType.ViewBased
import java.lang.ref.WeakReference
@vishalratna-microsoft
vishalratna-microsoft / Barrier.java
Last active June 23, 2022 11:04
Code for Barrier
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@vishalratna-microsoft
vishalratna-microsoft / LazyLifecycleCallbacks.kt
Created April 5, 2022 09:08
LazyLifecycleCallbacks contract
import android.view.View
interface LazyLifecycleCallbacks {
/**
* Lazy version of activity onCreate() callback. Should be used for one time initialisations that could be done after the
* screen has finished rendering. Should not be used for complementary calls that set/reset their state in
* onCreate/onDestroy
*/
fun onLazyCreate()