Problem (i)---thread pool, lock, Stack, and HashMap related

Source: Internet
Author: User

One, thread pool:

Multithreading technology mainly solves the problem of multiple threads in the processor unit, which can significantly reduce the idle time of the processor unit and increase the throughput capacity of the processor unit.

Suppose a server takes a task to complete: T1 creates a thread time, T2 the time to execute a task in the thread, T3 destroys the threading time.

if: T1 + T3 is much larger than T2, you can use a thread pool to improve server performance.

A thread pool consists of the following four basic components:

1. Thread pool Manager (ThreadPool): Used to create and manage thread pools, including creating a thread pool, destroying the thread pool, adding new tasks;

2, worker thread (poolworker): Thread pool threads, in the absence of a task in the waiting state, you can cycle the execution of tasks;

3, Task interface (tasks): Each task must be implemented by the interface for the task of scheduling the execution of tasks, it mainly specifies the entry of the task, the completion of the task after the end of the task, the implementation of the mission status, etc.;

4. Task Queue (Taskqueue): Used to store tasks that are not processed. Provides a buffering mechanism.

Java provides four thread pools through executors, namely:

Newcachedthreadpool creates a cacheable thread pool that can flexibly reclaim idle threads if the thread pool length exceeds the processing needs, and creates a new thread if it is not recyclable.

Newfixedthreadpool creates a thread pool that controls the maximum number of concurrent threads, and the excess threads wait in the queue.

(The size of a fixed-length pool is best set based on system resources.) such as Runtime.getruntime (). Availableprocessors ())

Newscheduledthreadpool creates a fixed-line pool that supports timed and recurring task execution.

Newsinglethreadexecutor creates a single threaded thread pool that performs tasks with only a single worker thread, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority).

Second, Lock: object locks and class locks

1, the method is modified by syncronized, is to lock the object of the class, that is, when the object accesses the method, the current object will be locked, the same time the same object can no longer access the method, or the object's other syncronized decorated methods. Different objects can access the same syncronized method or other syncronized method at the same time, and there is no mutex between the two objects. Synchronized modifies a non-static method, the synchronized (this) usage of a synchronous code block, and the usage of synchronized (non-this object) locks the object, and the thread wants to execute the corresponding synchronization code, which needs to acquire the object lock.

2, synchronized modified static method and synchronous code block synchronized (class. Class) Usage lock is the class, the thread wants to execute the corresponding synchronization code, need to obtain a class lock. The method that is modified by static and syncronized is to lock the class, that is, only one class can access the method at the same time. Object locks do not have an exclusive relationship with class locks.

Third, heap and stack

stack stack memory is used to store the body and variable name of the function. It is a reference variable for some basic types of variables and objects. and when the storage of the stack memory reaches the maximum, Java will release some of the memory; The code in Java is executed in the body of the function, and each function body is placed in the stack memory, such as the main function. Adding the main function calls the other functions, such as Add (), then the storage inside the stack is the bottom of the Main,mian is the Add. The stack runs in first out, so it destroys add and then destroys main when it executes.
heap memory is used to store instances of new objects and arrays, such as the main function declares a people class per,people per; This per is stored in stack stack memory, The object entity that is behind the instance is in the heap heap memory. The stack stack memory stored in per-storage points to the address of the heap heap memory. Heap memory exists in order to better manage memory and implement garbage collection. The garbage collection mechanism removes the new people () instance from the heap heap in memory, freeing the memory when per no longer points to the instance in the heap heap memory. Four, hash Related:Hash algorithm: A hashing algorithm can map a binary value of any length to a shorter, fixed-length binary value. We'll turn this binary value into a hash value. Characteristics of the hash value:
* The hash value is a binary value;
* The hash value has a certain uniqueness;
* The hash value is extremely compact;
* To find 2 different inputs that generate the same hash value, it is not possible to do so within a certain time frame. Java defines an interface java.util.Map for the mappings in the data structure, which has four commonly used implementation classes, namely HashMap, Hashtable, Linkedhashmap and TreeMap; HASHMAP: It stores data based on the hashcode value of the key, which in most cases can be directly anchored to its value, and thus has a fast access speed, but the traversal order is indeterminate. HashMap allows a maximum of one record's key to be null, allowing multiple records to have a value of NULL. HashMap non-thread-safe, where multiple threads can write HashMap at any one time, may result in inconsistent data. If you need to meet thread safety, you can use the collections Synchronizedmap method to make HashMap a thread-safe capability, or use Concurrenthashmap. (Space change time) Hashtable:hashtable is a legacy class, many of the common functions of mapping are similar to HashMap, except that it comes from the dictionary class and is thread-safe, and only one thread can write Hashtable at any one time. Concurrency is inferior to Concurrenthashmap because Concurrenthashmap introduces a segmented lock. Hashtable is not recommended for use in new code, can be replaced with hashmap without thread safety, and can be replaced with Concurrenthashmap where thread safety is required.

Problem (i)---thread pool, lock, Stack, and HashMap related

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.