Java Developer written interview 12 questions per day (2)

Source: Internet
Author: User
Tags thread class visibility volatile wrapper

1.Volatile and synchronized different points
(1), volatile can only be used for variables, the use of a small range. Synchronized can be used in variables, methods, classes, synchronous code blocks, etc., the scope of use is relatively wide.
(2), volatile can only guarantee visibility and order, can not guarantee atomicity. The visibility, order and atomicity of synchronized can be certified.
(3), volatile does not cause thread blocking. Synchronized may cause thread blocking.

Atomicity, visibility, and ordering in Java Multi-threading
(1), atomicity: Refers to a thread of multiple operations is a whole, can not be split, either do not execute, or all the execution, the middle can not be interrupted.
(2), Visibility: refers to the visibility between threads, is the result of a thread modification, other threads can immediately know.
(3), ordering: In order to improve the efficiency of execution, the compiler and processor in Java can reorder the instructions, reordering will affect the correctness of multi-threaded concurrency, ordering is to ensure that the order is not reordered (to ensure that the execution sequence of thread operations).

The role of the volatile keyword
In fact, the role of the volatile keyword is to ensure visibility and ordering (not guaranteed atomicity), if a shared variable is modified by the volatile keyword, then if a thread modifies the shared variable, the other threads are immediately known. Why is that so? For example, if thread a modifies its own copy of the shared variable, then if the shared variable is not modified by the volatile, then this modification does not necessarily flush the result of the modification into main memory immediately, if B goes to main memory to read the value of the shared variable, then this value is not the value before a modification. If the shared variable is modified by volatile, then the result of this modification will be forced to flush into main memory immediately, if at this time B go to main memory to read the value of shared variable, then this value is modified by a value.
Volatile can prohibit the order reordering, in order to reorder optimization, the instructions before the volatile variable can not be executed after volatile, and after the volatile instructions can not be executed before volatile, so it guarantees the order.

The role of the SYNCHRONIZED keyword
Synchronized provides the concept of a synchronous lock, the synchronized-modified code snippet prevents execution by multiple threads at the same time, and a thread must execute the synchronized-decorated code snippet before the other threads can begin executing the code.
Because synchronized guarantees that at the same time only one thread can execute a synchronous block of code, the execution of a synchronous block of code is equivalent to a single-threaded operation, so the visibility, atomicity, and ordering of threads (the order of execution between threads) is guaranteed.


The 2.JAVA data structure has those
① Arrays (Array)
In the program design, in order to deal with the convenience, the same type of a number of variables organized in an orderly form. These are ordered by the same number of
The collection of elements is called an array. In the C language, an array is a constructed data type. An array can be decomposed into multiple array elements, and these arrays
An element can be either a basic data type or a constructed type. Therefore, depending on the type of array elements, the array can be divided into numerical arrays, character arrays,
Array of pins, arrays of structures, and so on.
② Stack (Stack)
A stack is a special linear table that can be inserted and deleted only at one end. It stores the data in accordance with the advanced Principles, the first data is pushed into the bottom of the stack, and finally
Data is at the top of the stack and needs to read the data at the top of the stack to eject the data (the last data is read first).
③ Queues (queue)
A special linear table that allows for deletion only at the front end of the table (front), and in the back end of the table (rear). For
The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the team header. When there are no elements in the queue, it is called an empty queue.
④ linked list (Linked list)
A non-sequential, non-sequential storage structure on a physical storage unit, and the logical order of the data elements is achieved through the order of the pointers in the linked list.
A linked list consists of a series of nodes (each element in the list is called a node) that can be dynamically generated at run time. Each node consists of two parts:
One is the data field that stores the data element, and the other is the pointer field that stores the next node address.
⑤ (tree)
A tree is a n>0 set K that contains n (s) nodes, and a relationship n,n is defined in K that satisfies the following conditions:
(1) There is only one node K0, he has no precursor to the relationship N, called K0 as the root node of the tree. Abbreviation for root (root)
(2) In addition to K0, each node in K has and has only one precursor for the relationship N.
(3) The nodes in K can have M successors (m>=0) for the relationship N.
Heap (⑥)
In computer science, a heap is a special tree-shaped data structure that has a value for each node. Usually what we call the data structure of the heap, is refers to
Binary pile. The heap is characterized by the minimum (or maximum) value of the root node, and the two subtrees of the root node are also a heap.
⑦ Graph (graph)
The graph is composed of set E of the node's poor set V and the edge. In order to distinguish with the tree structure, nodes are often called vertices in the graph structure,
Edges are ordered pairs of vertices, and if there is an edge between the two vertices, it means that the two vertices have neighboring relationships.
⑧ Hash List (hash)
If there is a record of the same keyword and K in the structure, it must be in the storage location of f (k). As a result, the records can be obtained directly without comparison. Said
The corresponding relationship f is a hash function (hash functions), the table created by this idea is a hash list.


