20165104 Meng Fanbin - homework for the eighth week

20165104 Meng Fanbin-JAVA eighth week homework

Textbook learning content summary

process and thread

  • Threads are smaller than processes, and multiple threads can be generated during the execution of a process.

    Java's multithreading mechanism. Java has built-in support for multithreading. Our computer can only execute one of the threads at any given time, and the Java Virtual Machine simply switches quickly from one thread to another.

  • When there are other threads in the main method, the JVM does not end the Java application until all threads in the Java application have ended.
  • The Java language uses objects of the Thread class and its subclasses to represent threads. A newly created thread generally goes through four states: ① Create a Thread object; ② After the object is created in the first step, the object only has a memory dimension, and the start() method needs to be called again to make it a new thread. If the thread is an object of Thread, the run() method in the class is executed immediately, and the program must override the run() method in the subclass. Note that before the thread ends the run() method, do not let the thread call the start() method again, otherwise an IllegalThreadStateException will be thrown. ③ Thread interruption: switch to other threads; execute the sleep(int time) method, after time milliseconds, the thread will be re-queued to continue running from the interruption; execute the wait() method, the thread enters the waiting state, at this time , other threads call the notify() method to re-queue the thread in the waiting state to continue running from where it left off; it cannot enter the queued queue when an operation enters the blocking state. ④ There are two reasons for a thread to "die". One is that all the work of the thread has been run normally; the other is that the run() method is forced to end. When a thread "dies", the undead threads continue to occupy the CPU in turn.
  • Threads can be prioritized (using the setPriority() method), and low-priority threads will only be executed when high-priority threads have finished executing. You can use the getPriority() method to get the priority of the thread, which can only be between 1 and 10.

    Thread class and thread creation.

  • You can use the Thread class or subclass to create a thread object. Note that the subclass must override the run() method. At the same time, the subclass can add new member variables and methods to make the thread have specific functions.
  • If the target object is completely decoupled from the thread (the column in which the target object was created does not contain a reference to the thread object), the target object can use

    String name=Thread.currentThread().getName();

    to get the thread name currently using CPU resources; if the target object combines threads, the target object can use

    String name=Thread.currentThread();来获得当前使用CPU资源的线程引用。
  • If there is a "sleep" process for one of the threads in the target object, when the computer runs the program, it may have different results. Because if the thread has not finished running the run() method after reaching the specified number of milliseconds, it will stop first, and then continue to run after the sleep time is over.

    Common methods of threading

  • ①start() method: Only newly created threads can call this method.
  • ②run() method: Whether the method ends is an important criterion for judging whether the thread ends. Only when the method finishes running, the thread can call the start() method again.
  • ③sleep(int millsecond) method: The thread can call this method to put the program to sleep, and "wake up" until the number of seconds specified by the parameter has passed. This method must be called in a try-catch statement, because an InterruptedException will be thrown if the thread is interrupted while sleeping.
  • ④isAlive() method: This method is used to judge whether the thread is running, if so, return true, if not, return false. Note that an already running thread does not assign entities to the thread without entering the dead state.
  • ⑤The currentThread() method is a class method in the Thread class and returns the thread currently using the CPU.
  • ⑥The interrupt() method is used to "wake up" the thread that is in sleep state by calling the sleep method.

    thread synchronization

  • If several threads need to use a method at the same time, in order to avoid confusion, you can use synchronized to decorate. This way, the method can only be called in turn, and no other threads can call the method until the thread that is calling the method has finished using the method.

    coordinating synchronized threads

  • When a variable is used in the synchronization method used by a thread, and this variable needs to be modified by other threads to meet the requirements of this thread, the wait() method can be used to interrupt the execution of the thread and allow other threads to use this synchronization method (sleep method cannot do this function). If there is a thread that does not need to wait when running a synchronized method, then you need to apply the notifyAll() method when the method is used up to notify all other waiting threads to end waiting. The wait(), notify(), notifyAll() methods can only be used in synchronous threads.

    thread union

  • While thread A is running, if other thread B calls the join() method to join thread A, thread A will be interrupted immediately, and thread A will not be re-queued until thread B finishes running. If thread B has ended, calling the join() method will have no effect.

    GUI thread

  • When Java includes a GUI (Graphical User Interface), multiple threads are started.

    timer thread

  • The Timer class in the javax.swing package in Java can be used to create a timer: Timer(int a,Object b);, a is milliseconds, b is a monitor (monitor must be a component class), every a millisecond will be Execute b once. Note that there is also a class named Timer in the java.util package. Care should be taken to avoid confusion when using the class name.

    Daemon thread

  • Threads are non-daemon threads by default, and threads must set whether they are daemon threads before running. You can call voidsetDaemon(boolean on) to set yourself up as a daemon thread. When all non-daemon threads finish running, daemon threads must end with them.

Guess you like

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