preface

Hello, everyone. I’m Tang Xiaoyuan.

Today to everyone recommended is, process and thread of the beginning of knowledge, I hope to help you, thank you.

Introduction to the

First of all, in terms of terminology, process is the system for resource scheduling and allocation of the basic unit, thread is the process of the smallest execution unit;

For example, in The Task Manager in Windows, you can see the running process, as shown below

PS: One thing to note here is that a program does not equal a process

A program is a collection of instructions and is static; A process is an executing program that is alive

When you double-click to run the program (.exe file), the program’s instructions are loaded into memory, and you get the program’s process

All right, enough of the jargon, let’s move on to the human language

knowledge

1. What is the relationship between processes and threads?

Process can be regarded as a container, thread is the smallest execution unit in the container;

Let’s use an example, such as having a dormitory (process), two people in the dormitory (thread), and a toilet (shared resource).

The relationship between dormitory and people is the relationship between process and thread

A process can contain multiple threads.

2. Why have threads when you have processes?

  1. Each process has its own independent data space. The processes do not share these data resources, which makes communication inconvenient.

    However, threads are different. Multiple threads in a process share the data resources of the process, facilitating communication

  2. Switching between multiple processes costs a lot.

    But multithreading doesn’t. It’s cheap to switch back and forth

    (here under the simple explanation, the introduction of the concept of a virtual space, multiple processes have different virtual space and cache, but multiple threads share the process of virtual space and cache, process switch, cache invalidation, need to go to addressing the virtual space, but the thread because of Shared space, cache and can be used, so it is faster than the process)

3. What are the benefits of multithreading?

Multiple threads cooperate with each other to reuse system resources and improve system throughput

If there is only a single thread, the CPU will be idle while the program performs time-consuming operations (such as IO), resulting in a waste of resources

But multithreading can switch to other threads and continue to perform other tasks, making full use of the CPU

For example, when we watch a movie, our eyes are working and our ears are also working, so we can make full use of our bodies to enjoy the movie.

But if the eyes and ears have to work separately, it can be difficult (imagine sound and picture not syncing).

4. How do multithreading ensure data security?

There are several ways:

  1. Local variables, that is, local variables defined within a single thread that are visible only to themselves, are definitely safe
  2. Read-only objects, that is, shared objects that are read-only, must also be safe
  3. Thread-safe classes — that is, if the class itself is thread-safe, then operations based on that class must also be safe, such as the StringBuffer class
  4. Synchronization and locking mechanism, that is, users by locking, to ensure data security; This can lead to complex and problematic applications

5. What are the life cycles of threads?

As you can see from the JDK source code, the life cycle of a thread has six states, as shown below

 public enum State {
        NEW, // Create thread, but do nothing
        RUNNABLE, // The thread has been started and is running
        BLOCKED, // The thread is blocked
        WAITING, // Wait state, no time limit, until other events are notified
        TIMED_WAITING, // Wait state, there is a time limit, the time will return to the running state
        TERMINATED; // The thread terminates
    }
Copy the code

6. What is the relationship between these states?

The state TERMINATED is interoperable with the RUNNABLE state, except for the NEW state and TERMINATED state

7. How many ways can threads create New?

Three kinds of

  1. Inherit the Thread class (not recommended), but it is not in conformity with the principle of LSP (magnitude of substitution principle, the details may refer to: www.jianshu.com/p/cf9f3c7c0…

  2. (Recommended) Implement the Runnable interface, which is more flexible and secure than the first one

  3. (Recommended) implement the Callable interface, compared to the second, more return values and exceptions thrown

When is BLOCKED usually present?

In the case of a lock, if the lock is not released, the thread will block

9. What’s the difference between WAITING and TIMED_WAITING?

WAITING is an endless state of WAITING until another event informs it that it will stop WAITING and enter a RUNNABLE state.

The TIMED_WAITING state has a maximum waiting time, and if it has not been notified of the maximum waiting time, it automatically stops waiting and enters the RUNNABLE state (passive + active).

conclusion

This is just a brief introduction to threads and processes, but there is still a lot to learn

Reference Books:

  1. Java concurrent programming practice
  2. Practical Java High Concurrency Programming (2nd edition)

Reference links:

  1. On the Richter scale substitution principle: www.jianshu.com/p/cf9f3c7c0…

Afterword.

And finally, thank you for watching. Thank you.