java - 线程问题 - java.util.concurrent - 可运行/可调用

标签 java concurrency

好吧,我承认我对 Java 线程很陌生,我已经阅读了大量有关 java.util.concurrent 的文章,并且我有几个问题希望有一个真正的人能够回答。

首先,我创建了 ThreadPoolExecutor 和 BlockQueue 来帮助运行我的 Runnable/Callable 方法。为了用我有限的知识让事情变得简单,我将所有 Activity 限制为只读,传递到 Runnable 的任何对象都不会被修改,并且创建的信息是从数据库中提取的,应用程序使用该数据库来填充 GUI 表,其中的任何信息数据库可以在收到之前或之后更改,与显示目的无关。

我好奇的是我所做的是否是线程安全的。我扩展了 ThreadPoolExecutor,这样我就可以重写 afterExecute 方法,以便在 Runnable 完成线程中的工作后执行 Runnable 中的方法。据我所知,这个操作似乎是在主线程中完成的,并且被认为是线程安全的。

同样继续这个想法,如果我编写也在这个 Runnable/Callable 类中的另一个方法并直接使用它而不使用 run (或 ThreadPoolExecutor.execute()),它也会在主线程上运行并且注意安全。问题二:如果我从 Runnable 类的 run() 方法中使用此方法,它是否在线程上运行? (我假设是,并要求确认)并且如果我直接运行这个方法,它会在主线程上运行吗?

原因是因为我希望将与查询/更新/插入有关的所有内容保留在一个类中,但在主线程中保留对数据库的更新。

一些代码来帮助解释:

扩展的ThreadPoolExecutor:

public class DatabaseThreadPoolExecutor extends ThreadPoolExecutor {
    public DatabaseThreadPool(int corePoolSize, int maximumPoolSize, long keepAlive, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(//pass info to super)
    }

    protected void afterExecute(Runnable runnable, Throwable throwable) {
        if(runnable instanceof someRunnable) {
            ((SomeRunnable) runnable).afterExecution();
        }
    }
}

Runnable 类:

public class someRunable implements Runable {

    public someRunnable() {
        // used code withing thread
    }

    public doAfterExecute() {
        // run by ThreadPoolExecutor .afterExecute()
    }

    public doSomething() {
        // Run by someRunnable in thread
        // Also run directly by some other class
    }
}

其他一些调用 doSomething() 的类:

public class someOtherClass {
    public SomeOtherClass(Runnable someRunnable) {
        someRunable.doSomething();
    }
}

最佳答案

在没有看到所有代码的情况下,无法说如此高级的描述是“线程安全的”。据我们所知,您使用的架构中“读取”是阻塞操作,并且它会让您陷入僵局!

通常,人们可以将资源或类描述为“线程安全”,这意味着它在被多个线程使用时不会表现出不可预测的行为。但是,给定的资源是“线程安全”的,并不能证明当您尝试并行执行某些操作时不会导致任何错误,而如果所有操作都在一个线程上完成,则不会发生任何错误。只是行为是可预测的(对于任何给定的一组时间,这当然是不可预测的)。

“线程安全”也不表示某些东西实际上适合高度并发使用。完全同步的 ArrayList 是“线程安全的”,但您肯定不想编写一个在大量不断使用它的线程之间共享它的程序!

我们会尽力回答您的实际问题!:

First I have created ThreadPoolExecutor and BlockQueue to help run my Runnable/Callable methods. To keep things simple with my limited knownledge I have limited all activity to read only, no Objects passed into the Runnable is modified

这似乎是一个明显错误的陈述。如果你的可运行程序确实做了任何事情,那么你正在阅读的内容一定会去某个地方。该某个地方一定是可运行对象引用的东西。 您需要分析您将读取结果推送到的资源如何响应并发访问。这就是潜在的危险所在。

I have extended ThreadPoolExecutor so I can Override the afterExecute method to execute a method within the Runnable after it has finished its work in the thread. From what I have read it seems that this operation is done in the main thread and would be considered thread safe.

不,afterExecute() 将在调用 runnable 上的 run() 方法的同一个池线程中调用。不在提交 Runnable 的线程中。 “This method is invoked by the thread that executed the task

Also continueing with this idea, if I write another method that is also within this Runnable/Callable class and use it directly without the use of run (or ThreadPoolExecutor.execute()) that it would also run on the main thread and be safe.

是的,它将在您调用该方法的线程中运行,而不是在池线程中运行。 “安全”是我们无法仅从架构描述(以及商定的安全定义)开始辨别的东西:)。

Question Two: If I use this method from the run() method of the Runnable class is it run on the thread? (I assume it is, and ask for confirmation)

是的,您在 run() 方法内调用的任何内容都将由拾取您的任务的池线程执行。

关于java - 线程问题 - java.util.concurrent - 可运行/可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9714275/

相关文章:

java - 如何在 Android 中更改 Spinner 选择的背景颜色

java - 为什么我的if语句条件是: "if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)" always false?

NHibernate + 并发编辑 : How to get notified of changes?

java - DelayQueue中的leader到底是做什么用的?

c# - 从多个线程修改 Entity Framework 实体

java - Windows C++ 相当于 Java 的 LockSupport.parkNanos()

java - 如何在 Jframe Swing 中将 JLabel 居中?

Java 编译器错误 : "public type .. must be defined in its own file"?

java - 如何可靠地创建和检测线程死锁

java - Java等待并通知:IllegalMonitorStateException