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 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 / 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 / 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 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 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 / 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 / Main.java
Created October 24, 2018 16:34
Example of thread join
package com.pivincii;
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("Start Main Thread");
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Start Thread 1");
package com.pivincii;
public class CustomRunnable implements Runnable {
@Override
public void run() {
System.out.println("Start " + Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
package com.pivincii;
public class FirstThread extends Thread {
public void run() {
try {
System.out.println("Start " + currentThread().getName());
sleep(5000);
System.out.println("End" + currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
package com.pivincii.blogging
import android.os.Bundle
class MainActivity : ActivityResultObservableActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}