This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pivincii; | |
| public class Counter { | |
| private int count = 0; | |
| public void increment() { | |
| count ++; | |
| } | |
| public void decrement() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pivincii; | |
| import java.util.concurrent.atomic.AtomicInteger; | |
| public class Counter { | |
| private AtomicInteger count = new AtomicInteger(0); | |
| public void increment() { | |
| count.getAndIncrement(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pivincii; | |
| public class Counter { | |
| private volatile int count = 0; | |
| public void increment() { | |
| count ++; | |
| } | |
| public void decrement() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface PokeApi { | |
| @GET("characteristic/{id}") | |
| fun getCharacteristic(@Path("id") id: Int): LiveData<PokeCharacteristic> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } |