The relationship between JDK, JRE, and JVM:
1. All Java programs are compiled into class files, which are interacted by the virtual machine JVM and the operating system
2. The JVM needs to invoke the class library to interpret the required Lib to execute the class file, jvm+lib=jre the Java Runtime Environment
3, the JDK is mainly used for program development, the most important is the compiler, contains the Jre,java tool class and Java base Class library
Second, how to implement the thread
Implement the Runnable interface or inherit the thread class, and when you plan to multiply the inheritance, the first option is to implement runnable
Iii. What is the difference between Thread.Start () and Thread.run ()?
When start () is called, the thread is placed in the wait queue waiting for the CPU to dispatch. Then through the JVM, thread threads calls the run () method, executing the thread body of this thread
Iv. Why do I need the run () and start () methods, and we can use the run () method to complete the task?
Start () is used to start a thread, and the system will open a thread when the start () method is called. Run () is just a function of a line thread, which is independent of multithreading
1 Public synchronized voidstart () {2 if(Threadstatus! = 0)3 Throw Newillegalthreadstateexception ();4Group.add ( This);5 6 Booleanstarted =false;7 Try {8 start0 ();9 //private native void Start0 (),using the native modifier, the thread class calls the local method and registers with the JVM, which invokes the Run methodTenstarted =true; One}finally { A Try { - if(!started) { -Group.threadstartfailed ( This); the } -}Catch(Throwable ignore) { - } - } +}
V. What is the Threadlocal class and how to use it
In general, the variables we create can be accessed and modified by any one thread. Variables created with threadlocal can only be accessed by the current thread, while other threads cannot access and modify
Six, when to throw Invalidmonitorstateexception exception, why
When you call any of the methods in Wait ()/notify ()/notifyall (), a Illegalmonitorstateexception exception is thrown if the current thread does not get a lock on the object.
Seven, multi-threading differences in sleep, supend, and wait
Common denominator: will block the current thread
Different points:
1, sleep will not surrender any acquired object lock, is the thread class method, is the thread used to control its own process, call this method to catch Interruptedexception exception
2. Wait will surrender the object lock that invokes the wait object. Is the method defined in the object class for communication between threads, and wait has a notify method to wake the thread that calls wait.
3, suspend, causes the thread to enter a standstill state, unless a resume message is received, otherwise the thread will not be returned to the executable state
Viii. wait/notify differences in multi-threading
1. Wait () and Notify/notifyall methods must be used in a synchronous code block
Wait () and Notify/notifyall () are methods of the object class, and when you execute two methods, you first pass the synchronized[' s?? KR?NA?ZD] Get Lock
2, Notify/notifyall () after execution, does not immediately release the lock, but to wait until after execution of the code in the critical section, and then release
3. Test the change of a condition in multiple threads with if or while?
There is no data in the list, and then the thread goes to perform the operation of deleting the data. Therefore, it is necessary to use a while loop to determine the condition change, instead of using the IF
Ix. What happens when you use synchronization on a static method
Synchronized modifies the static method, actually is to lock the class object, commonly known as "class lock"
Answer: http://blog.csdn.net/u010842515/article/details/65443084
X. Join method of Thread
1, the main function of the join method in the thread class is synchronization, which can make the parallel execution between threads into serial execution
2. The Join method must be called after the thread start method is called to make sense
3, the principle of the Join method is to call the corresponding thread wait for the operation, for example, a thread in the call of the B-thread's Join method, is equivalent to a thread in a call the B-thread wait method, when the B thread finishes (or arrives at the wait time), The B thread automatically calls its own Notifyall method to wake the a thread to achieve the purpose of synchronization
Xi. deadlock
Formation reason: Cyclic waiting, multi-threaded application of multiple resources, formed a cycle of waiting
1, mutual exclusion: the process in a certain period of time exclusive resources;
2. Holding: When a process is blocked by the request for resources, the resources that have been obtained are kept in place;
3. No deprivation: The process has obtained resources and cannot be forcibly deprived until the end of use.
4, ring wait: A number of processes to form a tail-to-toe cycle waiting for resource relations;
Workaround:
1, for 2 and 3, the introduction of a transaction mechanism, all locking operations as a transaction treatment, once the lock, that is, to ensure that all operations can be rolled back, while the lock manager to detect deadlocks, and deprive of resources
2, for 4, the order must be locked in accordance with the agreement
3. Add a time-out when attempting to acquire a lock
4. Deadlock detection mechanism
Whenever a thread obtains a lock, it is written down in the data structure (map, graph, and so on) associated with the lock. In addition, whenever a thread requests a lock, it needs to be recorded in this data structure.
When a thread requests a lock failure, the thread can traverse the lock's diagram to see if a deadlock has occurred.
The Interview thread