Java Interview Preparation __java

Source: Internet
Author: User
Tags aop prev reflection

1.ArryList, Vector, LinkedList difference

ArrayList: Using ArrayList provides better performance when you add data after a column of data instead of in front or in the middle, and you need to randomly access the elements in it; the methods in the vector are modified by adding synchronized, Vector is therefore a thread-safe container, but the performance is ArrayList poor.

LinkedList: When you add or delete data in front or in the middle of a column of data, and access the elements in order, you should use LinkedList.

The difference between 2.HashMap, HashTable, Linkedmap, TreeMap

HashMap: Fast access, allowed value NULL, thread synchronization not supported, no order. To sync, you can add Synchronizedmap or use Concurrenthashmap.

HashTable: The key or value is not allowed to be empty, the thread is synchronized, and the write speed is slow.

Linkedhashmap: is a subclass of HashMap that holds the insertion order of records, and is slower to traverse than HashMap.

TREEMAP: The default is the ascending sort of key keys.

3. The transaction

Https://zhidao.baidu.com/question/104479831.html

A transaction (Transaction) is a unit of concurrent control and is a user-defined sequence of operations. These operations are either done or not done, and are an indivisible unit of work. With transactions, SQL Server can bind a logically related set of operations together so that the server maintains the integrity of the data. It is mainly used for some programs which have higher integrity of the operation process. For example, the banking system, the user in the process of transferring the program error, but the transfer operation is not completed. Then the operation is returned.

Main purpose: To ensure the consistency and integrity of data.

features :

1. Atomicity: All operations on the database in a transaction are an integral sequence of operations, either doing all or none

2. Consistency: Data state remains consistent before and after a transaction is executed. Data is not compromised because of the execution of the transaction

3. Isolation: The execution of a thing, not interfered with by other affairs, that is, the thing that executes concurrently does not interfere with each other

4. Durability: Once a thing is submitted, its change to the database is permanent


How transactions are implemented: There are two ways to implement them: encoding; declarative transaction management methods


Declarative transaction management based on AOP technology is essentially: to intercept before and after the method is executed, and then to create and join the transaction before the target method starts, after the target method is executed, and the transaction is committed or rolled back according to the execution.


Declarative transaction management is implemented in two ways: based on XML configuration files, and @transaction annotations on another real business approach, applying transaction rules to business logic


A common transaction management configuration: Transaction Interceptor Transactioninterceptor and transaction automatic agent Beannameautoproxycreator


4. Facet oriented AOP

Cut-face programming, extract the public parts scattered in the program, make slice class, such advantage is, the code reusable, once involves the function of the need to change, as long as the code to modify the line, otherwise, you have to change everywhere, if only modify 1, 2 that can accept, in case there are 1000 places.
The bottom of AOP is the JDK dynamic proxy and Cglib proxy, which is to enhance the functionality of the class.
The most common AOP applications are in database connectivity and transaction processing.

AOP is the aspect-oriented programming, it is to add the unified function to the program through the proxy way, and solve some public problems.
Application of AOP: Spring declarative transactions.
The types of AOP notifications are: pre-notification, post-notification, surround-notification, exception-notification


5. How to communicate between two projects

① uses Apache's HttpClient method.

GET Request:

		String url = "...";
		HttpClient client = new HttpClient ();
		HttpMethod method=new getmethod ("url");
		Closeablehttpresponse response = Client.executemethod (method);
		httpentity entity = response.getentity ();
POST request:
		String url = "...";
		HttpClient client = new HttpClient ();
		Postmethod Postmethod = new Postmethod (URL);
		Create a parameter queue    
        list<namevaluepair> formparams = new arraylist<namevaluepair> ();  
        Formparams.add (New Basicnamevaluepair ("username", "admin"));  
        Formparams.add (New Basicnamevaluepair ("Password", "123456")); 
        Urlencodedformentity uefentity = new Urlencodedformentity (formparams, "UTF-8");  
        Httppost.setentity (uefentity);  
        SYSTEM.OUT.PRINTLN ("Executing request" + Httppost.geturi ());  
        Closeablehttpresponse response = Httpclient.execute (httppost);
        httpentity entity = response.getentity ();  


② uses the HttpURLConnection method under the java.net package that the JDK takes.

Garbled solution: When Sent: Conn.setrequestproperty ("CharSet", "UTF-8");

