1
我已經用RxJava成功完成了一個小型Java程序。代碼是:Android RxJava並行化 - 使用ExecutorService進行計算
public static void main(String[] args) {
int threadCt = Runtime.getRuntime().availableProcessors() + 1;
//multi-threading
ExecutorService executor = Executors.newFixedThreadPool(threadCt);
Scheduler scheduler = Schedulers.from(executor);
final AtomicInteger batch = new AtomicInteger(0);
Observable.range(1,80)
.groupBy(i -> batch.getAndIncrement() % threadCt)
.flatMap(g -> g.observeOn(scheduler)
.map(i -> intenseCalculation(i))
).subscribe(System.out::println);
}
public static int intenseCalculation(int i) {
try {
System.out.println("Calculating " + i +
" on " + Thread.currentThread().getName());
Thread.sleep(500);
return i;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
使用此代碼一切工作正常。現在,我想這個代碼傳遞到Android:
Scheduler scheduler = Schedulers.from(executor);
final AtomicInteger batch = new AtomicInteger(0);
Observable.range(0, copiedCategories.size() - 1)
.groupBy(i -> batch.getAndIncrement() % threadCt)
.flatMap(g -> g.observeOn(scheduler))
.map(i -> intenseCalculation(i))
.subscribe(finishedListener::finished);
裏面的方法完成()我正在更新GUI(finishedListener的是,目前活動正在實施一個接口)。
no instance(s) of type variable(s) exist so that void conforms to R
在的build.gradle(對於應用程序)我使用:
compile 'io.reactivex:rxjava:1.2.9'
- (> intenseCalculation(I)I)
我正在與地圖的線得到一個錯誤
我該如何解決這個問題?
您能否重寫代碼?仍然不起作用... –
你有複製粘貼錯誤。您的Java和Android代碼在運營商順序上有所不同。在Java代碼中,你使用'flatMap()',在'flatMap()'內部執行'observeOn()。map()'。但在Android代碼中,您正在執行'flatMap()'和'map()'。你聲稱'現在我試圖將這個代碼傳遞給Android',但那不是相同的代碼。 – azizbekian
的確,我的錯誤。但是我仍然得到錯誤。我已經在groupBy() –