Basic application of multithreading

 

1. Threads and Processes

understand concepts

Ø A process is the smallest unit for the operating system to allocate resources (including cpu, memory, disk IO, etc.)

Ø Thread is the basic unit of cpu scheduling and allocation

Ø We open WeChat, the browser is a process

Ø A process may have multiple subtasks. For example, WeChat wants to receive messages and send messages. These subtasks are threads.

Ø Resources are allocated to processes, and threads share process resources.

difference comparison

Compared

process

thread

definition

Process is the running process of an entity in which the program runs, and is an independent unit for the system to allocate and allocate resources

A thread is the smallest scheduling unit in which a process runs and executes

system overhead

Creating undo switching is expensive, and resources need to be reallocated and reclaimed

Only save the contents of a small number of registers, the overhead is small, and the code is executed in the address space of the process

own assets

The basic unit of resource ownership

Basically no resources, only essential resources (program counter, a set of registers and stack)

schedule

The basic unit of resource allocation

Units allocated by independent scheduling

safety

Processes are independent of each other and do not affect each other

Threads share resources under a process and can communicate and influence each other

address space

Independent memory address space given by the system

Consists of related stack registers and thread control table TCB, registers can be used to store local variables within threads

 

 

 

 

 

 

 

The history and advantages of threads

history:

Ø When there is no operating system, a computer only executes one program, at that time, it is a waste of precious computer resources

Ø In order to improve resource utilization (for example, other programs can be executed while waiting for input), in order to improve fairness (different users and programs have equal rights to use resources on the computer), in order to improve convenience (implement multiple tasks When multiple programs are used instead of one program to achieve multiple tasks) the computer has an operating system added to it

Ø Similarly, for the same reason, threads were born. Threads can share the resources of a process.

Advantage:

With the development of technology, multi-processor systems are becoming more and more popular. On a dual-processor system, using only one thread is a waste of resources. We need to harness the power of multiprocessors

 

Three, the application of thread

 

1. Create a thread

Inherit Thread

Implement the Runnable interface

2. Thread priority

Regularity, cpu try to give resources to high priority

Randomness, the higher priority does not necessarily execute the run method first

3. The courtesy of the thread (yield)

Give up the current cpu resource and let other tasks occupy it, but I don't know when to give up, because after giving up, it may start to obtain time slices again

4. Thread interruption (interrupt)

线程对象有一个boolean变量代表是否有中断请求,interrupt方法将线程的中断状态设置会true,但是并没有立刻终止线程,就像告诉你儿子要好好学习一样,但是你儿子怎么样关键看的是你儿子。

线程的合并(join)

主线程和子线程同时运行,满足一定条件后,让子线程先运行至结束

6.判断是否中断

interrupted方法判断当前线程是否中断,清除中断标志

isInterrupt 方法判断线程是否中断,不清除中断标志

 

 

四、线程的生命周期

 

Ø 新建(New):创建后尚未启动的线程

Ø 运行(Runanle):包括了操作系统线程中的Running和Ready,处于此状态的线程 可能正在执行或者等待cpu分配时间片

Ø 无限期等待(Waiting):等待被其他线程显式唤醒,执行wait或者join方法或者LockSupport的park方法

Ø 限期等待(Timed Waiting):一定时间后会由系统自动唤醒

Ø 阻塞(Blocked):等待获取到一个排它锁

Ø 结束(Terminated):线程执行结束

五、守护线程

Ø 线程有两种一种是用户线程,一种是守护线程

Ø 垃圾回收线程是典型的守护线程,当jvm中还有非守护线程,守护线程就一直还在,知道非守护线程不存在了,守护线程才销毁

 

六、总结

Ø 线程提高了资源利用率

Ø 线程的实现可以通过继承Thread类,也可以通过实Runnable接口

Ø 线程中断方式有3种,常用的是interrupt方法,该方法并没有立即中断线程,只是做了一个中断标志

Ø interrupted和isInterrupt两种方法都可以查看线程中断状态,第一种查看的是当前线程的中断状态,第二种查看的该方法调用者的中断状态

Ø interrupted方法会清除中断状态,isInterrupt不会清除中断状态

Ø interrupt方法没有真正中断线程,所以可以在run方法里面判断中断状态,然后通过抛出异常或者return来中断线程

Ø 当线中断状态为true,再进入sleep会抛出异常,反之一样,sleep状态执行interrupt方法,同样会抛出异常

Ø 线程暂停容易将锁占住

Ø 线程具有优先级,可以通过方法设置线程优先级,cpu会将资源尽量给优先级高的线程,但是当优先级差别不大的时候,优先级高的不一定先执行完run方法

Ø 线程有两种,一种用户线程,一种守护线程,直到用户线程都销毁,守护线程才销毁

 

Guess you like

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