Java basics 2

Source: Internet
Author: User

1. Please state the thread synchronization method you know.
Wait (): puts a thread in the waiting state and releases the lock of the object it holds.
Sleep (): It is a static method that calls a running thread to capture interruptedexception exceptions.
Y (): Wake up a thread in the waiting state. Note that when this method is called, it cannot actually wake up a thread in the waiting state, instead, the JVM determines which thread to wake up, not by priority.
Allnotity (): Wake up all threads in the wait state. Note that it is not a lock for all wake-up threads but a lock for them to compete.
2. What are the collection classes you know? What are the main methods?
The most common collection classes are list and map. The specific implementation of list includes arraylist and vector, which are variable-size lists and are suitable for building, storing, and operating the list of elements of any type of object. List is applicable to accessing elements by Numerical index.

Map provides a more general method for storing elements. The Map Collection class is used to store element pairs (called "keys" and "values"). Each key maps to a value.

3. What are several implementation methods for multithreading? What are the implementation methods of synchronization?
Multithreading can be implemented in two ways: Inheriting the Thread class and implementing the runnable interface.
There are two types of synchronization implementation: synchronized, wait, and Y.

4. Basic concepts of threads, basic states of threads, and relationships between States
A thread refers to an execution unit that can execute program code during program execution. Each program has at least one thread, that is, the program itself.
Java threads have four states: running, ready, suspended, and ended.

5. What are common Java design patterns? Describe the factory model.
23 Design Patterns in Java:
Factory (factory mode), Builder (construction mode), factory method (Factory method mode ),
Prototype (original model mode), Singleton (Singleton mode), facade (facade mode ),
Adapter (adapter mode), bridge (Bridge Mode), composite (merging mode ),
Decorator, flyweight, proxy ),
Command (command mode), Interpreter (Interpreter mode), visitor (visitor mode ),
Iterator, mediator, memento ),
Observer (Observer mode), State (State mode), Strategy (Policy mode ),
Template Method (template method mode), chain of responsibleity (responsibility chain mode)
Factory mode: The factory mode is a frequently used mode. Classes implemented based on the factory mode can generate instances of a class in a group based on the provided data, generally, this group of classes has a common abstract parent class that implements the same method, but these methods perform different operations on different data. First, you need to define a base class. The subclass of this class implements the methods in the base class through different methods. Define a factory class. The factory class can generate different subclass instances according to the conditions. After obtaining the subclass instance, developers can call the methods in the base class without having to consider which subclass instance is returned.

6. What are the methods for sorting? List. Use Java to implement a fast sorting.
The sorting methods include insert sorting (direct insertion sorting and Hill sorting), exchange sorting (Bubble sorting and quick sorting), select sorting (Direct selection sorting and heap sorting), and merge sorting, sort allocation (Box sorting and base sorting)

Pseudo code for fast sorting.
// Use the quick sorting method to sort a [0: n-1]
Select an element from a [0: n-1] As m I d l e, which is the fulcrum
Divide the remaining elements into two left and r I g h t, so that the elements in L E F T are less than or equal to the fulcrum, and the elements in right are greater than or equal to the Fulcrum.
Recursively sort left by using the quick sorting method
Recursively sort right using the quick sorting method
The result is l e f t + m I d l e + r I g h t.

7. How to handle exceptions in Java? What are the meanings of keywords: throws, throw, try, catch, and finally? Can I throw an exception in the try block?
Java uses object-oriented methods to handle exceptions, classify various exceptions, and provides good interfaces. In Java, each exception is an object, which is an instance of the throwable class or other subclass. When a method encounters an exception, an exception object is thrown. The object contains the exception information. The method that calls this object can capture the exception and handle it. Java exception handling is implemented through five keywords: Try, catch, throw, throws, and finally. Generally, try is used to execute a program. If an exception occurs, the system throws an exception. In this case, you can catch it by its type, or finally (finally) is handled by the default processor.

