Alibaba internal interview summary and Alibaba interview Summary

Source: Internet
Author: User

Alibaba internal interview summary and Alibaba interview Summary

1. What is the difference between StringBuffer and StringBuilder? Assume there is a method. An object must be defined inside the method, which may be StringBuffer or StringBuilder. Next, multiple append operations are performed. When the method ends, the toString () Result of the object is returned, and this thread will be concurrently accessed by multiple threads. Please select this object to be defined as StringBuffer or StringBuilder? Why?

A: StringBuffer is thread-safe; StringBuilder is thread-unsafe.

1. Let's take a look at the definition of String StringBuffer StringBuilder. Final-modified classes cannot be inherited, that is, they cannot have child classes. Public final class StringBuffer public final class StringBuilder public final class String2. source code for the appdend method is as follows: public synchronized StringBuffer append (String str) {toStringCache = null; super. append (str); return this;} public StringBuilder append (String str) {super. append (str); return this;} 3. stringBuilder and StringBuffer can be used for character strings that change frequently. StringBuilder is more efficient than StringBuffer. StringBuffer can ensure thread security, while StringBuil Der cannot.

2. What is the use of synchronized? How to use it? (Pseudocode, listing all usage methods)

A: synchronized is a keyword of the Java language and A reentrant lock. When it is used to modify a method or a code block, it can ensure that at most one thread can execute the code segment at the same time. Synchronized is used to modify methods and code blocks (Object locks and member locks ).

