Rough finishing of Java face questions

Source: Internet
Author: User

1. Garbage collection

is the reclaimed free heap space

Recycle only if the CPU is idle and the heap is running out of space

2.threadlocal provides a copy of the thread's variables, and each thread runs just to update the copy.

Threadlocal can address thread insecurity, such as SimpleDateFormat

Both threadlocal and synchonized are used to solve multi-threaded concurrent access.

But there is an essential difference between threadlocal and synchronized. Synchronized is the mechanism by which a lock is used so that a variable or block of code can be accessed by only one thread at a time.

Instead, Threadlocal provides a copy of the variable for each thread, so that each thread accesses the same object at a certain time, isolating data sharing from multiple threads.

Synchronized, in contrast, is used to gain data sharing when communicating between multiple threads.

3.volatile function

Relates to the visibility of the memory model: When a shared variable is modified by volatile, it guarantees that the modified value is immediately updated to main memory and that it will read the new value when another thread needs to read it. (visibility)

Visibility can also be ensured through synchronized and lock

command reordering is prohibited.

The A.volatile keyword provides a lock-free mechanism for access to domain variables B. Using a volatile modifier domain is equivalent to telling a virtual machine that the domain might be updated by another thread to C.

Therefore, each time the domain is used, it is recalculated, instead of using the value in the Register D.volatile does not provide any atomic operation, nor can it be used to decorate a variable of the final type

4.JVM principle

The JVM compiler compiles the Java file into a. Class bytecode program, and the JVM's execution engine translates the bytecode program into machine code that can be recognized on different platforms, and then it can be executed.

5.JVM Architecture

A. Stack = = = Stack Frame save run-time state, troubleshoot how the program performs issues

Stack is stored in the method of the entry, operating environment, etc.

B. Heap

Heap holds object instances and arrays

C. program counter

Multithreading-related

D. Method area

Store method-related access modifier method name

E. String constant pool

F. Local method Zone native

6. Class Loader ClassLoader is responsible for loading classes into the JVM for calls between programs

A.bootstrap ClassLoader: Start class loader

Load the JDK core class library, such as Rt.jar

B.app ClassLoader: System class Loader

Load all class libraries on which the application classpath depends

C.extend classloader: Extension class Loader

All jars under java_home/jre/lib/ext/are loaded by default.

You can customize the class loader

7.Synchronized and Lock differences

Synchronized: Pessimistic lock, if locked, if not released, then will always be locked

Lock: The middle will be interrupted, will not be waiting to be unlocked, applicable to a thread during the wait for the control of this lock need to be interrupted.

8.string,stringbuffer and StringBuilder

String is not variable, although you can use + splicing, but so each splicing is the operation of new objects, the GC is also the object before recycling. So the least efficient

StringBuffer Thread-Safe

StringBuilder thread is unsafe but most efficient

9.Volatile and synchronized four different points:

A. Different granularity, the former for variables, the latter lock objects and classes

B. Syn blocking, volatile threads not blocking

C. SYN guarantees three major characteristics, volatile does not guarantee atomicity

d. SYN compiler optimizations, volatile does not optimize volatile have two characteristics:

10. Thread Pool Executorservice

Reduce resource consumption. Reduce the consumption caused by thread creation and destruction by reusing created threads

Newfixedthreadpool ()

Newcachedtheadpool ()

11. Class loader working mechanism:

1. Load: Import the Java binaries into the JVM and generate the class file.

2. Connect: a) Check: Verify the correctness of loading class file data B) Prepare: Allocate storage space for static variables of Class C) parsing: Turn a symbolic reference into a direct reference

3: Initialize: Initializes the static variables of the class, static methods, and static blocks of code.

Parental delegation Model: The class loader receives a class load request, first delegating the request to the parent class loader to complete the user-defined loader--application loader, extension ClassLoader, and the launch class loader.

12.SpringMVC Operating principle

Client request submitted to Dispatcherservlet

The handlermapping is queried by the Dispatcherservlet controller and is found and distributed to the specified controller.

After the controller invokes the business logic processing, it returns Modelandview

Dispatcherservlet query One or more Viewresoler view resolvers to find the Modelandview specified view

The view is responsible for displaying the results to the client

