What Java engineers should know about (Basic 01)

Source: Internet
Author: User

I am not CS trained, the time to learn Java is not long, but I prefer the industry. So I want to write some articles to record some of the knowledge that I want to learn more, and hope to share it all to discuss, progress together ~

I prefer the problem-driven learning form, so often look at some large companies face questions to understand their shortcomings, there are five questions to discuss with you. The questions discussed below are from--the Java face question you should know

1. Status of Java threads

This is a more common problem, often in a different face through the see, but often remember that probably forget.
Java threads can only be in the following six states at some point
1. New, the state that the thread has not started.
1. Running (RUNNABLE), a running thread is executing in a Java virtual machine, but it may be waiting for additional resources from the operating system (such as the processor).
1. In blocking (BLOCKED), a thread attempts to acquire a monitor lock (what the JVM specification implements, each object and class is logically associated with a monitor), but the lock is in the state of being held by another thread. For example, entering a synchronize block or other thread thread calls the Object.wait method.
1. Wait (waiting), a thread invokes the state after object.wait, Thread.Join, or Locksupport.park. For example, a thread called object.wait waits for the Object.notify or Object.notifyall method.
1. Timing Wait (timed_waiting) is the state of the time parameter, which is the relevant method that triggers the wait state.
1. Terminate (TERMINATED), thread is terminated (throws an uncaught exception) or normal exit state.

Reference: JDK8 's Thread source code, the relevant source code in 1742 lines start


2. Differences between processes and threads, how interprocess communicates, and how threads communicate with each other

I also did not go to the operating system of the class, only based on the search for information and their own understanding and discussion.
Processes and threads are based on a description of a CPU time period, and threads are based on further partitioning of the process. In modern OS, a process is the smallest unit of CPU resource allocation, and a thread is the smallest unit of CPU scheduling. Before a process, communication relies on system resources, and communication between threads relies on the methods provided by the JVM.
When a user releases a command to run a program, a process is generated. The same program can produce multiple processes (one-to-many relationships) to allow multiple users to run the same program at the same time without conflicting.
Wikipedia related explanation: The process requires some resources to complete the work, such as CPU usage time, memory, files, and I/O devices, and in sequential order, that is, each CPU core can only run one process at any time.
That multithreading can depend on multi-core execution, which means that multiple cores can run a process at the same time?
Processes and threads also seem to be related to the operating system, language implementation, and did not find very good information, welcome to discuss.


3. What is the data structure of HASHMAP? How it is implemented. The difference between the Hashtable,concurrenthashmap and the

HashMap is an array of node-based elements, node implements the Map.entry interface, and there are several key points to implement

    • When put, the corresponding hash value is calculated to map to the corresponding bucket.
    • If there are multiple elements in a bucket, the inner elements are constructed into linked lists to improve index efficiency.
    • If the internal linked list length exceeds the value of Treeify_threshold (the default is 8) it is converted to a red-black tree, which is converted to a linked list if the elements within the red-black tree are smaller than the untreeify_threshold size.
    • HashMap The default initial capacity (Default_initial_capacity) is 16, and the default load factor (Default_load_factor) is 0.75.
    • The load factor represents the degree of use of the table space by the HASHMAP, the more compact the space in the table when the load factor is high, the lower the retrieval efficiency, and the smaller the space occupied.
    • When the capacity is insufficient, the resize method expands by twice times by default before reaching the maximum value and does not scale up until the maximum value is reached.



Hashtable is a legacy class, many of the common functions of mapping and HashMap similar, the difference is that it is from the Dictionary class, and is thread-safe, at any time only one thread can write Hashtable, concurrency is not as good as 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.
Concurrenthashmap extensive use of Unsafe local methods, the use of segmented lock, reduce the lock granularity, improve the performance in a competitive situation. The guava cache is referenced to its implementation.
Resources:
Https://tech.meituan.com/java-hashmap.html
http://www.infoq.com/cn/articles/ConcurrentHashMap/
https://my.oschina.net/ovirtKg/blog/777520

4. What is the use of the index? How do I build an index?

The index is the function of the table of contents, for example, we want to quickly find the chapter of our interest when we turn the book through the index. If we don't have an index, we can only find what we want on a page-by-page. Different database implementations have different indexes, and MySQL often has b-tree (technically b+tree) and hash indexes.
In my personal experience, when I create an index, I use explain debugging and business-related statements, and when reading and writing are in one database, fewer indexes are needed to avoid insertion inefficiencies, because indexes are naturally updated once the index is inserted, and when the indexes are too long the update will have an impact on the entire process.
Avoid redundant indexes, such as the AB Index maintains the index of a, understand the structure of the b+tree is very intuitive to understand, MySQL will be optimized for search, but use the index is inefficient when you choose not to index.
Select the appropriate index order, in principle, high-selectivity indexes should be placed in front, but also consider the issue of index utilization. Of course, if the implementation of the read-write separation, then you can build the index freely!



Reference: High-performance MySQL third edition

5. How is the ArrayList realized, the difference between ArrayList and LinkedList? ArrayList how to achieve capacity expansion?

ArrayList is a data structure based on dynamic array implementation, and LinkedList is a data structure based on two-way linked list implementation. So ArrayList looks faster and LinkedList takes up more space. Since LinkedList is based on a linked list, and ArrayList needs to be dynamically expanded at insert time, insertions and deletions should theoretically be faster than ArrayList. But the actual test result is not so, because LinkedList's retrieval efficiency is too clumsy, as long as the capacity of 1/10 before the size of the ArrayList faster than.


Comparison of time complexity

Java Learning Exchange QQ Group: 589809992 prohibit small talk, non-happy do not enter!

What Java engineers should know about (basic)

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.