Programming protocol -OOP Statute -8

【 force 】 The standards for the use of basic data types and packaging data types are as follows :
1) be-all POJO Class property must use wrapper data type .
2) RPC Method must use wrapper data types for return values and parameters .
3) All local variables 【 recommend 】 Use basic data types .

These types of objects are often used as query objects or result objects , The default values for basic data types are 0, The default value for the reference type is null. In splicing sql When the result is displayed ,null Has a clear meaning , Can not be spliced or displayed , however 0 There are two meanings in the meaning of , Is it worthless or really 0?

Programming protocol -OOP Statute -9

【 force 】 definition DO/DTO/VO etc. POJO Isochronous , Do not set any property defaults

Homology .pojo When being a query object or updating an object , Automatic splicing according to conditions sql In terms of , It's a kind of injury . It has been explained clearly in the original text .

Counterexample : POJO Class gmtCreate The default value is new
Date(); But this attribute does not put a specific value into the data extraction , This field is also updated when other fields are updated , Causes the creation time to be modified to the current time .

Programming protocol - Concurrent processing -3&4

3【 force 】 Thread resources must be provided through the thread pool , Do not allow to explicitly create threads in the application .
explain :
The advantage of using thread pool is to reduce the time spent creating and destroying threads and the overhead of system resources , Solve the problem of insufficient resources . If you do not use thread pools , It may cause the system to create a large number of similar threads, resulting in memory consumption or “ Over switching ” Problems of .

That's not allowed new
Thread(…).start() This form creates and starts threads . Threads are scarce resources , Both creation and destruction come at a cost . Thread pools separate threads from tasks , No matter how many tasks , Can be executed with controllable number of threads , Threads can be reused . meanwhile , The number of threads will not increase because of the increase of tasks , An increase in the number of threads will cause CPU overload , It is very likely to cause the system to die , Even caused jvm No response from other applications .

4【 force 】 Thread pool not allowed Executors To create , But through ThreadPoolExecutor
How , This way of processing makes the students write more clear about the running rules of the thread pool , Avoiding the risk of resource exhaustion .
explain : Executors The disadvantages of the returned thread pool object are as follows :
1) FixedThreadPool and SingleThreadPool:
The allowed request queue length is Integer.MAX_VALUE, A large number of requests may pile up , Which leads to OOM.
2) CachedThreadPool and ScheduledThreadPool:
The number of creation threads allowed is Integer.MAX_VALUE, A large number of threads may be created , Which leads to OOM.

Executors Is a tool class , It has several convenient static methods to help us create thread pools with different characteristics , But these thread pools are risky . These easy ways , Actually, it's all set for us ThreadPoolExecutor Construction parameters of , So is the return result ThreadPoolExecutor Examples of .
ThreadPoolExecutor The construction parameters of , Specific analysis of specific problems , To meet our needs , Its parameter meaning is
int corePoolSize, Number of core threads , Minimum number of threads maintained by thread pool
int maximumPoolSize, Maximum threads
long keepAliveTime, Waiting time from idle to die for threads created outside the number of core threads
TimeUnit unit, Time unit of the previous parameter
workQueue, Waiting queue , Too many tasks , Beyond available threads in core threads , That is, when the core thread can't meet the growing task , New tasks are put into this queue , This queue is full , Will increase the number of threads , Until the number of threads reaches max Limitation of
handler, The core thread is too busy to perform new tasks , New tasks in queue , Queue full , Add new thread until the bus number arrives maximumPoolSize, All threads are busy , New tasks are still being submitted , Will call this reject handler To implement the rejection policy .

Problems needing attention :
If the waiting queue is unbounded , that maximumPoolSize It doesn't work , The later rejection strategy doesn't work either , Assuming the task grows very fast , Queue has unlimited capacity expansion , Memory will burst soon . This is it. FixedThreadPool and SingleThreadPool The problem with .
CachedThreadPool It's not a boundless queue , It uses one SynchronousQueue queue , This queue is a fake one , You can't put anything , You can think of the capacity as 0, Any tasks that exceed will result in new threads , Total threads received max limit , But what's the upper limit ?Integer.MAX_VALUE. Assuming the task grows very fast , Then this thread pool will keep creating new threads to handle new tasks . This can lead to CPU overload , And memory overflow .
3. ScheduledThreadPool similar .

A well-designed thread pool is like this :
1. The number of cores corresponds to the average number of tasks
2. Occasional jitter , Increase in tasks , Use bounded queues (ArrayBlockingQueue Set size ) To buffer tasks
3. max Don't be too big , Bigger than the core number , There are buffer queues ahead . Same as the number of cores
4. Time is designed by default
5. There can be a rejection policy , Why not ? Computer resources are limited , Throwing an exception when you can't handle it is better than supporting it CPU Better than memory .jdk Four built-in strategies are available

a.AbortPolicy, Throw RejectedExecutionException
b.CallerRunsPolicy, Trying to call the run method
c.DiscardOldestPolicy, The oldest waiting tasks will be discarded , Try to perform the current task
d.DiscardPolicy, Quietly discard , No exceptions

Personal feeling , Task submitter under the prompt of throwing exception , It's a good choice .

Next time, let's talk about other issues .

©2019-2020 Toolsou All rights reserved,
mybatis Return result mapping of series ( Essence )2020 year 7 month 15 day Wechat applet assembly Component Use of Element-UI Implementation of secondary packaging TreeSelect Tree drop-down selection component ( Essence )2020 year 6 month 26 day C# Class library Log help class Vue + Element-ui Drop down box for el-select Get extra parameters vue use THREE.js Create a cube that you can control Centos7 lower mysql8.0 To install and change the initial password ;Thread.getContextClassLoader And Thread.getClassLoader() difference Three.js - OrbitControls Orbit control around target target parameter el-select Get selected label value