Use try to specify a program to prevent all "exceptions. The catch clause should be followed by the try program to specify the type of the "exception" you want to capture.
Throw statements are used to explicitly throw an "exception ".
Throws is used to indicate various "exceptions" that a member function may throw ".
Finally ensures that a piece of code is executed no matter what "exception" occurs.
You can write a try statement outside a member function call, and write another try statement inside the member function to protect other code. Every time a try statement is encountered, the framework of "exception" is placed on the stack until all try statements are completed. If the try statement at the next level does not process an "exception", the stack will be opened until a try statement that handles this "exception" is encountered.

8. Can a ". Java" source file contain multiple classes (not internal classes )? What are the restrictions?
Yes. Only one class name must be the same as the file name.
9. What technologies are used to implement MVC? How to implement it?
MVC is short for Model-View-controller. "Model" indicates the business logic of the application (implemented by the JavaBean and EJB components), and "View" indicates the application surface (generated by the JSP page ), "controller" provides application processing process control (generally a servlet). This design model divides the application logic, processing process and display logic into different components for implementation. These components can be used for interaction and reuse.

10. How can I implement a thread in Java? What keywords are used to modify the synchronization method? Why is the stop () and suspend () methods not recommended?
There are two implementation methods: Inheriting the Thread class and implementing the runnable interface.
Use the synchronized keyword to modify the synchronization method
The use of stop () is opposed because it is insecure. It unlocks all the locks obtained by the thread, and if the object is in an inconsistent state, other threads can check and modify them in that state. It is difficult to check the real problem. The suspend () method is prone to deadlocks. When suspend () is called, the target thread stops, but still holds the lock obtained before that. In this case, no other thread can access the locked resources unless it is "suspended. For any thread, if they want to restore the target thread and attempt to use any locked resource, it will cause a deadlock. Therefore, you should not use suspend (), but set a flag in your Thread class to indicate whether the thread should be active or suspended. If the flag indicates that the thread should be suspended, use wait () to enter the waiting state. If the flag indicates that the thread should be recovered, restart the thread with a notify.

11. How many types of streams are available in Java? JDK provides some abstract classes for each type of stream for inheritance. which classes are they?
Byte stream: bytes stream. Byte streams are inherited from inputstream outputstream, and bytes streams are inherited from inputstreamreader outputstreamwriter. There are many other streams in the Java. Io package, mainly to improve performance and ease of use.

1. Please state the thread synchronization method you know.
Wait (): puts a thread in the waiting state and releases the lock of the object it holds.
Sleep (): It is a static method that calls a running thread to capture interruptedexception exceptions.
Y (): Wake up a thread in the waiting state. Note that when this method is called, it cannot actually wake up a thread in the waiting state, instead, the JVM determines which thread to wake up, not by priority.
Allnotity (): Wake up all threads in the wait state. Note that it is not a lock for all wake-up threads but a lock for them to compete.
2. What are the collection classes you know? What are the main methods?
The most common collection classes are list and map. The specific implementation of list includes arraylist and vector, which are variable-size lists and are suitable for building, storing, and operating the list of elements of any type of object. List is applicable to accessing elements by Numerical index.

Map provides a more general method for storing elements. The Map Collection class is used to store element pairs (called "keys" and "values"). Each key maps to a value.

3. What are several implementation methods for multithreading? What are the implementation methods of synchronization?
Multithreading can be implemented in two ways: Inheriting the Thread class and implementing the runnable interface.
There are two types of synchronization implementation: synchronized, wait, and Y.

4. Basic concepts of threads, basic states of threads, and relationships between States
A thread refers to an execution unit that can execute program code during program execution. Each program has at least one thread, that is, the program itself.
Java threads have four states: running, ready, suspended, and ended.

5. What are common Java design patterns? Describe the factory model.
23 Design Patterns in Java:
Factory (factory mode), Builder (construction mode), factory method (Factory method mode ),
Prototype (original model mode), Singleton (Singleton mode), facade (facade mode ),
Adapter (adapter mode), bridge (Bridge Mode), composite (merging mode ),
Decorator, flyweight, proxy ),
Command (command mode), Interpreter (Interpreter mode), visitor (visitor mode ),
Iterator, mediator, memento ),
Observer (Observer mode), State (State mode), Strategy (Policy mode ),
Template Method (template method mode), chain of responsibleity (responsibility chain mode)
Factory mode: The factory mode is a frequently used mode. Classes implemented based on the factory mode can generate instances of a class in a group based on the provided data, generally, this group of classes has a common abstract parent class that implements the same method, but these methods perform different operations on different data. First, you need to define a base class. The subclass of this class implements the methods in the base class through different methods. Define a factory class. The factory class can generate different subclass instances according to the conditions. After obtaining the subclass instance, developers can call the methods in the base class without having to consider which subclass instance is returned.

