如何比较Thread.currentThread()与保存的线程参考? ==,等于,......? [英] How to compare Thread.currentThread() to a saved Thread reference? ==, equals, ..?

查看:118
本文介绍了如何比较Thread.currentThread()与保存的线程参考? ==,等于,......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个类,它将操作从其他方法排队到ConcurrentLinkedQueue,然后有一个process()方法,只能从处理队列的创建线程调用。这是因为可以从其他线程调用其他方法的操作,但是它们的实际操作应该只在创建者线程上执行。这类似于Android的 runOnUiThread 必须使用它来运行UI线程之外的任何UI代码。

I am writing a class which queues up the operations from its other methods into a ConcurrentLinkedQueue, and then has a process() method which should be called only from the creating thread, which processes the queue. This is because the operations from the other methods can be called from other threads, but their actual actions should only be executed on the creator thread. This is akin to Android's runOnUiThread which must be used to run any UI code outside of the UI thread.

我对进程方法的想法是先断言创建者线程正在调用它,并且然后只需运行队列中的每一件事。我不确定如何将当前线程与存储的线程参考进行比较;我找不到关于引用是否保证相同的文档。我应该使用等于方法吗?我应该只使用 == 运算符吗?甚至可以比较Thread对象,还是应该比较它们的名称或其他一些数据呢?

My idea for the process method is to first assert that the creator thread is calling it, and then simply run each thing in the queue. I'm unsure about how to compare the current thread to the stored Thread reference; I can't find documentation about whether the references are guaranteed to be the same. Should I use the equals method? Should I just use the == operator? Can Thread objects even be compared, or should I be comparing their names or some other piece of data?

相关的代码位看起来像这样:(队列逻辑未显示)

The relevant bit of code will look something like this: (queue logic not shown)

public class MultiThreadTasker {
    private Thread creatorThread;

    public MultiThreadTasker() {
        creatorThread = Thread.currentThread();
    }

    public void process() {
        if(Thread.currentThread() != creatorThread) {
            throw new IllegalArgumentException("...");
        }
        // ...
    }

    // ...
}

我们也欢迎提供替代方法的建议。看起来我正在重新创建一个Executor,但我没有看到任何类型的Executor让你手动启动任务的运行而不是它处理它自己的线程。

Recommendations for alternative ways to do this would also be welcome. It seems like I'm reinventing an Executor, but I didn't see any sort of Executor that lets you manually kick off the running of the tasks instead of it handling its own thread(s).

推荐答案

如果您只想在当前线程上执行任务,为什么他们共享一个队列呢?我会在每个线程的本地队列中只有该线程可以看到/使用。

If you only want tasks to be executed on the current thread why do they share a queue at all? I would have a queue local to each thread which only that thread can see/use.

这就是我要做的。

private static final ThreadLocal<Queue<Runnable>> QUEUE = new ThreadLocal<Queue<Runnable>>() {
    @Override
    protected Queue<Runnable> initialValue() {
        return new LinkedList<Runnable>(); 
    }
};

public static void runLater(Runnable runnable) {
    QUEUE.get().add(runnable);
}

public static void runAll() {
    for (Runnable runnable : QUEUE.get())
        runnable.run();
    QUEUE.get().clear();
}

这篇关于如何比较Thread.currentThread()与保存的线程参考? ==,等于,......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