Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.jakewharton.rxrelay2:rxrelay:2.1.1'

implementation 'com.uber.autodispose:autodispose:1.4.0'
implementation 'com.uber.autodispose:autodispose-android-archcomponents:1.4.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.21'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
package com.tonytang.hello.again.rxjava;

import androidx.appcompat.app.AppCompatActivity;
import static com.uber.autodispose.AutoDispose.autoDisposable;
import static com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class MainActivity extends AppCompatActivity {

private TextView tv_throttle_first;
private TextView tv_throttle_last;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_throttle_first = findViewById(R.id.tv_throttle_first);
tv_throttle_last = findViewById(R.id.tv_throttle_last);
ThreadInfoStreaming.eventStreaming()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this)))
.subscribe(this::renderUI);
}

private void renderUI(ThreadInfo threadInfo) {
switch (threadInfo.source) {
case THROTTLE_FIRST:
tv_throttle_first.setText(threadInfo.toString());
break;
case THROTTLE_LAST:
tv_throttle_last.setText(threadInfo.toString());
break;
}
}


}
6 changes: 6 additions & 0 deletions app/src/main/java/com/tonytang/hello/again/rxjava/Source.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.tonytang.hello.again.rxjava;

public enum Source {
THROTTLE_FIRST,
THROTTLE_LAST
}
23 changes: 23 additions & 0 deletions app/src/main/java/com/tonytang/hello/again/rxjava/ThreadInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.tonytang.hello.again.rxjava;

import androidx.annotation.NonNull;
import java.util.Locale;

public class ThreadInfo {

public final long iteration;
public final Source source;
public final String threadName;

public ThreadInfo(long iteration, Source source, String threadName) {
this.iteration = iteration;
this.source = source;
this.threadName = threadName;
}

@NonNull
@Override
public String toString() {
return String.format(Locale.US, "%s:%s:%s", iteration, source, threadName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.tonytang.hello.again.rxjava;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import java.util.concurrent.TimeUnit;

public class ThreadInfoStreaming {

private ThreadInfoStreaming() {
}

static Observable<ThreadInfo> eventStreaming() {
return Observable.merge(throttleFirst(), throttleLast());
}

private static Observable<ThreadInfo> throttleFirst() {
return rawStream()
.throttleFirst(1, TimeUnit.SECONDS)
.map(iteration -> new ThreadInfo(iteration, Source.THROTTLE_FIRST,
currentThreadName()));
}

private static Observable<ThreadInfo> throttleLast() {
return rawStream()
.throttleLast(1, TimeUnit.SECONDS)
.map(iteration -> new ThreadInfo(iteration, Source.THROTTLE_LAST,
currentThreadName()));
}

private static String currentThreadName() {
return Thread.currentThread().getName();
}

private static Observable<Long> rawStream() {
return Observable.interval(500, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread());
}
}
32 changes: 17 additions & 15 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv_throttle_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
android:layout_height="wrap_content"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/tv_throttle_last"
android:layout_width="match_parent"
android:layout_marginTop="16dp"
android:layout_height="wrap_content"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.tonytang.hello.again.rxjava;

import com.google.common.truth.Truth;
import io.reactivex.Observable;
import io.reactivex.observers.TestObserver;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

public class ThrottleFirstTest {

@Test
public void without_cache_would_execute_footprint_three_times() {

TestObserver<Integer> test = Observable.just(1).throttleLatest(1, TimeUnit.SECONDS, Schedulers.computation()).test();
Thread thread = test.lastThread();
Truth.assertThat(thread.getName()).isEqualTo("Test worker");

}

}
Binary file added doc/throttle_first_vs_throttle_last_thread.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.