6. What are the methods for sorting? List. Use Java to implement a fast sorting.
The sorting methods include insert sorting (direct insertion sorting and Hill sorting), exchange sorting (Bubble sorting and quick sorting), select sorting (Direct selection sorting and heap sorting), and merge sorting, sort allocation (Box sorting and base sorting)

Pseudo code for fast sorting.
// Use the quick sorting method to sort a [0: n-1]
Select an element from a [0: n-1] As m I d l e, which is the fulcrum
Divide the remaining elements into two left and r I g h t, so that the elements in L E F T are less than or equal to the fulcrum, and the elements in right are greater than or equal to the Fulcrum.
Recursively sort left by using the quick sorting method
Recursively sort right using the quick sorting method
The result is l e f t + m I d l e + r I g h t.

7. How to handle exceptions in Java? What are the meanings of keywords: throws, throw, try, catch, and finally? Can I throw an exception in the try block?
Java uses object-oriented methods to handle exceptions, classify various exceptions, and provides good interfaces. In Java, each exception is an object, which is an instance of the throwable class or other subclass. When a method encounters an exception, an exception object is thrown. The object contains the exception information. The method that calls this object can capture the exception and handle it. Java exception handling is implemented through five keywords: Try, catch, throw, throws, and finally. Generally, try is used to execute a program. If an exception occurs, the system throws an exception. In this case, you can catch it by its type, or finally (finally) is handled by the default processor.

Use try to specify a program to prevent all "exceptions. The catch clause should be followed by the try program to specify the type of the "exception" you want to capture.
Throw statements are used to explicitly throw an "exception ".
Throws is used to indicate various "exceptions" that a member function may throw ".
Finally ensures that a piece of code is executed no matter what "exception" occurs.
You can write a try statement outside a member function call, and write another try statement inside the member function to protect other code. Every time a try statement is encountered, the framework of "exception" is placed on the stack until all try statements are completed. If the try statement at the next level does not process an "exception", the stack will be opened until a try statement that handles this "exception" is encountered.

8. Can a ". Java" source file contain multiple classes (not internal classes )? What are the restrictions?
Yes. Only one class name must be the same as the file name.
9. What technologies are used to implement MVC? How to implement it?
MVC is short for Model-View-controller. "Model" indicates the business logic of the application (implemented by the JavaBean and EJB components), and "View" indicates the application surface (generated by the JSP page ), "controller" provides application processing process control (generally a servlet). This design model divides the application logic, processing process and display logic into different components for implementation. These components can be used for interaction and reuse.

10. How can I implement a thread in Java? What keywords are used to modify the synchronization method? Why is the stop () and suspend () methods not recommended?
There are two implementation methods: Inheriting the Thread class and implementing the runnable interface.
Use the synchronized keyword to modify the synchronization method
The use of stop () is opposed because it is insecure. It unlocks all the locks obtained by the thread, and if the object is in an inconsistent state, other threads can check and modify them in that state. It is difficult to check the real problem. The suspend () method is prone to deadlocks. When suspend () is called, the target thread stops, but still holds the lock obtained before that. In this case, no other thread can access the locked resources unless it is "suspended. For any thread, if they want to restore the target thread and attempt to use any locked resource, it will cause a deadlock. Therefore, you should not use suspend (), but set a flag in your Thread class to indicate whether the thread should be active or suspended. If the flag indicates that the thread should be suspended, use wait () to enter the waiting state. If the flag indicates that the thread should be recovered, restart the thread with a notify.

11. How many types of streams are available in Java? JDK provides some abstract classes for each type of stream for inheritance. which classes are they?
Byte stream: bytes stream. Byte streams are inherited from inputstream outputstream, and bytes streams are inherited from inputstreamreader outputstreamwriter. There are many other streams in the Java. Io package, mainly to improve performance and ease of use.

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.