Java common face questions and answers
Java garbage Collection summary on the inner classes in Java
1) are transient and volatile Java keywords?
If you declare an instance variable with transient, its value does not need to be maintained when the object is stored. For example:
Class T
{
transient int A; No need to maintain
int b; Need to maintain
}
Here, if an object of the T class is written to a persistent storage area, the contents of a are not saved, but B's will be saved.
The volatile modifier tells the compiler that variables modified by volatile can be changed by other parts of the program. In multithreaded programs, sometimes two or more threads share an identical instance variable. Consider the efficiency issue, where each thread can save its own private copy of the shared variable. The actual copy of the variable is updated at different times, such as when entering the Synchronized method. Using STRICTFP to decorate classes or methods ensures that floating-point operations (and all cuts) are as accurate as earlier versions of Java. Cuts the exponent that affects only certain operations. When a class is STRICTFP modified, all methods are automatically modified by STRICTFP.
STRICTFP means fp-strict, that is to say, the meaning of the precise floating point. When a Java virtual machine makes a floating-point operation, if the STRICTFP keyword is not specified, the Java compiler and the runtime's expression on floating-point operations do so in an approximate way, so that the resulting results do not always satisfy you. Once STRICTFP is used to declare a class, interface, or method, the compiler and the runtime environment for the declared scope of Java are executed exactly as the floating-point specification IEEE-754. So if you want your floating-point arithmetic to be more accurate and not inconsistent with the results of different hardware platforms, then use the keyword STRICTFP.
You can declare a class, interface, and method as STRICTFP, but do not allow the STRICTFP keyword to be declared on methods and constructors in the interface, such as the following code:
STRICTFP interface A {}
Public STRICTFP class FpDemo1 {
STRICTFP void F () {}
}
2. How to use the error
Interface A {
STRICTFP void f ();
}
public class FpDemo2 {
STRICTFP FpDemo2 () {}
}
Once the keyword STRICTFP is used to declare a class, interface, or method, all floating-point operations in the scope declared by this keyword are accurate and conform to the IEEE-754 specification
Of For example, if a class is declared as STRICTFP, then all methods in the class are STRICTFP.
2) What is the difference between an abstract class and an interface? (instantaneous)
1.abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. However, a class can implement multiple interface.
2. In abstract class, you can have your own data members, or you can have non-ABSTARCT member methods, and in interface, you can only have static data members that cannot be modified (that is, it must be static final, but Data members are not generally defined in interface, and all member methods are abstract.
The 3.abstract class and interface reflect a different design concept. In fact, abstract class represents the "is-a" relationship, interface represents the "like-a" relationship.
4. Classes that implement abstract classes and interfaces must implement all of these methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.
5. The variable defined in the interface is the public static final type by default, and must be given its initial value, so the implementation class cannot be redefined or changed.
6. Variables in abstract classes are friendly by default, whose values can be redefined in subclasses or re-assigned.
7. The methods in the interface are public,abstract type by default.
3) Can you say a little bit about the reflection mechanism of Java? (instantaneous)
Open and causal connections (causally-connected) are two basic elements of reflective systems
4) How to implement multithreading in Java? (instantaneous)
Extends Thread
Implement Runnable
Method One: Inherit the thread class, overwrite the method run (), we rewrite the run () in the subclass of the thread class that was created, and the code to be executed by the thread is added. Here is an example:
public class MyThread extends Thread
{
int count=1, number;
public MyThread (int num)
{
Number=num;
System.out.println
("Create thread" + number);
}
public void Run () {
while (true) {
System.out.println
("thread" + number + ": Count" + count);
if (++count==6) return;
}
}
public static void Main (String args[])
{
for (int i=0;i〈5; i++) New MyThread (i+1). Start ();
}
}
This method is straightforward and consistent with everyone's habits, but it also has a big drawback, that is, if our class has been inherited from a class (such as a small program must inherit from the Applet Class), then can not inherit the Thread class, then if we do not want to create a new class, what should we do?
We might as well explore a new approach: instead of creating subclasses of the thread class and using it directly, we can only pass our method as an argument to an instance of the thread class, somewhat like a callback function. But Java has no pointers, we can only pass an instance of a class that contains this method.
So how do you limit the class to include this method? Use the interface, of course! (Although abstract classes can be met, but inheritance is required, and we are using this new approach, is not to avoid the limitations of inheritance?) )
Java provides interface java.lang.Runnable to support this approach.
Method Two: Implement Runnable interface
The runnable interface has only one method run (), we declare our class implements the Runnable interface and provide this method, and write our thread code into it to complete this part of the task. But the Runnable interface does not have any support for threading, and we must also create an instance of the thread class, which is implemented by the thread class's constructor public thread (Runnable target). Here is an example:
public class MyThread implements Runnable
{
int count=1, number;
public MyThread (int num)
{
Number=num;
SYSTEM.OUT.PRINTLN ("Create thread" + number);
}
public void Run ()
{
while (true)
{
System.out.println
("thread" + number + ": Count" + count);
if (++count==6) return;
}
}
public static void Main (String args[])
{
for (int i=0; i〈5;i++) New Thread (New MyThread (i+1)). Start ();
}
}
Strictly speaking, it is possible to create an instance of the thread subclass, but it must be noted that the subclass must not overwrite the Run method of the thread class, otherwise the thread will execute the Run method of the subclass, not the run method of the class that we use to implement the Runnable interface. Let's try this one.
Using the Runnable interface to enable multithreading allows us to accommodate all of the code in a class, and the downside is that we can only use a set of code, and if you want to create multiple threads and have different code for each thread, you still have to create the additional class, if that's the case, In most cases, it might not be as compact to inherit the Thread directly from multiple classes.
In summary, the two methods are different, we can use flexibly.
Let's take a look at some of the problems in multithreaded use.
Three, four states of the thread
1. New status: Thread has been created but not yet executed (start () has not yet been called).
2. Executable state: Threads can execute, although not necessarily executing. The CPU time may be assigned to the thread at any time, allowing it to execute.
3. Death status: Normally the run () return causes the thread to die. Calling Stop () or destroy () also has the same effect, but is not recommended, the former will produce an exception, the latter is forced to terminate, will not release the lock.
4. Blocking status: Threads are not allocated CPU time and cannot be executed.
Iv. Priority of Threads
The priority of the thread represents the importance of the thread, and when multiple threads are in an executable state and waiting for CPU time, the thread dispatch system decides which CPU time to assign to the individual threads based on the priority of each thread, the higher priority thread has a greater chance of getting CPU time, and the low priority thread is not without opportunity. Just the chance to be smaller.
You can call methods GetPriority () and SetPriority () of the thread class to access the thread's priority, the thread's priority bounds are between 1 (min_priority) and ten (max_priority), and the default is 5 (norm_ Priority).
Java common face question and solution-java development