When received: reader = new BufferedReader (New InputStreamReader (Conn.getinputstream (), "UTF-8"));

6. Reflection mechanism

The reflection mechanism is in the running state, for any class, all the properties and methods of the class can be known, and any one of its methods and properties can be invoked for any object; This dynamically acquired information, and the function of the method that dynamically invokes the object, is called the reflection mechanism of the Java language.

What is reflection? When our program is running, we need to dynamically load some classes that might not have been used before, so we don't have to load them into the JVM, for example, our project is sometimes used in MySQL, sometimes with Oracle, we need to dynamically load the driver class according to the actual situation, this time the reflection is useful, suppose Com.dbtest.myqlconnection,com.dbtest.oracleconnection These two classes we want to use, this time our program is written more dynamic, through class TC = Class.forName (" Com.dbtest.myqlConnection "); let the JVM find and load the class on the server with its full class name, and if it is Oracle, the incoming parameter becomes another. At this point you can see the benefits of reflection, this dynamic is reflected in the characteristics of Java.


"Deep parsing Java Game Server development":

Look at the directory, there is no dry goods, large space to talk about the installation environment, there are many introduction framework.

This book from the game's industry analysis, Java technology, game logic, database technology, network theory, server technology, architecture analysis, system optimization and other aspects of the game server development has done a comprehensive analysis.



the difference between a Web server and a game server:

1. In the case of third party support, web background has a lot of mature third-party framework, developers do not need to care about the implementation of the underlying controller jump, only need one or several configuration files, can complete the core controller part, and developers only need to focus on the web's own business logic, the logic and framework integration can, Using the framework simplifies the control layer code on one hand, and realizes the layering of the business logic on the one hand. And the game backstage development, because the demand of each kind of game is very big, from the network layer, to the business logic layer, all aspects must be based on their own game needs to build their own framework, so it is difficult to have some common things can be distilled out of a mature framework, game background development basically need to build their own framework for their own.
2. From the business logic level, Web background basically the logic is the same, perhaps this set of systems, a little change, another set of systems can be used, and the game is different, each game has its own characteristics, according to the different needs of the planning to achieve different logic, but there will be some general modules, But the overall difference is still very big.
3. In terms of data persistence, web data is basically very regular, the relationship between the table and the table is very clear, and will not be much change, and the game of data, with a variety of changes in the data, and even the traditional relational database can not meet the needs of game data persistence, The game has a lot of state and data is required to save the server, I personally think that in the game development, NoSQL than relational database more practical.
4. From the communication layer, users in the Web are independent individuals, and the game is a multiplayer online game world, in this game world, players and players need to interact, which requires the server real-time to all online players broadcast message, which is very loss of server performance, in this regard, Game backend to do more than the web to do more processing, game server is an IO-intensive server type.


Multithreading

Create three threads, a thread print 10 times a,b thread print 10 times b,c thread Print 10 times C, require thread to run simultaneously, alternating print 10 times ABC. This problem can be easily solved with the object's Wait (), notify (). The code is as follows:

/** * @author Dreamsea * @time 2015.3.9 * * Package com.multithread.wait;     
    public class MyThreadPrinter2 implements Runnable {private String name;     
    Private Object prev;     
    
    Private Object self;     
        Private MyThreadPrinter2 (String name, Object prev, object self) {this.name = name;     
        This.prev = prev;     
    This.self = self;     
        @Override public void Run () {int count = 10;     
                    while (Count > 0) {synchronized (prev) {synchronized (self) {     
                    System.out.print (name);    
                      
                    count--;     
                Self.notify ();     
                try {prev.wait ();     
                catch (Interruptedexception e) {e.printstacktrace ();     
       }     }} public static void Main (string[] args) throws Exception {     
        Object A = new object ();     
        Object B = new Object ();     
        Object c = new Object ();     
        MyThreadPrinter2 pa = new MyThreadPrinter2 ("A", C, a);     
        MyThreadPrinter2 PB = new MyThreadPrinter2 ("B", A, b);     
             
             
        MyThreadPrinter2 pc = new MyThreadPrinter2 ("C", B, c);  
        New Thread (PA). Start ();  Thread.Sleep (100);  
        Make sure that new Thread (PB) is executed in order a, B, and C. Start ();    
        Thread.Sleep (100);     
        New Thread (PC). Start ();    
        Thread.Sleep (100);     }     
}


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.