What is the difference between a process and a thread? --Multi-threading and thread pooling

Source: Internet
Author: User
Tags volatile

Each process is a program, and processes are independent of each other. In addition to distributed, the memory is not shared between process data.

Threads exist in a process, one process consists of one or more threads, each thread is a path, and data is shared between threads.

Multithreading

Advantages: Reasonable use of CPU resources, single-threaded exclusive CPU resources, wasting CPU resources. And can improve the operation efficiency of the program

Cons: 1, if there are a large number of threads running, will consume most of the memory, will affect performance (may crash), the CPU needs to switch between them.

2, thread running may be deadlock, thread safety issues (you need to consider using synchronized, lock (interface), volatile) when modifying operations

The Synchronized keyword guarantees that only one thread in the method is allowed to enter. The lock is automatically released only if the thread executes at the end or an exception occurs. Otherwise, other threads must wait for the thread to be freed. And the Synchronized keyword is a reentrant lock, that is, at execution time if you jump from one synchronization method to another, the thread will no longer be requesting a lock because it is not released. And the keyword is a non-fair lock, and every time a thread releases the lock, there is no guarantee that the lock will be stolen.

Lock is an interface that calls the lock () method in the Run () method when the lock is used. Remember to not declare the lock interface again in the run () method and call lock () so that a new lock is generated each time, and the method cannot be locked. Each time the unlock () method is called at the end to release the lock, because it does not automatically release the lock. No release will cause a deadlock phenomenon. Also can be re-entered lock. The default is a non-fair lock, which becomes a fair lock if the call with parameter construction is true when declared. That is, the thread waiting for the longest time gets the lock first. The Readwritelock interface has Readlock ()/writelock () during read and write operations. Read operations can be performed simultaneously. When a write operation has a lock, both read and write operations wait. Read operation is only a write operation waiting.

The volatile keyword can guarantee the visibility of your data. Not a lock. And while in use, try to ensure that it is an identifier while (flag) {}. Where flag can be visible. Because volatile does not guarantee atomic operation. This keyword guarantees that the write operation of a variable precedes the read operation of the variable.

Thread pool

Advantages: You can reduce the cost of creating and destroying threads, you can specify the maximum number of threads in the pool, and avoid excessive resources competing to consume memory. Better control of thread opening and recycling and the ability to perform tasks on a timed basis.

Executors provides four thread pools:

Executors.newfixedthreadpool (number): Creates a fixed number of thread pools where the exceeded threads wait in the queue, and if there is an exception, a new thread is created automatically.

Executors.newsinglethreadexecutor (): Creates a thread pool with a single thread, guaranteeing an orderly execution, and automatically creates a new thread if there is an exception.

Executors.newcachedthreadpool (): Creates a cached thread pool if there are previous threads that can be used, otherwise created. Threads with more than 60 seconds of leisure time will be discarded.

Executors.newscheduledthreadpool (number ): This thread pool supports the need to perform tasks on a timed and recurring basis.

What is the difference between a process and a thread? --Multi-threading and thread pooling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.