1. Threads and processes
Thread: A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch.
Process: A process is a program with a certain independent function, it is the system process resource allocation and scheduling of a separate unit.
Difference:
(1) A thread belongs to only one process, and one process contains one or more threads.
(2) The process has a separate memory unit, while multiple threads share memory.
(3) The creation of the process calls fork or vfork, while the thread's creation calls Pthead_create, and all the threads it owns are destroyed when the process ends, and the end of the thread does not affect the end of other threads in the same process.
(4) The thread is a lightweight process, it takes much less time to create and destroy than the process, and all the execution functions in the operating system are created threads to complete.
(5) Threads are generally synchronized and mutually exclusive when they are executed because they share the resources of the same process.
2, deadlock? What is the cause of the deadlock? What are the necessary conditions for deadlocks? How to handle deadlocks?
Deadlock: A deadlock is a phenomenon of two or more than two processes that are caused by competing resources or by communicating with each other during execution.
Deadlock reason: Insufficient system resources, competing resources. Inappropriate request Resource Order
The necessary conditions for deadlocks:
1. Mutex condition: A resource can only be used by one process at a time.
2. 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.
3. Inalienable conditions: the resources that the process has acquired cannot be forcibly stripped until it is exhausted and can only be released by itself when the process is exhausted.
4. Cyclic wait conditions: a cyclic waiting resource relationship is formed between several processes.
Ways to avoid deadlocks:
Because the mutex is immutable, it can only break one of the other three conditions to unlock the deadlock, by depriving the resource and killing one of the threads.
The simplest way to avoid deadlocks is to stop the loop waiting condition, set the flags, sort all the resources in the system, and stipulate that all process request resources must operate in a certain order to avoid deadlocks.
3. How do I implement threads in Java?
(1) Inherit the thread class
(2) Implement Runnable interface
(3) Implement the callable interface to create thread threads through the Futuretask wrapper
(4) using Executorservice, callable and future to realize multi-threading with return result
4, with runnable or thread?
Java does not support multiple inheritance of classes, but it allows you to invoke multiple interfaces (it is better to call the Runnable interface, of course)
5. What is the difference between the start () and the Run () method in the thread class?
(1) the Start () method is used to start the newly created thread, and start () invokes the run () method internally.
(2) when the run () method is called directly, it is only called in the original thread and no new thread is started.
6. What is the difference between runnable and callable in Java?
The main difference is that the callable call () method can return a value and throw an exception, while the runnable run () method does not have these features
7. What are the volatile variables in Java?
The purpose of the volatile keyword is to guarantee the visibility of variables across multiple threads (visibility is that each thread accesses a volatile modifier variable, and volatile guarantees that the thread can get the current most recent value)
Volatile is the lightest synchronization mechanism available in Java
8. What is thread safety? is vector a thread-safe class?
Thread Safety: If your code is in a process where multiple threads are running concurrently, these threads may run this code at the same time. If the result of each run is the same as the results of a single-threaded run,
And the values of the other variables are the same as expected, which is thread-safe.
Vectors are thread-safe with synchronous methods, and ArrayList similar to it are not thread-safe.
9. How do I stop a thread in Java?
(1) Use the exit flag to cause the thread to exit normally, that is, the thread terminates when the Run method completes.
(2) Use the Stop method to forcibly terminate, but this method is not recommended because stop and suspend and resume are obsolete methods.
(3) Use the interrupt method to break the thread.
10. What is the difference between notify and Notifyall in Java?
The Notify () method does not wake up a specific thread, so only one thread waits for it to be useful. While Notifyall () wakes all threads and allows them to scramble for locks to ensure that at least one thread can continue to run.
11, why wait, notify and notifyall These methods are not inside the thread class?
Because wait, notify, and Notifyall are all lock-level operations, they are defined in the object class because locks belong to objects. If defined in the thread class, it is not obvious which lock the thread is executing.
12. What is a treadlocal variable?
Threadlocal is a special variable in Java. Each thread has a threadlocal that each thread has its own independent variable, and the race condition is completely eliminated.
13. Why wait and notify methods are called in the synchronization block?
The main reason is that the Java API enforces this, and if you don't, your code throws a Illegalmonitorstateexception exception. Another reason is to avoid a race condition between wait and notify.
14. What is a thread pool? Why use it?
Creating threads takes expensive resources and time, and if the task is to create a thread, the response time is longer and the number of threads a process can create is limited. To avoid these problems, create a thread collection during the initialization of a multithreaded application,
These threads are then reused when new tasks are needed instead of creating a new thread, which is called the thread pool, and the threads inside are called worker threads.
Why do I need a thread pool?
(1) The thread pool improves response time for an application.
(2) The thread pool saves the CLR the overhead of creating a full thread for each short life cycle task and can reclaim resources after the task is completed.
(3) The thread pool optimizes threading time slices based on the processes currently running in the system.
(4) The thread pool allows us to open multiple tasks without setting properties for each thread.
(5) The thread pool can be used to resolve the limit on the maximum number of threads to handle a particular request.
15, how to detect whether a thread has a lock?
In Java.lang.Thread, there is a method called Holdslock (), which returns true if the current thread has a lock on a specific object.
16. Which parameter in the JVM is used to control the stack size of the thread
The-XSS parameter is used to control the stack size of the thread.
17. What is the difference between synchronized and Reentrantlock in Java?
Same point:
They are both lock-mode synchronization, and are blocking synchronization, that is, if a thread obtains an object lock, into the synchronization block, other access to the synchronization block of the thread must be blocked outside the synchronization block waiting.
Different points:
1. For synchronized, it is the Java keyword, which is a native syntax-level mutex that needs to be implemented by the JVM.
2.ReentrantLock is a PI-level mutex that is provided after JDK1.5 and requires the lock () and Unlock () methods to be completed with the try/finally statement block.
Synchronized through compilation, the synchronization block will be formed before and after the Monitorenter and monitorexit this two-byte code instruction. When executing the monitorenter instruction, the first attempt is to acquire an object lock.
If the object is not locked, or when the front-thread already has the object lock, the lock calculator plus 1, the corresponding, in the execution of the monitorexit instruction will be the lock calculator minus 1, when the calculator is 0 o'clock, the lock is released.
If an object lock fails, the current thread will block until the object lock is freed by another thread.
The Reentrantlock class offers some advanced features, mainly in the following 3 items:
1. Waiting can be interrupted, the thread holding the lock is not released for a long time, the waiting thread can choose to give up waiting, which is equivalent to synchronized to avoid the deadlock situation.
2. Fair lock, when multiple threads are waiting for the same lock, the lock must be acquired in the order of the time the lock is requested, the synchronized lock is not fair, the Reentrantlock default constructor is created by the non-fair lock, can be set to fair lock by the parameter true, but the performance of fair lock performance is not very good.
3. A lock binds multiple conditions, and a Reentrantlock object can be bound to an object at the same time.
18, there are three threads t1,t2,t3, how to ensure that they are executed sequentially?
T3 first, in the T3 run, call T2.join, let the t2 execution and then execute T3, in T2 run, call T1.join, let T1 execute after the completion of T2
(The Join () method is waiting for the next thread to run after the end of this thread)
19. How does the yield method in the thread class work?
The effect of the yield method is that when a thread uses this method, it takes its CPU time off and lets itself or other threads run.
Process and thread surface questions