[英]Creating a queue that operates a job
I want to build a thread listens a queue and perform a job whenever i add a item to queue. 每当我向队列中添加项目时,我都想构建一个线程来侦听队列并执行一项工作。
But i don't much idea about how to develop it. 但是我对如何开发它并不了解。 I have already tried some Flowable examples from RxJava2 but not recognized how to do it.
我已经尝试过RxJava2的一些Flowable示例,但没有意识到如何实现。
I am open for all examples in Android and Java, Maybe message handler or executors would be an easy solution. 我愿意接受Android和Java中的所有示例,也许消息处理程序或执行程序将是一个简单的解决方案。 Sadly no know-how.
可悲的是没有专门知识。 Esspecally RxJava2 would be great.
特别是RxJava2会很棒。
UPDATE UPDATE
In other words i want to build a queue mechanism on it because long logs are displaying as seperated and timing making them mixed whenever 2 of them called in near time. 换句话说,我想在其上建立队列机制,因为长日志显示为单独的,并且每当其中两个在近时调用时,就会使它们混合计时。
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);
}
}
Ok here is how I would do it.. 好的,这就是我的方法。
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();
}
}
This is just a demonstration of how to process a queue of objects in a seperate thread using RxJava, adapt it to your need 这只是演示如何使用RxJava在单独的线程中处理对象队列,并使其适应您的需求
This is the way i do. 这就是我的方式。
interface ILog {
String TAG = "HOWDY";
void display();
}
And my abstract class; 还有我的抽象课;
abstract class AbstractLog implements ILog {
String mLog;
AbstractLog(@NonNull String log) {
mLog = log;
}
}
Here is my concrete classes; 这是我的具体课程; There are also another class like Verbose and etc.
还有另一个类,例如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);
}
}
This is class developer going to interaction with. 这是要与之交互的类开发人员。
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();
}
}
}
Hope it helps to others. 希望对别人有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.