13.Session and Cookies: Cookies allow the server to track each client's access, but each client's access must be passed back to these cookies, if a lot of cookies, the invisible increase in the client and the service side of the data transfer volume,

And the session is a good solution to this problem, the same client each time and server interaction, the data store through the session to the server,

Instead of returning all of the cookie values each time, it returns an ID, the unique ID generated by each client's first access to the server,

The client simply returns this ID, which is usually a cookie named Jsessionid. This allows the server to use this ID to retrieve the KV value stored on the server.

14. Adapter Mode : To adapt one interface to another interface, Java I/O inputstreamreader the reader class to inputstream, thus enabling the byte stream to be quasi-swapped.

Decorator Mode: Keep the original interface, enhance the original function.

FileInputStream implements all the interfaces of the InputStream, Bufferedinputstreams inherits from FileInputStream is the specific adorner implementation,

The content read by InputStream is saved in memory while improving read performance.

15.Spring Transaction Configuration Method :

1. Pointcut information for locating business class methods that implement a facet of a thing

2. Transaction properties that control transaction behavior, including the object isolation level, transaction propagation behavior, time-out, and rollback rules.

Spring uses Aop/tx Schema namespaces and @transaction annotation techniques to make declarative things configuration.

16.Mybatis

Each MyBatis application is centered on an instance of a Sqlsessionfactory object. First, the configuration file is read through resource with a byte stream,

The sqlsessionfactory is then created through the Sqlsessionfactorybuilder (). Build method.

The Sqlsessionfactory.opensession () method is then used to create a sqlsession for each database transaction service.

Went through the MyBatis initialization –> create sqlsession–> Run SQL statement, return the results of the three procedures

17. Talk about the difference between Servlet,filter,listener and Intecepter

A. A servlet is an application that, like action, can dynamically return a page, a Web application that receives parameters, invokes a service, and returns the role of a page. Doget () and Dopost ()

The B.filter is a servlet-enhanced version of filter that pre-processes the user request, then gives the request to the servlet for preprocessing and generates the response, and finally the filter then handles the server response

Write a filter class to implement the filter class, configure into the Web. xml file, DoFilter ()

It's mostly coded to do the filtering.

Based on a function callback chain, dependent on the servlet container, which works on all requests (for example, pictures)

C.listener Listener

D.intecepter Interceptor similar to filter

Write into the Spring.xml file

Based on reflection, not dependent on the servlet container, only works on action

Check if you are logged in,

18. Reflection

Reflection is the mapping of the various components of Java into the corresponding Java classes. is a feature of the dynamic language that Java embodies

19.java value passing and reference passing

In Java, the method pass is the value of the transfer, that is, the copy, does not change the value of

20. If the synchronized keyword satisfies the user's needs, use synchronized because it simplifies the code .

If you need more advanced functionality, use the Reentrantlock class, and pay attention to releasing the lock in time, otherwise a deadlock will occur, usually releasing the lock in the finally code.

Threadlocal and synchronization mechanisms are all designed to solve the problem of access violation of the same variable in multi-threading B. The former adopts the method of "space-changing Time", which adopts the way of "time-changing space".

Volatile does not guarantee atomic operation, so volatile is not a substitute for synchronized. In addition, volatile organizes the compiler to optimize the code, so it does not apply if it is not being used.

The principle is that every time a thread accesses a volatile modified variable, it is read from memory, not read in the cache, so each thread accesses the same variable value. This guarantees the synchronization.

http://www.jianshu.com/p/e513dea5492b

21.futureTask have you used it?

Futuretask represents a task for an asynchronous operation.

Futuretask inside can pass in a specific implementation class of callable, can wait for the result of the task of this asynchronous operation to obtain, judge whether has completed, cancels the task and so on operation.

Of course, because Futuretask is also an implementation class for the Runnable interface, Futuretask can also be put into the thread pool.

Before doing the project, import Excel done.

22. How to wake up a blocked thread

If the thread is blocked because it calls the wait (), sleep (), or join () method, it can be disconnected and awakened by throwing interruptedexception;

If the thread encounters IO blocking, it is powerless because IO is implemented by the operating system, and Java code has no way of directly contacting the operating system.

