【线程一】【翻译】Java中进程Process与线程Thread区别

摘要

Thread and Process are two closely related term in multi-threading and main difference between Thread and Process in Java is that Threads are part of process. i.e. one process can spawn multiple Threads. If you run a Java program in UNIX based system e.g. Linux and if that program creates 10 Threads, it still one process and you can find that by using ps -ef | grep identifier command which is one of most popular use of grep command in UNIX, Where identifier is unix text which can be used as regular expression to find that Java process. Another major difference between Process and Thread is that, each process has its own separate memory space but Threads from same process same memory space. Some linux command map Java thread with light weight process or lwp, e.g. if you use prstat command in Solaris, you can get how many light weight process or Thread a particular Java program is using.
【Yol翻译】线程是进程的子集,一个进程可以有很多线程,每条线程并行执行不同的任务。不同的进程使用不同的内存空间,而所有的线程共享一片相同的内存空间。别把它和栈内存搞混,每个线程都拥有单独的栈内存用来存储本地数据。

来源:http://www.paymoon.com/index.php/2015/08/18/what-is-difference-between-thread-vs-process-java/

具体

In this section we will see some more differences between Process vs Thread in Java to get a clear idea about What is process and What is Thread in Java :

1) Both process and Thread are independent path of execution but one process can have multiple Threads.
译1)【Yol翻译】两者都独立运行,但是进程和线程关系:1:N
2) Every process has its own memory space, executable code and a unique process identifier (PID) while every thread has its own stack in Java but it uses process main memory and share it with other threads.
译2)【Yol翻译】每个进程有自己的内存空间,运行代码和pid(就像linux上的ps -ef|grep java一样),而线程有自己的stack,但是这个线程和其它线程共享主进程的内存空间。
3) Threads are also refereed as task or light weight process (LWP) in operating system
译3)【Yol翻译】线程也被认为是任务或者轻量级进程
4) Threads from same process can communicate with each other by using Programming language construct like wait and notify in Java and much simpler than inter process communication.
译4)【Yol翻译】同进程下的线程可以通信。如wait,notify方法(生产消费者模式)
5) Another difference between Process and Thread in Java is that it’s How Thread and process are created. It’s easy to create Thread as compared to Process which requires duplication of parent process.
译5)【Yol翻译】两者create方式不同。
6) All Threads which is part of same process share system resource like file descriptors , Heap Memory and other resource but each Thread has its own Exception handler and own stack in Java.
译6)【Yol翻译】同进程下的所有线程共享系统资源,内存,其它等。但是各自线程有自己的异常处理和stack

一句话总结

There were some of the fundamental difference between Process and Thread in Java. Whenever you talk about Process vs Thread, just keep in mind that one process can spawn multiple Thread and share same memory in Java. Each thread has its own stack.

线程进程区别一句话:

1)进程与线程关系,是拥有关系,且为:1:N。

2)对1)中线程是共享进程的内存。

3)每个线程有自己的栈。

1 thought on “【线程一】【翻译】Java中进程Process与线程Thread区别

发表评论

电子邮件地址不会被公开。