我想构建一个线程,每当我将一个项目添加到队列时,它就会侦听一个队列并执行一个作业。
但我对如何开发它并不太了解。我已经尝试过一些来自RxJava2的Flowable示例,但是没有认识到如何做到这一点。
我对Android和Java中的所有示例都是开放的,也许消息处理程序或执行程序将是一个简单的解决方案。可悲的是没有诀窍。特别是RxJava2会很棒。
更新
换句话说,我想在它上面构建一个队列机制,因为长日志显示为分离的时间,并且只要其中两个在近时间调用它们就会混合。
public final class Logcat {
private static final String TAG = "HOWDY";
public static void v(String message) {
Log.v(TAG, message);
}
public static void d(String message) {
Log.d(TAG, message);
//TODO I will add a for-loop later for long messages to make sure to show all of them for each method.
}
public static void e(Throwable throwable) {
Log.e(TAG, throwable.getMessage());
}
public static void e(String message) {
Log.e(TAG, message);
}
public static void e(ApiError error) {
Log.e(TAG, error.message);
}
}
答案 0 :(得分:1)
好的,我会这样做..
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
public class DemoRxJava2 {
public static void testWithQueue() {
CompletableFuture<String> allDone = new CompletableFuture<>();
AtomicBoolean submitDone = new AtomicBoolean(Boolean.FALSE);
final Queue<Long> queue = new ConcurrentLinkedQueue<>();
Observable.interval(2, TimeUnit.SECONDS)
.takeWhile(tick -> !queue.isEmpty() || !submitDone.get())
.flatMap(tick -> {
return Observable.create(sub -> {
while (!queue.isEmpty()) {
sub.onNext(queue.poll());
}
sub.onComplete();
});
})
.subscribeOn(Schedulers.single())
.doOnSubscribe(dis -> System.out.println("Queue processing active"))
.doOnComplete(() -> {
System.out.println("Queue processing done");
allDone.complete("DONE");
})
.subscribe(nextTs -> System.out.printf("[%s] : Processing tx : %d\n", Thread.currentThread().getName(), nextTs));
Observable.interval(1,TimeUnit.SECONDS)
.take(10)
.doOnSubscribe(dis -> System.out.println("Job submitter start"))
.doOnNext(tick -> {
long ms = System.currentTimeMillis() / 1000;
System.out.printf("[%s] : Submitting tx : %d\n", Thread.currentThread().getName(), ms);
queue.add(ms);
})
.doOnComplete(() -> submitDone.set(Boolean.TRUE))
.blockingSubscribe();
try {
allDone.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
public static void testWithSubject() {
CompletableFuture<String> allDone = new CompletableFuture<>();
PublishSubject<Long> queue = PublishSubject.create();
queue.observeOn(Schedulers.single())
.flatMap(tx -> Observable.just(tx).delay(2, TimeUnit.SECONDS))
.doOnSubscribe(dis -> System.out.println("Queue processing active"))
.doOnComplete(() -> allDone.complete("DONE"))
.subscribe(nextTs -> System.out.printf("[%s] : Processing tx : %d\n", Thread.currentThread().getName(), nextTs));
Observable.interval(1, TimeUnit.SECONDS)
.take(10)
.doOnSubscribe(dis -> System.out.println("Job submitter start"))
.doOnNext(tick -> {
long ms = System.currentTimeMillis() / 1000;
System.out.printf("[%s] : Submitting tx : %d\n", Thread.currentThread().getName(), ms);
queue.onNext(ms);
})
.doOnComplete(() -> queue.onComplete())
.blockingSubscribe();
//wait until all done
try {
allDone.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
testWithQueue();
testWithSubject();
}
}
这只是演示了如何使用RxJava处理单独线程中的对象队列,使其适应您的需要
答案 1 :(得分:0)
这就是我的方式。
interface ILog {
String TAG = "HOWDY";
void display();
}
我的抽象课;
abstract class AbstractLog implements ILog {
String mLog;
AbstractLog(@NonNull String log) {
mLog = log;
}
}
这是我的具体课程;还有另一个类,如Verbose等。
public class ErrorLog extends AbstractLog {
ErrorLog(@NonNull String log) {
super(log);
}
ErrorLog(@NonNull Throwable throwable) {
super(throwable.getMessage());
}
ErrorLog(@NonNull ApiError error) {
super(error.message);
}
@Override
public void display() {
Log.e(TAG, mLog);
}
}
这是与./进行互动的班级开发人员。
public final class Logcat {
private static LogQueue sQueue = new LogQueue();
public static void v(String log) {
Message message = new Message();
message.obj = new VerboseLog(log);
sQueue.sendMessage(message);
}
public static void d(String log) {
Message message = new Message();
message.obj = new DebugLog(log);
sQueue.sendMessage(message);
}
public static void e(Throwable throwable) {
Message message = new Message();
message.obj = new ErrorLog(throwable);
sQueue.sendMessage(message);
}
public static void e(String log) {
Message message = new Message();
message.obj = new ErrorLog(log);
sQueue.sendMessage(message);
}
public static void e(ApiError error) {
Message message = new Message();
message.obj = new ErrorLog(error);
sQueue.sendMessage(message);
}
private static class LogQueue extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
ILog log = (ILog) msg.obj;
log.display();
}
}
}
希望对别人有所帮助。