A summary of the classic Java surface questions of Ali years

Source: Internet
Author: User
Tags mysql injection sorts volatile

Characteristics of volatile:

A. Prohibit command rearrangement (with exception)
B, Visibility

Volatile memory Semantics:

When a volatile variable is written, jmm flushes the shared variable value in the local memory corresponding to the thread to the main memory.

When reading a volatile variable, JMM will set the local memory of the thread to be invalid, and the thread will then read the shared variable from main memory.

Re-ordering of volatile

1, when the second operation for volatile write operations, regardless of the first operation is what (normal read or write or volatile read and write), can not be reordered. This rule ensures that all operations before the volatile write are not reordered to volatile;

2, when the first operation is a volatile read operation, no matter what the second operation is, it cannot be reordered. This rule ensures that all operations after the volatile read are not reordered until volatile;

3. When the first operation is a volatile write operation, the second operation is a volatile read operation and cannot be reordered.

This rule consists of the preceding two rules: two volatile variable operations cannot be reordered;

In addition to the above three cases, it is possible to reorder.

Like what:

1, the first operation is the normal variable read/write, the second is the reading of volatile variables;
2, the first operation is the write of volatile variables, the second is a common variable read/write;

Memory Barrier/Memory Fence

A memory barrier (memory Barrier, or sometimes called an Fence) is a CPU instruction that controls reordering and memory visibility issues under certain conditions. The Java compiler also prohibits reordering based on the rules of the memory barrier. (that is, a technique that allows memory states in a CPU processing unit to be visible to other processing units.) )

Memory barriers can be divided into the following types:

Loadload Barrier: For such a statement Load1; Loadload; Load2, the data to be read by the LOAD1 is guaranteed to be read before Load2 and subsequent read operations are accessed.

Storestore Barrier: For such a statement Store1; Storestore; Store2, the Store1 write operation is guaranteed to be visible to other processors before Store2 and subsequent write operations are performed.

Loadstore Barrier: For such a statement Load1; Loadstore; Store2, the data to be read is guaranteed to be read by the Load1 before Store2 and subsequent write operations are brushed out.

Storeload Barrier: For such a statement Store1; Storeload; Load2, ensure that Store1 writes are visible to all processors before Load2 and all subsequent read operations are performed. Its overhead is the largest of the four types of barriers.

In most implementations of the processor, this barrier is a universal barrier that combines the functions of three other memory barriers.

Memory barriers prevent the CPU from using optimization techniques to reduce memory operation latency and must consider the resulting performance penalty. To achieve optimal performance, it is best to modularize the problem to be solved so that the processor can perform the task by unit and then put all the required memory barriers on the boundary of the task cell. This method allows the processor to execute a task unit without restriction. Another benefit of a reasonable combination of memory barriers is that the buffer will be less expensive after the first brush, because no additional work is required to refill the buffer.

Happens-before Principles

How is Java implemented across platforms?

How is cross-platform implemented? This will refer to the Java Virtual machine (Java VM, or JVM).

The JVM is also a software that has different versions of different platforms. We write the Java source code, compiled will generate a. class file, called a bytecode file. The Java Virtual machine is responsible for translating bytecode files into machine code under a specific platform and then running. That is, as long as the corresponding JVM is installed on different platforms, you can run the bytecode file and run the Java program we wrote.

In this process, the Java program that we write does not make any changes, just through the JVM, the "middle layer" can be run on different platforms, and really achieve the "compile once, run everywhere" purpose.

JVM is a "bridge", is a "middleware", is the key to achieve cross-platform, Java code is first compiled into bytecode files, and then by the JVM to translate bytecode files into machine language, so as to achieve the purpose of running Java programs.

Note: The result of compiling is not to generate machine code, but to generate bytecode, bytecode can not run directly, must be translated into machine code by the JVM to run. The bytecode generated under different platforms is the same, but the machine code translated by the JVM is not the same.

Therefore, running the Java program must have the support of the JVM, because the result of the compilation is not machine code, it must be re-translated by the JVM to execute. Even if you package a Java program as an executable file (for example,. exe), you still need the JVM's support.

Note: The cross-platform is a Java program, not a JVM. The JVM is developed in C/s + +, is compiled machine code, not cross-platform, different platforms need to install different versions of the JVM.

Mobile phone sweep QR Code login is how to achieve?

Reference: http://www.jianshu.com/p/7f072ac61763

What are the states of Java threads and how are these states transformed?

    1. NEW: A new Thread object was created.

    2. Runnable: After the thread object is created, other threads (such as the main thread) call the object's start () method. The thread in this state is in a pool of running threads, waiting for the thread to be dispatched to get the CPU right.

    3. Run (running): the thread that can run the state (runnable) obtains the CPU time slice (TimeSlice) and executes the program code.

    4. Block: Blocking state refers to a thread that, for some reason, abandons the use of the CPU, letting the CPU TimeSlice and temporarily stop running. Until the thread enters the operational (runnable) state, there is a chance to get the CPU TimeSlice again to the run (running) state. There are three types of blocking:

(i). Wait for blocking: the thread that is running (running) executes the o.wait () method, and the JVM puts the thread into the waiting queue (the waitting queues).

