有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程在JAVA中进程或线程需要什么样的资源

我偶然发现了Java文档,上面说

Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.

参考:https://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html

在这种情况下,我们所说的资源究竟是什么意思

EDIT1:

还有为什么RunnableThreads

什么是通用资源

双方使用的资源有什么不同


共 (2) 个答案

  1. # 1 楼答案

    生成一个新进程将创建一个新的Java虚拟机

    其中as线程将共享内存、JVM等

    JVM不是轻量级程序,因此会消耗更多内存等

    有些JVM是多进程的,允许多个进程共享一个JVM

    从问题中的链接教程: https://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html

    A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.

    Threads share the process's resources, including memory and open files. This makes for efficient, but potentially problematic, communication.


    要解决编辑1

    首先让我们定义一些通用的计算术语

    操作系统概念

    资源https://en.wikipedia.org/wiki/System_resource

    In computing, a system resource, or simply resource, is any physical or virtual component of limited availability within a computer system. Every device connected to a computer system is a resource. Every internal system component is a resource. Virtual system resources include files (concretely file handles), network connections (concretely network sockets), and memory areas

    过程 https://en.wikipedia.org/wiki/Process_(computing)

    In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.

    线程 https://en.wikipedia.org/wiki/Thread_(computing)

    In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.

    Java概念

    过程

    与Java有关,一个进程通常运行一个单独的JVM、不同的堆等

    线程

    线程共享一个JVM,并且能够访问相同的类和内存,但由于它们是Java之外的一个概念,与操作系统有关,因此在交互/创建它们时存在开销

    可运行-https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html

    Runnable是一个仅包含在Java中的概念,操作系统并不知道,它实际上只是一个带有一个名为run的方法的接口,但是您需要自己处理运行它

    将其从线程中抽象出来的原因是,涉及线程本身的类必须考虑与底层操作系统绑定的兼容性,您的runnable不需要知道这些,它只是预期在Java上下文中运行的代码

    它实际上只是一个标记,向其他人显示您计划通过线程或其他形式的计划执行来运行它

    其中as线程是由操作系统管理的外部概念,因此与内存、上下文切换等相关的成本

    进程的成本甚至更高,并且有不共享的独立程序内存

  2. # 2 楼答案

    与环境有关的一切,如cpu、内存、磁盘、网络等