Whether inheritance in 3.java is single or multi-inheritance
Although multi-inheritance can make subclasses have the characteristics of more than one parent class at the same time, its disadvantage is also significant, there are two main aspects:
(1) If you have an instance variable of the same name in more than one parent class that inherits from a subclass, the subclass will be ambiguous when referencing the variable, and cannot determine which parent class's variable should be used
(2) If you have the same method in more than one parent class that inherits from a subclass, and there are no overrides for the method in the subclass, then calling the method will produce ambiguity and cannot determine which parent class should be called.

Because of the above fatal disadvantage, a class in Java is forbidden to inherit multiple parent classes. Single inheritance is to abandon the above two points.


What's the difference between 4.a=a+b and a+=b?
In the same data type as the two variables: there is no difference between a+=b and a=a+b.
But when the data types of the two variables are not the same, you need to consider the problem of automatic conversion of data types. That is, the accuracy is involved.
"+ =" is an operator in Java, not two, so the automatic type conversion occurs when the operation is performed, or the low-level variable can be converted directly to the advanced variable.

5. Describe the similarities and differences between synchronized and Java.util.concurrent.locks.Lock?
Main similarities: Lock can complete all functions implemented by synchronized
Main differences: Lock has more precise line semantics and better performance than synchronized. The synchronized automatically releases the lock, and lock must require the programmer to release it manually, and must be released in the finally clause. Lock also has more powerful features,
For example, its Trylock method can be used to hold the lock in a non-blocking manner

6. What is a primary key and what is a foreign key
The primary key equals the unique constraint and the non-null constraint foreign key is used to establish a relationship between the two tables, you need to specify the column that references the main table
(1) The foreign key must be a subset of the primary key (2) Delete the foreign key before deleting the primary key

7. Talk about the instantiation order of classes, such as parent static data, constructors, fields, subclass static data, constructors, fields, and when new, their order of execution
The parent class static generation variable,
Parent class static code block,
Sub-class static variables,
Subclass Static code block,
Parent class non-static variable (parent class instance member variable),
Parent class constructors,
Sub-class non-static variables (subclass instance member variables),
The subclass constructor.

First static, after the father son.
Static First: parent static > Child static
Precedence: Parent > Subclass Static Code blocks > non-static code blocks > Constructors
The instantiation process of a class:
1, static code block in the parent class, static for the current class
2, sequential execution of normal code blocks of the parent class
3, the constructor of the parent class
4, subclass generic code block
5, the constructor of the subclass (current class), executed sequentially.
6, the execution of the subclass method,


8. What is value passing and reference passing
Value passing: (formal parameter type is the basic data type): When the method is called, the actual parameter passes its value to the corresponding formal parameter, the formal parameter only initializes its own storage unit content with the value of the actual parameter, it is two different storage units, so the change of the formal parameter value in the method execution does not affect the value of the actual parameter.
Reference passing: (formal parameter type is a reference data type parameter): Also known as a pass-through address. Method invocation, the actual parameter is an object (or an array), then the actual parameters and formal parameters point to the same address, in the execution of the method, the operation of the formal parameters is actually the operation of the actual parameters, the result is preserved after the end of the method, so the change in the method execution parameters will affect the actual parameters.

9. Several ways to realize multithreading
1. Inherit the thread class to create threads
2. Implement runnable Interface Creation thread
3. Implement the callable interface to create thread threads through the Futuretask wrapper
callable<v> onecallable = new somecallable<v> ();
Create a Futuretask<integer> object from callable<integer>:
futuretask<v> OneTask = new futuretask<v> (onecallable);
Note:futuretask<integer> is a wrapper that is created by accepting Callable<integer>, which implements both the future and the Runnable interface.
Create a thread object from futuretask<integer>:
Thread onethread = new Thread (onetask);
Onethread.start ();
At this point, a thread is created.
4. Use Executorservice, callable, and future to implement threads with return results

The Executorservice, callable, and future three interfaces are actually executor frameworks. The thread that returns the result is a new feature introduced in the JDK1.5, and with this feature it is no longer necessary to get the return value. And it can be flawed if it is achieved.

A task that can return a value must implement the callable interface. Similarly, a task without a return value must implement the Runnable interface.

After performing the callable task, you can get a future object that is called get on the object to get to the object returned by the callable task.

Note: The Get method is blocked, that is, the thread has no return result, and the Get method waits.

Combined with the thread pool interface, Executorservice can realize the multi-threading of the legendary return results.

10. What is the difference between arrays (array) and list (ArrayList)? When should I use array instead of ArrayList?
An Array can contain primitive types and object types, and ArrayList can only contain object types. The Array size is fixed, and the size of the ArrayList is dynamically changing. ArrayList provides more methods and features, such as AddAll (), RemoveAll (), iterator (), and so on. For basic type data, the collection uses automatic boxing to reduce the coding effort. However, this approach is relatively slow when dealing with fixed-size base data types.
Arrays can be used when the length can be determined and the data type is consistent, other times using ArrayList

Java Developer written interview 12 questions per day (2)

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.