"Java Programming" Week 8 Learning Summary 20165218 2017-2018-1

20165218 2017-2018-1 "Java Programming" Week 8 Learning Summary

Textbook learning content summary

Chapter 12 Java Multithreading Mechanisms

threads in java

  • A computer can only execute one thread at any given time , and the multi-threading mechanism refers to the Java virtual machine quickly switching threads so that they execute in turn
  • ThreadObjects of class and its subclasses represent threads
  • A thread's life cycle has four states : new, running, interrupted, and dead
  • setPriority(int grade)method to adjust thread priority

Common methods of threading

  • start()Only threads in the newly created state can call
  • run()There is no specific content, the user program needs to create a subclass of its own Thread class and override the run()method; after execution, the thread dies
  • sleep(int millsecond)Called to temporarily put a high-priority thread to sleep; must be try-catchused in
  • isAlive()The method returns when the thread is in the new and dead state, and returns falsewhen it is in the running statetrue
  • currentThread()Returns the thread that is using CPU resources
  • interrupt()Used to "wake up" sleepthe thread that called the method

thread synchronization

The method of using synchronized modification must comply with the synchronization mechanism: when a thread A uses the synchronized method, other threads must wait until thread A is finished using this method.


coordinating synchronized threads

  • wait()The method can interrupt the execution of the thread and temporarily give up the right to use the CPU
  • wait()method must notifyAll()wake up with method
  • wait()Methods, notify(), notifyAll()are final methods in the Object class and are not allowed to be overridden

thread union

//线程A在使用CPU资源的期间
B.join();

A interrupts immediately until thread B finishes executing


code hosting

(screenshot of the running result of the statistics.sh script)

Summary of last week's exam mistakes

  • wrong question 1

Connection is a class name not a method name

  • wrong question 2

  • ...

References

Guess you like

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