Java Interview Finishing

Source: Internet
Author: User
Tags ack

The difference between IO and NIO

This is a very common problem, if only to answer the difference between IO and NIO can only be passed. I personally feel that we should answer the following questions:

1), Io Introduction,

2), TCP three handshake, because this is also one of the differences between the two,

3), NiO Introduction,

IO: (Block-io) is a mode of communication that blocks synchronization . is a more traditional means of communication, Simple mode , easy to use . However, the concurrent processing power is low (each connection creates a new thread for link processing), the communication takes time (TCP three handshake), and depends on the speed of the network.

TCP Three-time handshake :

First handshake: Establishes the connection, the client sends the SYN packet to the server side, waits for the server to confirm

Second handshake: The server receives a SYN packet from the client, returns an ACK and a SYN packet to the client, waits for the client to confirm

Third handshake: The client receives the Ack+ayn packet from the server, sends an ACK packet to the server, and the connection is established successfully

NIO: (New io/non-block IO) is a non-blocking synchronous communication mode. Communication between client and server via channel (avoid the overhead of using three handshake for TCP to establish a connection). NiO reads and writes to the channel. These channel will be registered on the selector multiplexer. The selector polls the channel through a thread without stopping. Find out which channel is ready to perform IO operations.

Reference documents

Io,bio,aio differences

TCP Three-time handshake detailed description

The difference between Java heap and stack

1), heap and stack in common,

2), heap and stack differences,

3), data sharing between threads,

Java memory is divided into two categories, one is heap memory, the other is stack memory

Heap: used primarily to store instantiated objects, arrays. The memory space is dynamically allocated by the JVM. A JVM has only one heap of memory, and threads can share data.

Stacks: Mainly used to store local variables and object reference variables, each thread will have a separate stack space, so the threads are not sharing data.

Now that the memory is answered, you can actually answer the memory overflow situation.

Talk about memory overflow

1), first answer what is memory overflow,

2), and then talk about the causes of memory overflow,

3), finally put forward a number of memory overflow solutions,

Memory overflow: The actual memory used by the program is greater than the memory set by the virtual machine

Cause of overflow:

1), Virtual machine configuration parameter settings are unreasonable

2), there is a dead loop in the code or a large number of duplicate object entities are generated

3), data flow if there is no retreat can easily lead to

4), the amount of data loaded in memory is too large

5), a large amount of garbage can not be recycled by the JVM

Solution:

1), the most direct, the least responsible solution is to modify the JVM startup parameters

2), the correct process is to check the error log, find OutOfMemory error causes, and then modify the bug

JVM garbage collection mechanism

1), first answer what the JVM thinks of the garbage,

2), there are commonly used garbage collection algorithms,

3), there are commonly used garbage collector,

The JVM thinks that objects that are not being used are garbage and need to be removed from memory.

Commonly used garbage collection algorithms are: Reference calculation method, Mark clear method, tag compression method, copy algorithm, generational, partition thought

Reference calculation Method : Ancient algorithm, the object is quoted when added one, the reference break minus one, if zero is garbage collection. Frequent plus and minus operation performance is low.

tag cleanup : "Mark" the Trash first ( traverse all GC Roots, then mark all GC Roots objects as surviving objects ), then unify "clean up". The memory space after cleanup is not continuous and performance is not high.

tag Compression : A compression function based on the mark sweep method.

Replication Algorithm : Memory is divided into two sizes of the same size, a two block, when using a memory, a live object in the memory will be copied to B, and then empty all the objects in memory a. Using B memory is the same. The downside is that the memory gets smaller.

Generational thinking : The new generation, the old age (objects in the Cenozoic that survived the frequent GC). New generation because of low survival rate, need to replicate fewer objects, the proposed replication algorithm. The old age has a high survival rate and fewer objects to clean up, so it is recommended to use the marker compression method.

Partitioning idea : divides the entire memory into multiple independent spaces. Improve system performance by garbage collection in each separate space.

Serial Garbage collector: Using single thread for garbage collection

Parallel Garbage collector: Use multithreading for garbage collection with higher performance requirements

CMS retract : Concurrent collector, faster than parallel, when more resources are consumed, minimizing system downtime

G1 : The new collector provided after JDK1.7 is a tag-compression-based algorithm that is characterized by the entire Java heap

Reference Documentation:

JVM Series Blog: 52264043

Lock and synchronized differences: 64910926?locationnum=11&fps=1

HashMap Bottom-level implementation

1), simply talk about the bottom realization of hashmap

2), HashMap and Hashtable differences

3), working thread safe hashmap Use

The bottom of the hashmap is the data structure of the linked list hash, which is the structure of the array and the linked list. The position of the array is determined by calculating the hash value of the key, and if the position already has a value, it is added after the element to form a linked list. Each entity store hash,key,value,next four values. In the search, the position of the array is found by hash, and then the linked list is traversed through key until it is found. After jdk1.8, if the list length exceeds the threshold, the linked list will be converted to red-black trees to improve the retrieval efficiency. The red-black tree is self-balancing to find the binary tree, which solves the imbalance caused by multiple insertions of the two-fork tree into the new node. When a red black tree is inserted into a new node, it will satisfy its own rules through [color change] and [selection], and refer to the document connection for details.

