20165320 Eighth week study summary

main content

threads and processes

A thread is not a process, but it behaves much like a process. A thread is a smaller execution unit than a process. A process can generate multiple threads during its execution, forming multiple execution threads.

Java's Multithreading Mechanism

1. Multithreading refers to a situation where several execution bodies exist in an application at the same time, and work together according to several different execution threads.

2. Main thread: main thread

3. Thread state and life cycle

新建——运行——中断(四种原因)——死亡

4. Thread scheduling and priority

优先级可以通过setPriority(int grade)方法来调整

Thread class and thread creation

1. Use the Thread class to directly create a thread object. The construction method is:

Thread(Runnable target)

2. The relationship between the target object and the thread

·目标对象与线程完全解耦
String name = Thread.currenThread().getName();
·目标对象组合线程(弱耦合)
Thread.currentThread();

3. About the number of starts of the run method

run方法有可能被中断
waterAmount = waterAmount-m;

Common methods of threading

1.start(): start the thread

2.run(): The execution operation after the thread is called

3.sleep (int millsecond): give up CPU resources

4.isAlive(): return status

5.currentThread(): Returns the used CPU resources

6.interrupt(): wake up the sleeping thread

thread synchronization

Mechanism: When a thread uses the synchronized method, other threads must wait when they want to use it.

coordinating synchronized threads

Use the wait() method to interrupt the execution of the thread, make the thread wait, and temporarily give up the right to use the CPU.

thread union

During the period when a thread A occupies the CPU, it can let other threads call join() to join with this thread.

B.join();
A在运行期间联合了B。

Timer threads and daemon threads

Java provides a very convenient Timer class, the class in the javax.swing package.

After the timer is created, use the method start() of the Timer class to start the timer, stop() to stop the timer, and restart() to restart the timer.

Call the void setDaemon(boolean on) method to set yourself as a daemon thread

thread.setDaemon(true);

Screenshot of script running

Guess you like

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