简体   繁体   English

Java中的多线程执行服务,是否忽略线程的详细信息?

[英]executor service in java for multi threading, ignore the details of threading?

I tried learning threads and runnable in java and a collegue of mine told me to stop it will take too learn these details. 我试图学习线程并在Java中运行,而我的一位同事告诉我停止它也将需要学习这些细节。 Just lookup how to write an executor. 只是查找如何编写执行程序。 How does this work in java. 这在Java中是如何工作的。 Can someone provide an example? 有人可以提供例子吗?

Thank you! 谢谢!

To solve small, isolated problems in programming without a bigger project behind, you can go and learn about executors directly. 为了解决编程中小的孤立问题而又没有更大的项目,您可以直接去了解执行器。 Here is an example of how to use Threads pools with the Executor Framework , which is suitable as an entry point. 这是一个如何将线程池与Executor框架一起使用的示例,该框架适合作为入口点。

However, if you plan to work more in an environment where concurrency is an important topic, then it is crucial to have a profound understanding of the basics behind more high level concepts. 但是,如果您打算在并发是重要主题的环境中进行更多工作,那么深刻理解更高级概念背后的基础知识至关重要。 With a good understanding of what happens behind the scenes, you will be able to judge quickly what the best solution is for a problem in regard to performance and scalability. 充分了解幕后情况之后,您将能够快速判断对于性能和可伸缩性问题而言,最佳的解决方案是什么。 Without this understanding, the code you write will do what you want it to do, but if it gets applied to slightly different scenarios that you didn't test, it is likely that your code will perform poorly, maybe even be unusable because of performance degredation. 如果没有这种了解,您编写的代码将执行您想要的操作,但是如果将其应用于未经测试的稍微不同的场景,则代码的性能可能很差,甚至可能由于性能而无法使用降解。 Concurrency is a complex topic and the concepts for solutions can't be applied well without good understanding of the matter. 并发是一个复杂的话题,解决方案的概念不能很好地理解该问题。

Having said this, I strongly recommend also learning about Threads and Runnable. 话虽如此,我强烈建议您还学习有关线程和可运行的信息。

This is an easy to understand implementation of an executor that will run three classes together at max: 这是执行程序的易于理解的实现,该执行程序将最多同时运行三个类:

ExecutorService executorService = Executors.newFixedThreadPool(3);
Callable<Void> thread1 = new MyClass(); 
executorService.submit(thread1);
Callable<Void> thread2 = new MyClass2(); 
executorService.submit(thread2);                
executorService.shutdown();

and the class: 和班级:

public class MyClass implements Callable<Void> {
@Override
public Void call() throws Exception{ }
}

It is true you don't need in depth knowledge for basic stuff just keep in mind that if your classes are accessing shared resources with the possibility of conflicts then you need to have a proper thread implementation with locking etc. 的确,您不需要深入了解基本知识,只需记住,如果您的类正在访问有冲突可能性的共享资源,则您需要具有锁定等功能的适当线程实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM