Java basics-multithreading and java basics-Multithreading

Source: Internet
Author: User

Java basics-multithreading and java basics-Multithreading
1. Purpose of introducing threads and processes

The purpose of introducing processes in the OS is to enable more programs to execute concurrently, so as to improve the resource utilization and the amount of the system.

 

The thread is introduced to reduce the time overhead of the Program During Concurrent execution, and the OS has better concurrency.

 

2. Differences between processes and threads

A process is a dynamic execution process of a program. It needs to go through a complete process from loading code to execution, multi-threaded operating systems can run multiple processes (that is, running programs) at the same time. Because the CPU has a time-sharing mechanism, each process can cyclically obtain its own CUP time slice. Because the CPU execution speed is very fast, it seems that all programs are running at the same time (the term is "concurrency ").

A thread is an entity of a process and the basic unit of CPU scheduling and allocation. It is smaller than a thread and can run independently. A thread basically does not own system resources and only has a resource that is essential for running. However, a thread can share all the resources of a process with other threads in the same process.

 

3. Generally, any thread has five states: creation, ready, running, blocking, and termination.

Creation status: A New Thread object can be created using the Thread class constructor.

Ready state: Call the start () method to start the thread. At this time, the thread enters the thread queue and waits for the CPU service.

Running status: Call the run () method of the thread object.

Blocking status: If you call methods such as sleep (), suspend (), and wait () in the executable status, the thread enters the blocking status.

The thread can enter the blocking state only when the cause of the blocking state disappears.

Death state: When the stop () method or run () method is called to end the execution.

 

4. Concept of deadlock

 

5. Process Synchronization

Process Synchronization means that only one thread is running in the same period of time for multiple operating systems. Other threads can continue to execute after the thread is completed.

There are two ways to solve the synchronization problem:

Synchronous Code Block

Synchronized (synchronization object) {code to be synchronized ;}

Synchronization Method

Synchronized (['s zookeeper kr]) keyword declares a method as a synchronous method.

 

 

6. Thread Pool

The thread pool creates a large number of Idle threads when the system starts. The program sends a Runable object to the thread pool, and the thread pool starts a thread to execute the run () method. When run () after the method ends, the thread does not die, but returns to the thread pool again to become idle, waiting for the run () method of the next Runable object.

It is costly for the system to start a new thread because it involves interacting with the operating system. In this case, the use of the thread pool can improve the performance. Especially when the program needs to create a large number of threads with a short lifetime, we should consider using the thread pool.

The thread pool can also effectively control the number of concurrent processes in the system to avoid JVM crash because the system contains a large number of concurrent threads.

  

 

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.