What is the role of 23.thread.sleep (0)

Because Java employs a preemptive thread scheduling algorithm, it is possible for a thread to get CPU control often, in order for some of the lower-priority threads to gain control of the CPU,

You can use Thread.Sleep (0) To manually trigger the operation of the operating system to allocate time slices, which is also an operation to balance CPU control.

24. Optimistic lock : Just like its name, optimistic locking holds that competition does not always occur, so it does not need to hold locks, as it is optimistic about the thread-safety issues that arise from concurrent operations

Compare-Set these two actions as an atomic operation attempt to modify the in-memory variables, if the failure indicates a conflict, then there should be a corresponding retry logic.

(2) Pessimistic lock: Still like its name, for the thread security problem caused by concurrency, pessimistic locks believe that competition always occurs, so each time the operation of a resource,

Will hold an exclusive lock, just like synchronized, regardless of 3,721, directly on the lock on the operation of resources.

25. Collection Class: Hashmap,hashtable

HashMap: Thread is not secure

Hashtable: Thread safety is similar to Concurrenthashmap

Vector, Hashtable, Properties and Stack are all synchronous (synchronized) classes, so they are thread-safe and can be used in multithreaded environments.

The Java 1.5 Concurrent API introduces a number of concurrent classes that allow the collection to be modified during the iteration, and because they will clone a collection at iteration time, it is also safe in a multithreaded environment.

26.spring face question

1). The spring core is actually control inversion and dependency injection,

Control inversion is the instantiation of the class of control to spring to give us the completion of the beanfactory to do

2). Spring scope

Singletion: Single case

Prototype: Create an instance for each bean

3). Next Spring

First spring is a big concept, spring from the very beginning of a library to now a series,

The most important of these are the spring Framework, Spring Data, Spring Security, spring batch, etc.

And the quick framework Spring Boot, the most important of which is the spring framework, which includes IOC, AOP, MVC, and testing.

The first thing to understand is what is spring's core idea? The most central concept of spring's entire series is IOC, AOP, what IOC and AOP do not unfold,

In short, the responsibility for the object creation process is given to the container, which manages the object's birth and death,

The object creation process is deferred from compile time to runtime, which is loaded through configuration, which solves the need to select a specific implementation without compiling later.

is actually the object-oriented core concept, for interface programming. The IOC started out as a factory, plus dependency management,

In this way, a system is created from the original new to the configuration assembly, internal through injection to resolve the dependency,

Plug and Play as long as the interface protocol is satisfied. Through the IOC, AOP actually forms a routine through which the integration of the system is accomplished.

So spring does not say that it is writing an ORM, but that it uses a unified repertoire to complete the integration of multiple ORM,

This is the basis for spring to do more and more, slowly forming the spring way, in fact, this is spring's most valuable place.

27. Next Springboot

Spring is a "engine"

Spring MVC is an MVC framework based on spring

Spring Boot is a set of rapid development integration packages based on SPRING4 conditional registration

Spring initially decoupled the application components with "Factory mode" (DI) and "proxy mode" (AOP). Everyone thought it was good, so we had an MVC framework (some Spring decoupled components) in this pattern.

To develop Web applications (SPRINGMVC). Then found every development to make a lot of reliance, write a lot of boilerplate code is very troublesome, so a few lazy integration package (starter), this set is Spring Boot.

For decades, Spring's efforts have been to reduce complexity, decoupling, and less code.

My feeling is that complexity does not come out of thin air, and it does not disappear from thin air, but moves from one place to another.

While mastering spring can reduce a lot of unnecessary work, mastering spring itself becomes a complex thing.

Spring's XML, annotated configuration, EL expression, the DSL, complicates a lot of simple things and brings more trouble when spring Boot auto-configuration fails.

28. Distributed Lock:

Distributed locks: is a way to control the synchronization of shared resources between distributed systems. In distributed systems, it is often necessary to coordinate their actions. If one or a group of resources is shared between different systems or different hosts of the same system,

When accessing these resources, mutual exclusion is often required to prevent interference with each other to ensure consistency, in which case a distributed lock is required.

I've been using Redis for the moment.

Zookeeper implementation has not been used

Java Communication Group 669823128

Rough finishing of Java face questions

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.