Java Face question Summary 2 (continuous update)

Source: Internet
Author: User
Tags date now

1. How to format a date

Answer:
Date Now=new date ();

SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");

String Formatnow=sdf.format (now);

Variable Formatnow is a well-formed date.

2. There are several ways to implement multithreading, what are the different methods of synchronization?

Answer: (1) There are two ways to implement multithreading: inheriting the Thread class or implementing the Runnable interface.
Java.lang.Thread, and rewrite the Run () method, put the execution body of the thread into it, implement the Java.lang.Runnable interface, implement its run method, put the thread's execution body into it.

There is little difference between the two implementations. The way to inherit the thread class is simpler to implement, but the class that inherits it can no longer inherit other classes, so it cannot inherit the useful methods of other classes. And the use is to want to runnable interface of the way there is no such problem, and this way of implementation of the thread body and the thread object itself is separated, logic is also relatively clear, so it is recommended to use this way more.
(2) There are two ways to achieve synchronization: one is synchronous and the other is synchronous code block.

The synchronization method is preceded by the method return type with the Synchronized keyword

The synchronization code block is synchronized (write the object that needs to be synchronized here) {...}

3. Talk about the differences between clustered and non-clustered indexes, and their pros and cons.

Answer:
Clustered index, the data stored in the table is stored in the order of the index, the retrieval efficiency is higher than the normal index, but the impact on the new/modified/deleted data is relatively large nonclustered index, does not affect the data storage order in the table, the retrieval efficiency is lower than the clustered index, the impact on the data addition/modification/deletion is very small

4. What are the necessary conditions for deadlocks? How to overcome?

Answer: Four necessary conditions to generate a deadlock:

Mutex condition: A resource can only be used by one process at a time.

Request and hold condition: When a process is blocked by a request for resources, it remains in place for the resources that have been obtained.

Non-deprivation: the resources that the process has acquired cannot be forcibly stripped until the end of use.

Cyclic wait condition: a cyclic waiting resource relationship is formed between several processes.

These four conditions are necessary for the deadlock, as long as the system has a deadlock, these conditions must be established, and as long as one of the above conditions is not satisfied, there will be no deadlock.

Workaround for Deadlock:

A undo all the processes that are deadlocked, and B undo the deadlock process one by one until the deadlock does not exist, and C forcibly discards the resource consumed from the deadlock process until the deadlock disappears. D forcibly deprive a sufficient number of resources from other processes to allocate to the deadlock process to unlock the deadlock state

5. In the network programming of the server, the methods to resolve the session trace are:

A. Use cookies.

B. Use URL rewriting.

C. Use a hidden form field.

D. None of the above methods can be used alone.

Solution: ABC

6. Advantages and principles of garbage collection. and consider 2 kinds of recycling mechanisms.

A notable feature of the Java language is the introduction of a garbage collection mechanism, which makes it possible for the C + + programmer to solve the most troublesome memory management problems, which makes it unnecessary for Java programmers to consider memory management when writing programs. Because there is a garbage collection mechanism, objects in Java no longer have a "scope" concept, and only references to objects are scoped. Garbage collection can effectively prevent memory leaks and effectively use memory that can be used. The garbage collector is typically run as a separate low-level thread, unpredictable and clear and recyclable for objects that have died in the heap or that have not been used for a long time, and the programmer cannot call the garbage collector in real time for garbage collection of an object or all objects. The recycling mechanism has generational replication garbage collection and token garbage collection, incremental garbage collection.

7. Please say the thread synchronization method you know.

Wait (): causes a thread to be in a wait state and releases the lock of the object it holds.
Sleep (): Makes a running thread sleep, is a static method that calls this method to catch the interruptedexception exception.
Notify (): Wakes up a waiting thread, noting that when this method is called, it does not actually wake up a waiting state thread, but is determined by the JVM to wake up which thread, and not by priority.
Allnotity (): Wakes all the threads that are in the waiting state, noting that they do not give all the wake-up threads an object lock, but instead let them compete.

What is 8.GC? Why do you have a GC? (base).
GC is a garbage collector. Java programmers don't have to worry about memory management because the garbage collector is automatically managed. To request garbage collection, you can call one of the following methods: System.GC () Runtime.getruntime (). GC ()

The difference between 9.Overload and override.

Can the overloaded method change the type of the return value? The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism. Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked". If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, which is called a method overload (overloading). The overloaded method is to change the type of the return value.

Java Face question Summary 2 (continuous update)

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.