Hashmap,hashtable,linkedhashmap differences

1), HashMap is not thread-safe, Hashtable is thread-safe, internal through synchronized modified lock, its poor performance

2), HashMap allow a key for null,hashtable not allowed

3), the initial capacity of the HashMap is 16, while the initial capacity of Hashtable is 11

4), the work of the use of HashMap high frequency, if you want to order the value of the use of linkedhashmap, if the consideration of thread safety CONCURRENTHASHMAP,CONCURRENTHASHMAP use segmented locking performance than Hashtable.

Reference Documentation:

ArrayList and LinkedList differences, Java common List collection usage scenario analysis

HashMap principle of implementation: 51588156

The difference between HashMap and Hashtable: https://www.cnblogs.com/aspirant/p/6856487.html

What is a red-black tree: http://www.sohu.com/a/201923614_466939

Java single-instance mode implementation

1), what is a singleton mode

2), a common singleton pattern has those

3), write one of the

Singleton mode: A class has only one instance, and it instantiates itself and provides this instance to the system as a whole.

Common singleton modes are: Lazy mode, a hungry man mode, static class internal loading

Thread-Safe Static class internal loadingPublicClass singletondemo{PrivateStaticclass  singletonholder{private  static singletondemo instance = new singletondemo (); } public span class= "DT" >static Singletondemo getinstance () {return SingletonHolder. instance;}              

Reference Documentation:

Https://www.cnblogs.com/cielosun/p/6582333.html

Talk about the MVC pattern

When I heard the question, I was happy and simple, but I didn't know how to answer it when I was interviewing.

Mvc:model,view,controller.

Model: Modeling layer, responsible for implementing business logic, Operation database

View: Views layer, responsible for page display

Controller: Control layer, responsible for processing requests

We understand through the actual framework,

SPRINGMVC, it implements the MVC design pattern of the web framework. Requests sent by the user are mapped to the corresponding handler via the front-end controller (Dispatcherservlet), based on the URL. And this handler can be modified with annotations @requestmapping, and the class of this method can be decorated with annotations controller to show that the class is a control layer. You can invoke the interface inside the method to complete the operation of the business logic and modify the data, placing the returned results in the Modelandview variable. Dispatcherservlet the path of the page back through the view parser, and puts the data into scope and renders it to the user. The interface that implements the business logic can be understood as the model layer, and the class that processes the request can be understood as the controller layer, and the page is the view layer.

When it comes to SPRINGMVC, there is no less Struts2,

Struts2 is also a Web application framework based on the MVC design pattern, in which the request for conformance is given to the servlet in the Web. xml file, which The servlet then references the Struts-config.xml file to find the corresponding action method, which is associated to the corresponding page when the execution is complete. SPRINGMVC is a lot simpler than Struts2.

Let's talk briefly about Hibernate,mybatis,springdata.

We can talk about spring again.

and design principles.

The problem of partial application

Because of the time and energy of the situation, here only to do simple collation of the problem.

How is Redis transaction done?

A: Redis's transaction is done mainly through multi (open transaction), EXEC (COMMIT transaction), watch (monitor version number) several commands.

Second, how to achieve snapping anti-oversold function?

A: snapping, seconds to kill the scene to ensure that the user experience, but also to prevent oversold. In fact, it is very simple, reduce the database calls, 1000 commodity time-limit snapping, is 1000 numbers minus one. Thread safety is ensured by locking. Use caching to improve efficiency.

Third, how to design data query interface to ensure the security and performance of data?

A: Security: Server Interface token authentication; query parameter length check; query parameter format check (avoid SQL injection);

Performance: index optimization; ehcache local cache; Redis cache warming up;

Iv. How does nginx do permission interception?

A: Nginx and LUA can implement permission interception

V. 2*16 how to calculate the fastest results?

A: Move 4-bit to the left

Vi. How does the schema set 10 Boolean permissions in a field?

Answer: Use the Int field, respectively, with 0,1 for false and true

Seven: Why not use MySQL to do distributed locks?

A: I will answer from the performance, using Redis to do distributed lock, is to determine whether the key value exists, there is a lock. Use zookeeper to do distributed lock, is to judge whether the temporary node exists, there is a lock. A deadlock occurs when MySQL does a distributed lock with high concurrency.

Eight: How can distributed systems avoid repeated consumption of messages?

A: Avoid repeating the sending angle from the producer: persist the messages sent by the producer to the database, and if the same information is not saved, the message service sends the message to the consumer in the database after the transaction of the survivor service is complete.
From consumers to avoid duplication of consumption angle: consumer services may be a cluster, to consider idempotent. Query and delete is a natural power operation, so we have to decide before updating and creating, whether it already exists, whether it has been updated or not.

Nine, what do you know about our company?
For:.......

Turn from

Java Interview Finishing

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.