Hey, hey, I’m back! 😝

A long long time ago, a young man said that the next chapter is going to talk about this ~ (fortunately recorded ha ha otherwise I do not know where to write ((φ(◎ロ◎;) Phi))))

Time slice

A timeslice, also known as a quantum or processor slice, is the microscopic amount of CPU time that a time-sharing operating system allocates to each running process (in the case of a preemption kernel: the time from the start of a process until it is preempted).

Time slices are assigned to each process by the operating system kernel’s scheduler. First, the kernel assigns each process an equal initial slice of time, then each process executes the corresponding slice in turn, and when all processes are running out of time slices, the kernel recalculates and allocates time slices for each process, and so on.

Simply put, the CPU running time is divided up and allocated to different programs to run ~ 😋

Process scheduling algorithm

The following –

Here is a brief introduction to the first come, first served, short jobs and time slice rotation

Friends can roughly understand ~

First come, first served (FCFS)

The CPU obtains the earliest process from the ready queue, allocates CPU resources to it, and runs it

Short assignments

Selecting a queue with the shortest estimated running time improves THE OVERALL CPU utilization and system running efficiency, but some large tasks cannot be scheduled for a long time

Time slice rotation

According to the principle of first come, first served, a task is taken out from the ready queue, and a certain CPU time slice is allocated to it to run. After the time slice is used, a time counter sends out a clock interrupt request. After receiving the clock interrupt request, the scheduler stops the process and puts it into the end of the ready queue. A task is then taken from the head of the queue and allocated a CPU time slice to run


context

Often heard is context switching ~ and Spring between contexts, application contexts, etc

Still have this classmate, please combine the context, answer below this question: “Zhu Ziqing’s” figure “: I buy a few oranges to go. You stay here. Don’t move.” What does it mean to buy oranges?

Hahaha ~ I don’t know if you have realized it, we can find the information in this article

The context in a program is nothing more than the context in which the program is run, for example

Process/thread context switch

Context switching is when the kernel (the core of the operating system) switches processes or threads on the CPU. Information during context switching is stored in a PCB-Process Control Block. A PCB is also called a SwitchFrame. Context switch information is kept in the CPU’s memory until it is used again.

Spring’s context ApplicationContext

When we use Springboot, we often need to get the Spring context ApplicationContext to get the objects created by the Spring container, configuration information, etc

Like this

/** * @author Java4ye */ @Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null) { SpringUtil.applicationContext = applicationContext; Public static applicationContext getApplicationContext() {return applicationContext; Public static Object getBean(String name){return getApplicationContext().getBean(name); Public static <T> T getBean(class <T> clazz){return getApplicationContext().getBean(clazz); } public static <T> T getBean(String name,Class<T> Clazz){return getApplicationContext().getBean(name, clazz); }}Copy the code

Use this method to get Spring container-managed objects

 A a = SpringUtil.getBean(A.class);
Copy the code

The context here can be understood as the Spring container environment ~ 😄

Ahem, ahem, ahem, ahem

How do I get objects to be managed by the Spring container?

Ha ha ha ha here with a simple ~

  1. @Beanannotations
  2. implementationBeanFactoryPostProcessor Interface (this but a knife! ~ cough cough talk about Spring and then bring ~ pit 🕳😝)

Let’s not talk about XML the old-fashioned way


Cough cough don’t talk to finish finish the last bit hurriedly write thread 😂 (knowledge zha so many ah)

Process, thread, coroutine

The relationship is shown as follows:

A process is the basic unit of allocation and scheduling in an operating system

Threads are the smallest unit of operating system scheduling

At least one thread is running in each process!

Why design threads?

Must be the process has big shortcomings 😄 for example

  1. Concurrency, we know that processes have their own memory space, so synchronization between multiple processes becomes very troublesome
  2. Creating and destroying processes is extremely expensive, as you can see from the figure above.

In order to reduce system overhead, there is this thread. You can see that threads are isolated from each other, but share resources in the process, which can solve the data synchronization problem

So why design coroutines?

Threads, while much less expensive than processes, still have their problems

Such as

  1. Thread creation and destruction

  2. Thread switching overhead

There is a mature solution to this problem in Java: thread pool, Netty

Thread this little guy, nicknamed lightweight process, it has its own stack, program counters, registers and other unshared information ~ 😄

If there are too many, not only will there be memory pressure, but the overhead of competing with each other is also a big problem!

In today’s era of high concurrency, is there a more flexible way to increase the concurrency of a system?

Of course, that’s where the coroutine comes in!

Coroutine also known as 👉 user mode thread, no context switch and other system overhead, very high efficiency!

And it takes very little memory, threads are at the MB level, it’s at the KB level

Do more research later and share this coroutine ~ 😝

conclusion

Recent knowledge points are as follows: 😝

Welcome attention, make a friend!! (•̀ ω •́)y

Java4ye programmer 4ye Sauce, very happy to meet you!! 😝

Welcome to follow the blogger’s official account: Java4ye 😋

Let’s start this unexpected meeting! ~

Welcome to leave a message! Thanks for your support! O (≧ ヾ del ≦ *)