Home » Java » Sort out the JAVA base (two)

Sort out the JAVA base (two)

1.Staticclass and nonstaticclass difference.

Staticclass is a static nested class, and nonstaticclass is an internal class.

nesting is independent of itself, and internal is part of.

2.java polymorphism implementation principle.

subclass function to find the address, then there is address, subclass function can not find the address, go to the parent class address.

3. two ways to implement multithreading: Thread, and Runable.



4. thread synchronization methods: sychronized, lock, reentrantLock.

, etc.

Sychronized is used when resource competition is not very intense, and can not be synchronized by interrupt;

EentrantLock is used when resource competition is fierce and can be synchronized by interrupt;

Lock needs to manually release.

5. lock levels: Method locks, object locks, class locks,.

method locks are similar to object locks;


object is a lock for the object, it is only in the position statement of a memory of the object a flag identifies whether the object has the lock, so it will only object currently locked. A general object lock is syncronized modification of a non static member variables, or syncronized modification of a non static method. The object lock, different objects to access the same time is not syncronized modified by blocking.


lock is lock the entire class, when there are multiple threads to declare an object of this class will be blocked until the lock has the class object is destroyed or take the initiative to release locks. At this time the thread is blocked by picking out a kind of possession of the lock, the class declaration object. Other threads continue to be blocked.

either lock or lock object class, between parent and child classes are not directly related to obstruction. When a parent class and subclass of class lock is not affected, the opposite is also true. As part of the synchronized method signature, it is on the modified method the child. To override the synchronization method in the parent class or is the time synchronization method declared in the interface, synchronized modifiers are not automatically inherited, so the corresponding block asked
The title doesn't appear.

6. write producer consumer mode.


The design concept and function of


ThreadLocal lets us create variables.

that can only be accessed by the same thread

multiple threads execute the code at the same time, and the threads cannot access each other's ThreadLocal variables.



8.ThreadPool usage and advantage.

ThreadPoolExecutor to create a pool of threads;

use thread pool, you can use used thread, do not need to recreate thread.

improve thread management.

9.Concurrent package.


Jconcurrentdk1.5 > >java.util.concurrent package, including java.util.concurrent.atomicjava.util.concurrent.lock includes concurrent collections, the thread pool mechanism, synchronization mechanism, thread safe update tools, lock.

concurrent package

AbstractExecutorService provides a default way to call the ExcutorService method;

CountDownLatch synchronization assistance allows one or more threads to wait for a set of operations that other threads are executing..

ArrayBlockingQueue, a bounded blocked queue.

supported by an array

The difference between

10.wait (), , and sleep (), .

wait) > call > notify) , to enter the operating state of the.

pool waiting ready to obtain the object

when calling sleep () , the thread does not give up the thread lock, just wait for the time it takes, and then automatically resume the running.

11.foreach compare to normal for cycle efficiency.

for cycle each cycle will call the GetCount ( >foreach GetList ( does not consider the length of.

The object is locked when the

foreach loop cannot be modified during.

12.JavaIO and NIO.

IO >NIO IO read one or more bytes, no buffer, unable to move. So it need to do NIO, he can read into the buffer, some flexible processing but to check whether the buffer. Area contains all you need to deal with data, has covered the raw data of.IO flow is blocked, until completely finished reading, while NIO is non blocking when there is no time to read and write threads can do other things, so a separate thread now you can manage multiple input and output channel .

The role of

13. reflection and principle.

reflection actually creates objects at run time instead of creating objects.

at compile time

> as long as you know the name of the class, you can know all of his methods, variables, names, types, and so on.

14. generic features, can List be converted to List.

Although the >String >Object ListList > is two different types, no inheritance relationship between them.

generics are used to remember the type.

of elements within a collection