2015.6.10 (Java concurrency)

Source: Internet
Author: User

Today's review is familiar with some concepts of Java concurrency.
Program: The system to complete a task, is a program;
Process: Each running program is a process, Windows Task Manager can see each process, Linux under the use of the ps–e command to view all currently running processes;
Threads: Each running program (process) may contain multiple sequential execution flows, each of which can be considered a thread.
1. Characteristics of the process:
1) Independence: The process is a separate entity in the system, it can have its own independent resources, each process has its own private address space, other processes can not access the data in the process space.
2) Dynamic: The difference between the process and the program is that the program is static, the process is dynamic, the program is just a static set of instructions, and the process is a set of instructions running in the system, the life cycle and other time concepts;
3) Concurrency: Between processes, can be executed alternately, improve the efficiency of program execution.
2. A thread is a sequential execution flow, which is part of a process, a process can have multiple threads, and the threads are characterized as follows:
1) cannot share memory between processes, but threads can share data in the same piece of memory;
2) The system creation process needs to reallocate system resources for the process, but the cost of creating threads is very small, so multi-task concurrency is more efficient than multi-process implementation.
3) The Java language built-in multi-threaded function support, rather than simply as the underlying operating system scheduling mode, Java encapsulates the operating system underlying scheduling, shielding the differences between different operating system scheduling. 、
3. Daemon Thread:
A daemon thread is a thread of a generic service that a user program provides in the background when it runs, such as a garbage collection thread for garbage collection. This type of thread is not an integral part of the user thread, but a "service thread" that serves the service. Based on this feature, the JVM exits when the user thread in the virtual machine exits the runtime and the daemon does not have a service object. In the JDK source code, this explains the daemon thread:
* Marks this thread as either a {@linkplain #isDaemon daemon} thread or a user thread. The Java Virtual machine exits if the only threads running is all daemon threads.
4. How threads are Created
There are two ways to create a thread: one is to implement the Runnable interface, then pass it to the constructor of thread, create a thread object, and inherit the thread class directly.
5. Thread Life cycle
When we create a new thread in a Java program, its state is new. When we call the thread's start () method, the state is changed to runnable. The thread scheduler allocates CPU time to threads in the runnable thread pool and changes their state to running, other thread states are waiting (when hibernating), Blocked (when the system calls the blocking Io method) and dead (run or call execution ends, Throws an exception, etc.).
6. Thread Priority
Each thread has a priority, in general, a high-priority thread will have priority at run time, but this depends on the implementation of the thread schedule, which is operating system-related (OS dependent). We can define the priority of threads, but this does not guarantee that high-priority threads will be executed before the low-priority thread. The thread priority is an int variable (from 1-10), 1 represents the lowest priority, and 10 represents the highest priority. However, the Win7 system has a priority of only 7 levels, not corresponding to the priority one by one in Java, so Java programs under Windows typically use only max_priority, norm_priority, and min_priority three levels of precedence.
7. Thread Scheduler (threads Scheduler) and time shards (slicing)
The thread scheduler is an operating system service that is responsible for allocating CPU time to threads in the runnable state. Once we create a thread and start it, its execution depends on the implementation of the thread scheduler. Time sharding refers to the process of allocating available CPU time to available runnable threads. The allocated CPU time can be based on the thread priority or the time the thread waits. Thread scheduling is not controlled by the Java Virtual machine, so it is better to control it by the application (i.e. don't let your program depend on the priority of the thread).
Small sense:
Think of a function in school, with websocket realization of Web instant communication, in fact, the server receives client messages, forwarding messages, stored messages can be implemented with multithreading technology to improve efficiency. Client-sent messages are stored in the background with a map object, opening up multiple threads, a thread to hold the data to the Map object, a user to forward the message to the online, one to persist the data to the database during idle time, and one to listen to the message sent by the client, which can greatly improve the message forwarding , storage efficiency. Of course, this may involve the read and write separation of the map object, and the lock mechanism can be used to ensure read-write consistency (next time, the implementation of the lock in Java concurrency is discussed in detail).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

2015.6.10 (Java concurrency)

Related Article

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.