简体   繁体   English

难以理解Java线程

[英]Trouble understanding Java threads

I'm sorry in advance, I learned about multiprocessing from python and I'm having a bit of trouble understanding Java's approach. 抱歉,我从python了解了多处理的知识,在理解Java的方法时遇到了一些麻烦。 In python, I can say I want a pool of 4 processes and then send a bunch of work to my program and it'll work on 4 items at a time. 在python中,我可以说我想要一个由4个进程组成的池,然后向我的程序发送一堆工作,并且它一次可以处理4个项目。 I realized, with Java, I need to use threads to achieve this same task and it seems to be working really really well so far. 我意识到,使用Java,我需要使用线程来完成相同的任务,到目前为止,它似乎真的工作得很好。

But..I unlike in python, my cpu(s) aren't getting 100% utilization(they are about 70-80%) and I suspect its the way I'm creating threads(code is the same between python/java and proceses are independent). 但是..我与python不同,我的cpu没有获得100%的利用率(它们大约为70-80%),并且我怀疑它是我创建线程的方式(python / java和程序是独立的)。 In Java, I'm not sure how to create one thread so I create a thread for every item in a list I want to process, like this: 在Java中,我不确定如何创建一个线程,因此我要为要处理的列表中的每个项目创建一个线程,如下所示:

    for (int i = 0; i < 500; i++) {
        Runnable task = new MyRunnable(10000000L + i);
        Thread worker = new Thread(task);
        // We can set the name of the thread
        worker.setName(String.valueOf(i));
        // Start the thread, never call method run() direct
        worker.start();
        // Remember the thread for later usage
        threads.add(worker);
    }

I took it from here . 我从这里拿走的。 My question is this the correct way to launch threads or is there a way to have java itself manage the number of threads so its optimal? 我的问题是这是启动线程的正确方法,还是有办法让java本身来管理线程数,使其达到最佳状态? I, like everyone I guess, want my code to run as fast as possible and I'm trying to understand how to tell and resolve any issues that maybe arising from too many threads being created. 我和我猜想的每个人一样,我希望我的代码尽可能快地运行,并且我试图理解如何分辨和解决可能由于创建过多线程而引起的任何问题。 This is not a major issue, just curious to how it works under the java hood. 这不是主要问题,只是对它在Java引擎下的工作方式感到好奇。

Thanks! 谢谢!

You use an Executor , the implementation of which handles a pool of threads, decides how many, and so forth. 您使用Executor ,其实现处理一个线程池,确定线程数,依此类推。 See the Java tutorial for lots of examples. 有关许多示例,请参见Java教程

In general, bare threads aren't used in Java except for very simple things. 通常,除了非常简单的东西外,Java中不使用裸线程。 Instead, there will be some higher-level API that receives your Runnable or Task and knows what to do. 相反,会有一些更高级别的API接收您的Runnable或Task并知道该怎么做。

Take a look at the Java Executor API. 看一下Java Executor API。 See this article , for example. 例如,请参阅本文

Although creating Threads is much 'cheaper' than it used to be, creating large numbers of threads (one per runnable as in your example) isn't the way to go - there's still an overhead in creating them, and you'll end up with too much context switching. 尽管创建线程比以前要便宜得多,但是创建大量线程(如示例中的每个可运行线程)并不是可行的方法-创建它们仍然会产生开销,并且最终上下文切换过多。

The Executor API allows you to create various types of thread pool for executing Runnable tasks, so you can reuse threads, flexibly manage the number that are created, and avoid the overhead of thread-per-runnable. Executor API允许您创建各种类型的线程池来执行Runnable任务,因此您可以重用线程,灵活地管理所创建的线程数,并避免每个可运行线程的开销。

The Java threading model and the Python threading model (not multiprocessing) are really quite similar, incidentally. 顺便说一下,Java线程模型和Python 线程模型(不是多处理)确实非常相似。 There isn't a Global Interpreter Lock as in Python, so there's usually less need to fork off multiple processes. 没有像Python那样的Global Interpreter Lock,因此通常较少需要派生多个进程。

Thread is a "low level" API. 线程是“低级” API。

Depending on what you want to do, and the version of java you use, their is better solution. 根据您要执行的操作以及所使用的Java版本,它们是更好的解决方案。 If you use Java 7, and if your task allow it, you can use the fork/join framework : http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html 如果您使用Java 7,并且如果您的任务允许,则可以使用fork / join框架: http : //docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html

However, take a look at the java concurrency tutorial : http://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html 但是,请看一下Java并发教程: http : //docs.oracle.com/javase/tutorial/essential/concurrency/executors.html

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

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