20165105 Eighth week study summary

 Chapter 12 Learning Summary

process and thread:进程和线程都是一个时间段的描述,是CPU工作时间段的描述。进程是程序的一次动态执行过程;线程是比进程更小的执行单位

   the difference:
          (1) A process is an independent unit of resource allocation and scheduling, while a thread is the basic unit of CPU scheduling
          (2) The same process can include multiple threads, and the threads share the resources (registers, stacks, and contexts) of the entire process, and one process includes at least one thread.
          (3) The creation of the process calls fork or vfork, and the creation of the thread calls pthread_create. After the process ends, all threads it owns will be destroyed, and the end of the thread will not affect the end of other threads in the same process.
          (4) A thread is a two-level light process. Its creation and destruction take much less time than a process. All execution functions in the operating system are performed by creating threads.
          (5) Synchronization and mutual exclusion are generally required when executing in threads, because they share all the resources of the same process
          (6) Threads have their own private attributes TCB, thread id, registers, hardware context, and processes also have their own private attributes process control block PCB, these private attributes are not shared, and are used to mark the flag of a process or a thread
 

Thread state and life cycle

  • The Java language uses objects of the Thread class and its subclasses to represent threads, and the complete life cycle goes through the following states:
    1. new
    2. Running: You must call the start() method to notify the JVM to run
    3. Disruption: Four Reasons Disruption
    4. Death: There are two reasons, one is the end of normal operation; the other is that the thread is forced to end early, that is, the run() method is forced to end.
  • Common method
    • start();
    • run(), before the thread ends run(), do not let the thread call the start() method, otherwise an IllegalThreadStateException will occur
    • sleep(int millsecond) must call the sleep method in a try-catch block
    • isAlive();
    • currentThread() returns the thread that is currently using CPU resources
    • interrupt();
  • Thread synchronization mechanism: when a thread A uses the synchronized method, it must wait until thread A finishes using the synchronized method
  • You cannot use wait(), notify(), notifyAll() in unsynchronized methods
  • A uses Join() to join B during operation, then the A thread immediately executes the terminal, and waits for the execution of B to end, and A re-queues to wait for CPU resources
  • The AWT-EventQueue thread is responsible for handling GUI events, and the AWT-Windows thread is responsible for drawing forms or components to the desktop

Code hosting: https://gitee.com/BESTI-IS-JAVA-2018/besti-Java20165105

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324767982&siteId=291194637