Thread related description

1. Process: Each process has independent code and data space, a process contains multiple threads, and a process is the smallest unit of resource allocation.
   Thread: The same type of threads share code and data space, and each thread has an independent running stack and program counter. A thread is the smallest unit of CPU scheduling.
   Main thread: The thread generated by the JVM calling the program main().
   Current thread: Thread.currentThread()
   Background thread: daemon thread, the difference from user thread is whether to wait for the main thread to end depending on the end of the main thread. You can use isDaemon() and setDaemon() to judge and set
   the foreground thread: the thread that accepts the background thread service.
2.
   Threads and processes are divided into five stages: creation, ready, running, blocking, and termination.
   Multiprocessing means that the operating system can run multiple tasks (programs) at the same time.
   Multithreading refers to the execution of multiple sequential flows in the same program.
3. Multi-threaded   Thread Runable interface Callable interface
  is implemented in java. 4.    Each object related to locks has one and only one lock (lock) associated with it.    Achieving synchronization is expensive and may even cause deadlocks.    The lock obtained by the synchronized keyword is the object (lock).    Scope: If the properties, methods, and code blocks modified by it are static, all instance objects of the class are associated with the same lock. Otherwise, the locks between the various instances are irrelevant.    Whoever gets the lock can execute the code it controls.








   A thread acquires a lock. When accessing a synchronization method on another object in a synchronization method, it will acquire the two object locks

5. The commonly used function
   sleep(long millis); yields the CPU, the thread sleeps, and the lock is maintained
   join(); The main thread where it is located needs to wait for the thread to terminate before it can end.
   yield() If a thread of the same priority is in a runnable state, it will return the currently running thread to a runnable state, and reschedule
   setPriority(): Change the thread priority, by default there are three levels of 1, 5, and 10
   interrupt( ); send an interrupt signal to the thread.
   obj.wait(), obj.notify() must be used together with synchronized(obj), wait() and notify() operate on the thread that has acquired the obj lock.
      wait() is to release the object lock, while the thread sleeps,
      notify() is the wake-up operation for the object lock, after the thread ends normally, the jvm randomly selects a thread from the thread of the wait() object lock, and assigns its object lock

6. In java, at least two threads are started each time the program runs, one is the main thread and the other is the garbage collection thread.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326987134&siteId=291194637