Package basic; public final class TestSynchronized {public static void main (String [] args) {new Thread ("Thread A") {@ Override public void run () {try {print ("thread... ");} catch (InterruptedException e) {e. printStackTrace ();}}}. start (); new Thread ("Thread B") {@ Override public void run () {try {print ("Thread B... ");} catch (InterruptedException e) {e. printStackTrace ();}}}. start ();} public static synchronized void print (String str) throws InterruptedException {System. out. println ("current Thread:" + Thread. currentThread (). getName () + "execution start"); for (int I = 0; I <10; I ++) {System. out. println (str); Thread. sleep (2000);} System. out. println ("current Thread:" + Thread. currentThread (). getName () + "execution completed") ;}// code execution result: current thread: thread A execution started thread... thread... thread... thread... thread... thread... thread... thread... thread... thread... current thread: thread A execution completed current thread: thread B execution started thread B... thread B... thread B... thread B... thread B... thread B... thread B... thread B... thread B... thread B... current thread: thread B is finishedSynchronized modification method

Synchronized can also modify the code block while modifying the method. Objects and member locks are divided into modified code blocks. The code implementation is as follows:

 

Package basic; public class SynchronizedExample {public static void main (String [] args) {new Thread ("Thread A") {@ Override public void run () {print ("thread ");}}. start (); new Thread ("Thread B") {@ Override public void run () {print ("Thread B ");}}. start ();} public static void print (String str) {System. out. println ("Thread:" + Thread. currentThread (). getName () + "start execution"); synchronized (SynchronizedExample. class) {for (int I = 0; I <10; I ++) {System. out. println (Thread. currentThread (). getName () + "printed information:" + I); try {Thread. sleep (1000);} catch (InterruptedException e) {}} System. out. println ("Thread:" + Thread. currentThread (). getName () + "execution ended") ;}// code execution result: thread A starts to execute thread A prints the information: 0 thread: thread B starts to execute thread A and prints the information: 1 thread A prints the information: 2 thread A prints the information: 3 thread A prints the information: 4 thread A prints the information: thread A of thread 5 prints the information: thread A of thread 6 prints the information: thread A of thread 7 prints the information: thread A of thread 8 prints the information: thread 9: thread A execution end thread B prints the information: 0 thread B prints the information: 1 thread B prints the information: 2 thread B prints the information: 3 thread B prints the information: 4. Thread B prints the message: thread 9: thread B execution endsExample code of synchronized object lock (only applicable to the current instance of the Class)

 

Next, let's look at it again,

 

Package basic; public class SynchronizedExample {public static void main (String [] args) {final MySynchronized mySynchronized = new MySynchronized (); new Thread ("Thread ") {@ Override public void run () {mySynchronized. print ("thread ");}}. start (); new Thread ("Thread B") {@ Override public void run () {mySynchronized. print ("thread B ");}}. start () ;}} class MySynchronized {public synchronized void print (String str) {System. out. println ("Thread:" + Thread. currentThread (). getName () + "start execution"); for (int I = 0; I <10; I ++) {System. out. println (Thread. currentThread (). getName () + "printed information:" + I); try {Thread. sleep (1000);} catch (InterruptedException e) {}} System. out. println ("Thread:" + Thread. currentThread (). getName () + "execution ended") ;}// code running result: thread A starts to execute thread A prints the information: 0 thread A prints the information: thread A prints the information: thread A prints the information: 6. Thread A prints the information: 7. Thread A prints the information: 8. Thread A prints the information: 9. Thread A: thread B: 0 thread B prints the information: 1 thread B prints the information: 2 thread B prints the information: 3 thread B prints the information: 4 thread B prints the information: 5. Thread B prints the information: 6. Thread B prints the information: 7. Thread B prints the information: 8. Thread B prints the information: 9. Thread B ends the execution of package basic; public class SynchronizedExample {public static void main (String [] args) {final MySynchronized mySynchronized_first = new MySynchronized (); final MySynchronized synchronized = new MySynchronized (); new Thread ("Thread A") {@ Override public void run () {mySynchronized_first.print ("Thread ");}}. start (); new Thread ("Thread B") {@ Override public void run () {mySynchronized_second.print ("Thread B ");}}. start () ;}} class MySynchronized {public synchronized void print (String str) {System. out. println ("Thread:" + Thread. currentThread (). getName () + "start execution"); for (int I = 0; I <10; I ++) {System. out. println (Thread. currentThread (). getName () + "printed information:" + I); try {Thread. sleep (1000);} catch (InterruptedException e) {}} System. out. println ("Thread:" + Thread. currentThread (). getName () + "execution ended") ;}// code running result: thread A starts to execute thread A prints the information: 0 thread: thread B starts to execute thread B and prints the information: 0 thread A prints the information: 1 thread B prints the information: 1 thread A prints the information: 2 thread B prints the information: thread A prints the information: thread B prints the information: thread A prints the information: thread B prints the information: 4 thread A prints the information: thread 5 B prints the information: thread 5 A prints the information: thread 6 B prints the information: thread 6 A prints the information: thread 7 B prints the information: 7. Thread A prints the information: 8. Thread B prints the information: 8. Thread A prints the information: 9. Thread B prints the information: 9. Thread: thread A: Execution end thread: package basic; public class completion {public static void main (String [] args) {final MySynchronized synchronized = new MySynchronized (); new Thread ("Thread A") {@ Override public void run () {mySynchronized_first.print ("Thread ");}}. start (); new Thread ("Thread B") {@ Override public void run () {mySynchronized_second.print ("Thread B ");}}. start () ;}} class MySynchronized {public static synchronized void print (String str) {System. out. println ("Thread:" + Thread. currentThread (). getName () + "start execution"); for (int I = 0; I <10; I ++) {System. out. println (Thread. currentThread (). getName () + "printed information:" + I); try {Thread. sleep (1000);} catch (InterruptedException e) {}} System. out. println ("Thread:" + Thread. currentThread (). getName () + "execution ended") ;}// code running result: thread A starts to execute thread A prints the information: 0 thread A prints the information: thread A prints the information: thread A prints the information: 6. Thread A prints the information: 7. Thread A prints the information: 8. Thread A prints the information: 9. Thread A: thread B: 0 thread B prints the information: 1 thread B prints the information: 2 thread B prints the information: 3 thread B prints the information: 4 thread B prints the information: 5. Thread B printed the information: 6. Thread B printed the information: 7. Thread B printed the information: 8. Thread B printed the information: 9. Thread B ended the execution.Example code of synchronized object lock (code block modified by synchronized ,)

 

 

 

3. What is the role of the ReentranLock class? Which of the following methods are commonly used? What are their respective features?

4. What are the optional solutions for synchronizing multiple machines in a cluster environment? (It is best to use pseudocode to write the key part)

5. How to list the design points and usage of optimistic locks?

6. What is idempotence control? For example, how do you implement idempotence control? (Or how to implement idempotence control in project IDCM ?)

7. What are the key technologies used by spring to implement aop?

8. What are the differences and features between HashMap and ConcurrentHashMap?

9. What classes have you used in java. util. concurrent package? What are their purposes and features?

10. If the data size of a table is large and the query performance is affected, what optimization solutions can be provided? What are the principles for creating indexes?

11. What is the understanding of database transaction isolation level? (How is the project IDCM used ?)

12. What is the difference between annotation @ Component @ Repository @ Service @ Controller in Spring? (How is context: component-scan annotation scan configured in project IDCM ?)

 

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.