java multithreading: 1, the difference between threads and processes? Parallel (asynchronous) and concurrent (synchronous) difference? What daemon threads are?

1, the difference between threads and processes?

(1) The fundamental difference between
the process is a unit of resource allocation;
thread is the scheduling and execution units.

(2) overhead
process switching overhead larger. Each process has its own code and data space.
Small thread switching overhead. Thread can be seen as lightweight processes, threads share the same type of code and data space, each thread has its own independent operational stack.

(3) the environment in
the process: to run multiple tasks (programs) at the same time in the operating system.
Thread: Uniform application has multiple sequential flow of execution simultaneously.

(4) memory allocation
process: during the operation of the system will assign different areas of memory for each process.
Thread: Resource thread uses resources that it belongs to the process, thread group can share resources.

(5) contains the relationship
is only one thread can be seen as single-threaded process. If a process has multiple threads, then the execution is not a line, but multiple threads undertaken jointly.
The process is the thread of the container, the thread is part of the process, it can be said threads are lightweight processes.

2, parallel (asynchronous) and concurrent (synchronous) difference?

My understanding is that concurrency and parallel processing are in the face of multiple tasks describe:

If you say that you are now in vegetable, mother tell you to pick up the phone.

(1) (alternate doing different things) and then come back if you stop chopping, answer the phone, answer the phone continue to finish the cooking. This is concurrent.
(2) (at the same time to do something different) If you answer the phone side vegetable side, simultaneously. This is the parallel.

In other words, the synchronization is alternately work, while the work is asynchronous.

3, guard what threads are?

(1) When a daemon thread is the main thread running daemon threads run together. When the main thread of destruction, the destruction of daemon threads together. For example GC. Setting method is as follows daemon threads.

// 标识当前方法为守护线程,一定要在启动线程前设置为守护线程
thread.setDaemon(true);
thread.start();

(2) non-daemon threads still continue to operate after the end of the main thread, the main thread of each other.

Published 57 original articles · won praise 13 · views 1099

Guess you like

Origin blog.csdn.net/weixin_42924812/article/details/105209740