Skip to content

Instantly share code, notes, and snippets.

View finnkvan's full-sized avatar

Finn finnkvan

View GitHub Profile
@finnkvan
finnkvan / Counter.java
Created October 24, 2018 18:04
Interview Question
package com.pivincii;
public class Counter {
private int count = 0;
public void increment() {
count ++;
}
public void decrement() {
@finnkvan
finnkvan / AsynDemo.java
Created October 24, 2018 18:47
synchronized example
package com.pivincii;
public class AsynDemo {
synchronized public void countTo(int value) {
int count = 0;
while (count < value) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
@finnkvan
finnkvan / AsynDemo.java
Created October 24, 2018 18:55
synchronized example
package com.pivincii;
public class AsynDemo {
private Object mutex = new Object();
public void countTo(int value) {
synchronized (mutex) {
int count = 0;
while (count < value) {
try {
@finnkvan
finnkvan / AsynDemo.java
Created October 24, 2018 19:02
Synchronized function
package com.pivincii;
public class AsynDemo {
synchronized public void countTo(int value) {
int count = 0;
while (count < value) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
@finnkvan
finnkvan / AsynDemo.java
Created October 24, 2018 19:29
ReetranLook
package com.pivincii;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class AsynDemo {
Lock lock = new ReentrantLock();
public void countTo(int value) {
lock.lock();
@finnkvan
finnkvan / Counter.java
Created October 24, 2018 20:16
AtomicCounter
package com.pivincii;
import java.util.concurrent.atomic.AtomicInteger;
public class Counter {
private AtomicInteger count = new AtomicInteger(0);
public void increment() {
count.getAndIncrement();
}
@finnkvan
finnkvan / Counter.java
Created October 24, 2018 20:18
Using Volatile
package com.pivincii;
public class Counter {
private volatile int count = 0;
public void increment() {
count ++;
}
public void decrement() {
// Copyright: https://developer.android.com/topic/libraries/architecture/livedata
class NameViewModel : ViewModel() {
// Create a LiveData with a String
val currentName: MutableLiveData<String> by lazy {
MutableLiveData<String>()
}
// Rest of the ViewModel...
}
interface PokeApi {
@GET("characteristic/{id}")
fun getCharacteristic(@Path("id") id: Int): LiveData<PokeCharacteristic>
}
class LiveDataCallAdapter<R>(private val responseType: Type): CallAdapter<R, LiveData<ApiResponse<R>>> {
override fun adapt(call: Call<R>): LiveData<ApiResponse<R>> {
return object : LiveData<ApiResponse<R>>() {
private var isSuccess = false
override fun onActive() {
super.onActive()
if (!isSuccess) enqueue()
}