(b). Synchronous blocking: When a thread running (running) acquires a synchronization lock on an object, if the synchronization lock is occupied by another thread, the JVM puts the thread into the lock pool.

(iii). Other blocking: The thread that runs (running) executes the thread.sleep (long ms) or T.join () method, or when an I/O request is made, the JVM will place the thread in a blocked state. When the sleep () state time-out, join () waits for the thread to terminate or times out, or the I/O process is complete, the thread is re-transferred to the operational (runnable) state.

    1. Death (dead): The thread Run (), the main () method finishes executing, or the run () method exits because of an exception, the thread ends the life cycle. The thread of death cannot be resurrected again.

The difference between a list interface, a set interface, and a map interface

Reference: http://blog.csdn.net/zcg_java/article/details/43232251

What is the difference between a cookie and a session?

Reference: http://blog.csdn.net/axin66ok/article/details/6175522

An explanation of the equals and Hashcode methods in Java

Reference: http://www.cnblogs.com/Qian123/p/5703507.html

CAS algorithm in Java

Reference: http://www.kancloud.cn/seaboat/java-concurrent/117870

Timsort principle

Reference: Http://suo.im/532OHc

What is the difference between comparable and comparator?

Reference: http://www.cnblogs.com/szlbm/p/5504634.html

Handwriting Singleton mode (thread safe)

Reference: http://www.cnblogs.com/hupp/p/4487521.html

How do you communicate between Java threads?

Reference 1:http://www.cnblogs.com/mengdd/archive/2013/02/20/2917956.html

Reference 2:http://www.jb51.net/article/84213.htm

Memory generational improvements for JAVA8

Reference: http://blog.csdn.net/chlu113/article/details/51890469

Understanding of the Java memory model and its role in concurrency?

Reference: http://www.cnblogs.com/_popc/p/6096517.html

Arrays and collections different implementation principles for sort?

1, Arrays.sort ()
The algorithm is a fast, tuned ordering that provides N*log (N) performance on many datasets, which results in lower two performance for other fast sorts.

2, Collections.sort ()
The algorithm is a modified merge sort algorithm (where, if the highest element in the lower sub-list has a high benefit, the lowest element in the sub-list is ignored). This algorithm provides guaranteed performance of N*log (N), which dumps the specified list into an array and then sorts the array, iterating over the list of each element at the corresponding position in the reset arrays.

Common methods of object in Java

1. Clone ()
2, Equals ()
3. Finalize ()
4, GetClass ()
5, Hashcode ()
6, notify ()
7, Notifyall ()
8. ToString ()

For the understanding of polymorphism in Java

The so-called polymorphism refers to the specific type of reference variable defined in the program and the method call issued by the reference variable is not determined during programming, but is determined during the program's run, that is, the instance object that refers to which class the reference variable will point to, and how the method call from that reference variable is implemented in the class. Must be determined by the time the program is running. Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism.

Polymorphic definition: means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)

There are three prerequisites for implementing polymorphism in Java: Inheritance, overrides, and parent-class references to child-class objects.

Inheritance: Subclasses and parent classes that have inheritance relationships must exist in polymorphism.

Rewrite: Subclasses redefine some methods in the parent class and call the methods of the subclasses when they are called.

The parent class reference points to the subclass object: In polymorphism, a reference to a subclass needs to be assigned to the parent class object, so that the reference can have the ability to invoke methods and subclasses of the parent class.

The technique of implementing polymorphism is called dynamic binding, which is the actual type of the referenced object that is judged during execution, and its corresponding method is called according to its actual type.

The role of polymorphism: to eliminate the coupling relationship between types.

Session mechanism?

Reference: http://justsee.iteye.com/blog/1570652

What is the serialization and deserialization of Java? Why do I need serialization and deserialization? How do I implement the serialization and deserialization of Java?

Reference: http://blog.csdn.net/wangloveall/article/details/7992448/

Spring AOP Implementation principle?

Reference: http://blog.csdn.net/moreevan/article/details/11977115/

How does the Servlet work?

Reference: http://www.ibm.com/developerworks/cn/java/j-lo-servlet/

What is the difference between Java NiO and IO?

Reference: http://www.jb51.net/article/50621.htm

What is the difference between heap memory and stack memory in Java?

Reference: http://www.cnblogs.com/whgw/archive/2011/09/29/2194997.html

Reflection speaks, mainly the concept, where the reflection mechanism is needed, the performance of reflection, how to optimize?

Definition of reflection mechanism:

is in the running state, for any one class, can know all the properties and methods of this class, any object can be invoked through the reflection mechanism of a class of any method, the dynamic acquisition of class information and the function of dynamic call class object method is called Java reflection mechanism.

The effect of reflection:

1. Dynamically create an instance of the class, bind the class to an existing object, or get the type from an existing object.

2. The application needs to load a specific class from a particular assembly at run time.

How to ensure RESTful API security?

Reference: http://blog.csdn.net/ywk253100/article/details/25654101

How to prevent MySQL injection?

Reference: http://www.jb51.net/article/87948.htm

I have a public number that often shares some of the dry stuff related to Java technology, and if you like my share, you can use the search for "Java leader" or "Javatuanzhang" for attention.

A summary of the classic Java surface questions of Ali years

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.