文章列表

Tomcat http强制跳转https

作者:じ☆ve宝贝

** 最新tomcat http强制跳转https [最新tomcat http强制跳转https](https://www.studyjava.cn/post/750),支持tomcat配置301永久重定向和302临时重定向。匹配百度站长全站开启https认证 ** 1.编辑tomcat/conf/server.xml找到关于SSL的字段 ``` <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="D:\\home\\tomcat.keystore" keystorePass="123456" truststoreFile="D:\\home\\tomcat.keystore" truststorePass="123456" /> ``` (tomcat要与生成的服务端证书名一致) 属性说明: clientAuth:设置是否双向验证,默认为false,设置为true代表双向验证 keystoreFile:服务器证书文件路径 keystorePass:服务器证书密码 truststoreFile:用来验证客户端证书的根证书,此例中就是服务器证书 truststorePass:根证书密码 2.编辑 tomcat/conf/web.xml在</welcome- file-list>添加: ``` <login-config> <!-- Authorization setting for SSL --> <auth-method>CLIENT-CERT</auth-method> <realm-name>Client Cert Users-only Area</realm-name> </login-config> <security-constraint> <!-- Authorization setting for SSL --> <web-resource-collection > <web-resource-name >SSL</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> ```

使用存储过程生成测试数据

作者:じ☆ve宝贝

## 存储过程: ``` DELIMITER $$ DROP PROCEDURE IF EXISTS `school`$$ CREATE PROCEDURE `school`() BEGIN DECLARE v_cnt DECIMAL (10) DEFAULT 0 ; DECLARE id DECIMAL (10) DEFAULT 0 ; dd:LOOP INSERT INTO user_school VALUES (NULL,id+1,'清华'), (NULL,id+2,'北大'), (NULL,id+3,'北航'), (NULL,id+4,'北电'), (NULL,id+5,'北邮'), (NULL,id+6,'华南理工'), (NULL,id+7,'花呗理工'), (NULL,id+8,'浙大'), (NULL,id+9,'汉大帮'), (NULL,id+10,'秘书帮') ; COMMIT; SET v_cnt = v_cnt+10 ; IF v_cnt = 10000 THEN LEAVE dd; END IF; SET id = id+10; END LOOP dd ; END$$ DELIMITER ; ``` 注意 IF v_cnt = 10000(插入条数,此处为10000条) THEN LEAVE dd; ``` 执行存储过程:call school ```

MySQL快速导入文件LOAD DATA INFILE

作者:じ☆ve宝贝

**MySQL导入文本数据LOAD DATA INFILE语法** ``` LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_name [PARTITION (partition_name [, partition_name] ...)] [CHARACTER SET charset_name] [{FIELDS | COLUMNS} [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char'] ] [LINES [STARTING BY 'string'] [TERMINATED BY 'string'] ] [IGNORE number {LINES | ROWS}] [(col_name_or_user_var [, col_name_or_user_var] ...)] [SET col_name={expr | DEFAULT}, [, col_name={expr | DEFAULT}] ...] ``` ** FIELDS参数含义:** TERMINATED BY ',' //描述字段的分隔符,默认情况下是tab字符(\\t) ENCLOSED BY '"' //描述的是字段的括起字符,比方以双引号括起每一个字段 ESCAPED BY '\\' //描述的转义字符,默认的是反些杠(backslash:\\ ) ** LINES参数含义:** TERMINATED BY '\n' // 描述行分隔符 STARTING BY '' // 行前缀,只加载该前缀开头的数据 实例: ``` mysql>LOAD DATA INFILE "${value}" INTO TABLE databases.table_name; ``` 注意${value}值查找方法[查找LOAD DATA INFILE文件存放目录](http://www.studyjava.cn/topic/view/a9f55279842f4918896f4831c1cb9cf8.html) ``` //LOCAL参数表示文件是本地的文件,服务器是远程已经登陆的服务器 mysql>LOAD DATA LOCAL INFILE "${value}" INTO TABLE databases.table_name; ``` ``` //设置插入语句的优先级(LOW_PRIORITY 低优先级),LOAD DATA语句的执行将会被延迟,直到没有其它的客户端读取表 mysql>LOAD DATA LOW_PRIORITY INFILE "${value}" INTO TABLE databases.table_name; ``` ``` //插入数据的时候,忽略文件与数据表中重复的键值 mysql>LOAD DATA LOW_PRIORITY INFILE "${value}" IGNORE INTO TABLE databases.table_name; ``` ``` //插入数据的时候,替代文件与数据表中重复的键值 mysql>LOAD DATA LOW_PRIORITY INFILE "${value}" REPLACE INTO TABLE databases.table_name; ``` ``` //字段以逗号分割,字段内容用双引号标记开始和结束 mysql>LOAD DATA INFILE "${value}" INTO TABLE databases.table_name FIELDS TERMINATED BY ',' ENCLOSED BY '"'; ``` ``` //按指定的列把文件导入到数据库中 mysql>LOAD DATA INFILE "${value}" INTO TABLE databases.table_name (column1, column2, column3); ``` ## 测试: **导出数据到文件:** ``` SELECT * INTO OUTFILE "C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/test.sql" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY'"' LINES TERMINATED BY '\n' FROM user ; ``` **从文件中加载数据** ``` LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/test.sql' REPLACE INTO TABLE test.ouser FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' ; ```

thymeleaf 设置不校验html标签

作者:じ☆ve宝贝

> 默认配置下,thymeleaf对.html的内容要求很严格,比如,如果少封闭符号/,就会报错而转到错误页。也比如你在使用Vue.js这样的库,然后有<div v-cloak></div>这样的html代码,也会被thymeleaf认为不符合要求而抛出错误。 #### 通过设置thymeleaf模板可以解决这个问题,下面是具体的配置: **properties中如下** ``` spring.thymeleaf.cache=false spring.thymeleaf.mode=LEGACYHTML5 ``` **ymal中如下** ``` spring: thymeleaf: cache: false mode: LEGACYHTML5 ``` LEGACYHTML5需要搭配一个额外的库NekoHTML才可用 项目中使用的构建工具是Maven添加如下的依赖即可完成: ``` <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency> ```

【idea】jdk、maven、Git环境配置

作者:じ☆ve宝贝

## jdk配置 ![](/upload/f1ead993cc3746bea61018acae0631ad.png) ## Maven配置 ![](/upload/6a52cff86833463ea924ca3b8e325b95.jpg) ## Git配置 [idea使用GIT设置](https://www.studyjava.cn/topic/view/ed32ed164e224e2997575a0d1ff0a482.html) ## 全局设置编码UTF-8 ![](/upload/f91fed1bf6904b3bb6d1bec2d2892709.png) 建议将底部的 Transparent native-to-ASCII conversion 也勾选上,有Unicode显示的就可以自动转化为其应该显示的语言了. ## 设置显示行号 ![](/upload/4e48c45c34e544288d9e073261ce5b13.png)

sysbench 自定义表结构lua脚本

作者:じ☆ve宝贝

my_common.lua ``` -- Copyright (C) 2006-2017 Alexey Kopytov <akopytov@gmail.com> -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -- ----------------------------------------------------------------------------- -- Common code for OLTP benchmarks. -- ----------------------------------------------------------------------------- function init() assert(event ~= nil, "this script is meant to be included by other OLTP scripts and " .. "should not be called directly.") end if sysbench.cmdline.command == nil then error("Command is required. Supported commands: prepare, prewarm, run, " .. "cleanup, help") end -- Command line options sysbench.cmdline.options = { table_size = {"Number of rows per table", 10000}, range_size = {"Range size for range SELECT queries", 100}, tables = {"Number of tables", 1}, point_selects = {"Number of point SELECT queries per transaction", 10}, simple_ranges = {"Number of simple range SELECT queries per transaction", 1}, sum_ranges = {"Number of SELECT SUM() queries per transaction", 1}, order_ranges = {"Number of SELECT ORDER BY queries per transaction", 1}, distinct_ranges = {"Number of SELECT DISTINCT queries per transaction", 1}, index_updates = {"Number of UPDATE index queries per transaction", 1}, non_index_updates = {"Number of UPDATE non-index queries per transaction", 1}, delete_inserts = {"Number of DELETE/INSERT combination per transaction", 1}, range_selects = {"Enable/disable all range SELECT queries", true}, auto_inc = {"Use AUTO_INCREMENT column as Primary Key (for MySQL), " .. "or its alternatives in other DBMS. When disabled, use " .. "client-generated IDs", true}, skip_trx = {"Don't start explicit transactions and execute all queries " .. "in the AUTOCOMMIT mode", false}, secondary = {"Use a secondary index in place of the PRIMARY KEY", false}, create_secondary = {"Create a secondary index in addition to the PRIMARY KEY", true}, mysql_storage_engine = {"Storage engine, if MySQL is used", "myisam"}, pgsql_variant = {"Use this PostgreSQL variant when running with the " .. "PostgreSQL driver. The only currently supported " .. "variant is 'redshift'. When enabled, " .. "create_secondary is automatically disabled, and " .. "delete_inserts is set to 0"} } -- Prepare the dataset. This command supports parallel execution, i.e. will -- benefit from executing with --threads > 1 as long as --tables > 1 function cmd_prepare() local drv = sysbench.sql.driver() local con = drv:connect() for i = sysbench.tid % sysbench.opt.threads + 1, sysbench.opt.tables, sysbench.opt.threads do create_table(drv, con, i) end end -- Preload the dataset into the server cache. This command supports parallel -- execution, i.e. will benefit from executing with --threads > 1 as long as -- --tables > 1 -- -- PS. Currently, this command is only meaningful for MySQL/InnoDB benchmarks function cmd_prewarm() local drv = sysbench.sql.driver() local con = drv:connect() assert(drv:name() == "mysql", "prewarm is currently MySQL only") -- Do not create on disk tables for subsequent queries con:query("SET tmp_table_size=2*1024*1024*1024") con:query("SET max_heap_table_size=2*1024*1024*1024") for i = sysbench.tid % sysbench.opt.threads + 1, sysbench.opt.tables, sysbench.opt.threads do local t = "sbtest" .. i print("Prewarming table " .. t) con:query("ANALYZE TABLE sbtest" .. i) con:query(string.format( "SELECT AVG(id) FROM " .. "(SELECT * FROM %s FORCE KEY (PRIMARY) " .. "LIMIT %u) t", t, sysbench.opt.table_size)) con:query(string.format( "SELECT COUNT(*) FROM " .. "(SELECT * FROM %s WHERE cn_name LIKE '%%0%%' LIMIT %u) t", t, sysbench.opt.table_size)) end end -- Implement parallel prepare and prewarm commands sysbench.cmdline.commands = { prepare = {cmd_prepare, sysbench.cmdline.PARALLEL_COMMAND}, prewarm = {cmd_prewarm, sysbench.cmdline.PARALLEL_COMMAND} } -- Template strings of random digits with 11-digit groups separated by dashes -- 10 groups, 119 characters local c_value_template = "###########-###########-###########-" .. "###########-###########-###########-" .. "###########-###########-###########-" .. "###########" -- 5 groups, 59 characters local pad_value_template = "###########-###########-###########-" .. "###########-###########" function get_c_value() return sysbench.rand.string(c_value_template) end function get_pad_value() return sysbench.rand.string(pad_value_template) end function create_table(drv, con, table_num) local id_index_def, id_def local engine_def = "" local extra_table_options = "" local query if sysbench.opt.secondary then id_index_def = "KEY xid" else id_index_def = "PRIMARY KEY" end if drv:name() == "mysql" or drv:name() == "attachsql" or drv:name() == "drizzle" then if sysbench.opt.auto_inc then id_def = "INTEGER NOT NULL AUTO_INCREMENT" else id_def = "INTEGER NOT NULL" end engine_def = "/*! ENGINE = " .. sysbench.opt.mysql_storage_engine .. " */" extra_table_options = mysql_table_options or "" elseif drv:name() == "pgsql" then if not sysbench.opt.auto_inc then id_def = "INTEGER NOT NULL" elseif pgsql_variant == 'redshift' then id_def = "INTEGER IDENTITY(1,1)" else id_def = "SERIAL" end else error("Unsupported database driver:" .. drv:name()) end print(string.format("Creating table 'sbtest%d'...", table_num)) query = string.format([[ CREATE TABLE sbtest%d( `id` char(100) DEFAULT NULL, `cn_name` varchar(200) DEFAULT NULL COMMENT '中文名', `cn_short` varchar(200) DEFAULT NULL COMMENT '中文简称', `en_name` varchar(200) DEFAULT NULL COMMENT '英文名', `en_short` varchar(200) DEFAULT NULL COMMENT '英文简称', `cn_once` varchar(200) DEFAULT NULL COMMENT '曾用名', `credit_code` varchar(200) DEFAULT NULL COMMENT '统一信用代码', `reg_institute` varchar(200) DEFAULT NULL COMMENT '登记机关', `scale` varchar(200) DEFAULT NULL COMMENT '公司规模', `organize_code` varchar(200) DEFAULT NULL COMMENT '企业注册码', `registration_number` varchar(200) DEFAULT NULL COMMENT '工商注册号', `legal_person` varchar(200) DEFAULT NULL COMMENT '法人', `company_type` varchar(200) DEFAULT NULL COMMENT '企业控股: 1:', `register_type` varchar(200) DEFAULT NULL COMMENT '企业注册类型 ', `estiblish_time` varchar(200) DEFAULT NULL COMMENT '营业期限开始', `to_time` varchar(200) DEFAULT NULL COMMENT '营业期限截止', `check_date` varchar(200) DEFAULT NULL COMMENT '发照日期', `area_type` varchar(200) DEFAULT NULL COMMENT '地区', `reg_location` varchar(200) DEFAULT NULL COMMENT '企业地址 ', `business_scope` varchar(200) DEFAULT NULL COMMENT '经营范围', `web_url` varchar(200) DEFAULT NULL COMMENT '网址', `company_logo` varchar(200) DEFAULT NULL COMMENT 'logo', `cn_desc` varchar(200) DEFAULT NULL COMMENT '中文简介', `en_desc` varchar(200) DEFAULT NULL COMMENT '中文简介', `begin_time` varchar(200) DEFAULT NULL COMMENT '成立时间', `stop_time` varchar(200) DEFAULT NULL COMMENT '停业时间', `registered_capital` varchar(200) DEFAULT NULL COMMENT '注册资本', `currency_type` varchar(200) DEFAULT NULL COMMENT '注册资本货币类型 ', `commerce_status` varchar(200) DEFAULT NULL COMMENT '商业状态', `remark` varchar(510) DEFAULT NULL, `creater_id` varchar(200) DEFAULT NULL COMMENT '修改人', `create_time` varchar(200) DEFAULT NULL COMMENT '创建时间', `status` varchar(200) DEFAULT NULL COMMENT '1:正常 2删除 3...', `modifier_id` varchar(200) DEFAULT NULL COMMENT '修改人', `modify_time` varchar(200) DEFAULT NULL COMMENT '修改时间', PRIMARY KEY (id) ) %s %s]], table_num, engine_def, extra_table_options) con:query(query) if (sysbench.opt.table_size > 0) then print(string.format("Inserting %d records into 'sbtest%d'", sysbench.opt.table_size, table_num)) end if sysbench.opt.auto_inc then query = "INSERT INTO sbtest" .. table_num .. "(id, cn_name, cn_short, en_name, en_short, cn_once, credit_code, reg_institute, scale, organize_code, registration_number, legal_person, company_type, register_type, estiblish_time, to_time, check_date, area_type, reg_location, business_scope, web_url, company_logo, cn_desc, en_desc, begin_time, stop_time, registered_capital, currency_type, commerce_status, remark, creater_id, create_time, status, modifier_id, modify_time) VALUES" else query = "INSERT INTO sbtest" .. table_num .. "(id, cn_name, cn_short, en_name, en_short, cn_once, credit_code, reg_institute, scale, organize_code, registration_number, legal_person, company_type, register_type, estiblish_time, to_time, check_date, area_type, reg_location, business_scope, web_url, company_logo, cn_desc, en_desc, begin_time, stop_time, registered_capital, currency_type, commerce_status, remark, creater_id, create_time, status, modifier_id, modify_time) VALUES" end con:bulk_insert_init(query) local c_val local pad_val for i = 1, sysbench.opt.table_size do c_val = get_c_value() pad_val = get_pad_value() if (sysbench.opt.auto_inc) then query = string.format("('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size), c_val,get_pad_value(),getTimeStamp(),get_pad_value(),getTimeStamp(),get_pad_value(),getTimeStamp(), pad_val,guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size),guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size),get_pad_value(),get_pad_value(),getTimeStamp()) else query = string.format("('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size), c_val,get_pad_value(),getTimeStamp(),get_pad_value(),getTimeStamp(),get_pad_value(),getTimeStamp(), pad_val,guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size),guid(),get_pad_value(),get_pad_value(),getTimeStamp(), sb_rand(1, sysbench.opt.table_size),get_pad_value(),get_pad_value(),getTimeStamp()) end con:bulk_insert_next(query) end con:bulk_insert_done() if sysbench.opt.create_secondary then print(string.format("Creating a secondary index on 'sbtest%d'...", table_num)) con:query(string.format("CREATE INDEX k_%d ON sbtest%d(cn_name)", table_num, table_num)) end end local t = sysbench.sql.type local stmt_defs = { point_selects = { "SELECT cn_name FROM sbtest%u WHERE estiblish_time=?", {t.VARCHAR, 100}}, simple_ranges = { "SELECT cn_name FROM sbtest%u WHERE estiblish_time BETWEEN ? AND ?", {t.VARCHAR, 100}, {t.VARCHAR, 100}}, sum_ranges = { "SELECT SUM(cn_short) FROM sbtest%u WHERE estiblish_time BETWEEN ? AND ?", {t.VARCHAR, 100}, {t.VARCHAR, 100}}, order_ranges = { "SELECT cn_name FROM sbtest%u WHERE estiblish_time BETWEEN ? AND ? ORDER BY cn_name", {t.VARCHAR, 100}, {t.VARCHAR, 100}}, distinct_ranges = { "SELECT DISTINCT cn_name FROM sbtest%u WHERE estiblish_time BETWEEN ? AND ? ORDER BY cn_name", {t.VARCHAR, 100}, {t.VARCHAR, 100}}, index_updates = { "UPDATE sbtest%u SET en_name='test' WHERE estiblish_time=?", {t.VARCHAR, 100}}, non_index_updates = { "UPDATE sbtest%u SET cn_name='non_index_update' WHERE estiblish_time=?", {t.VARCHAR, 100}}, deletes = { "DELETE FROM sbtest%u WHERE estiblish_time=?", {t.VARCHAR, 100}}, inserts = { "INSERT INTO sbtest%u (id, cn_name, cn_short, en_name, estiblish_time) VALUES (?, ?, ?, ?, ?)", {t.VARCHAR, 100}, {t.VARCHAR, 200}, {t.VARCHAR, 200}, {t.VARCHAR, 200}, {t.VARCHAR, 200}}, } function prepare_begin() stmt.begin = con:prepare("BEGIN") end function prepare_commit() stmt.commit = con:prepare("COMMIT") end function prepare_for_each_table(key) for t = 1, sysbench.opt.tables do stmt[t][key] = con:prepare(string.format(stmt_defs[key][1], t)) -- print(string.format(stmt_defs[key][1], t)) local nparam = #stmt_defs[key] - 1 if nparam > 0 then param[t][key] = {} end -- print(string.format('----byte,typeby=%s,len=%s,key=%s',type(btype),len,key)) for p = 1, nparam do local btype = stmt_defs[key][p+1] local len if type(btype) == "table" then len = btype[2] btype = btype[1] end -- print(string.format('%s,%s---', btype,sysbench.sql.type.VARCHAR)) if btype == sysbench.sql.type.VARCHAR or btype == sysbench.sql.type.CHAR then -- print(string.format('----cahr,typeby=%s,key=%s,btype=%s,len=%s',type(btype),key,btype,len)) param[t][key][p] = stmt[t][key]:bind_create(btype, len) else -- print(string.format('----nochar,typeby=%s,key=%s,btype=%s',type(btype),key,btype)) param[t][key][p] = stmt[t][key]:bind_create(btype) end end if nparam > 0 then stmt[t][key]:bind_param(unpack(param[t][key])) end end end function prepare_point_selects() prepare_for_each_table("point_selects") end function prepare_simple_ranges() prepare_for_each_table("simple_ranges") end function prepare_sum_ranges() prepare_for_each_table("sum_ranges") end function prepare_order_ranges() prepare_for_each_table("order_ranges") end function prepare_distinct_ranges() prepare_for_each_table("distinct_ranges") end function prepare_index_updates() prepare_for_each_table("index_updates") end function prepare_non_index_updates() prepare_for_each_table("non_index_updates") end function prepare_delete_inserts() prepare_for_each_table("deletes") prepare_for_each_table("inserts") end function thread_init() drv = sysbench.sql.driver() con = drv:connect() -- Create global nested tables for prepared statements and their -- parameters. We need a statement and a parameter set for each combination -- of connection/table/query stmt = {} param = {} for t = 1, sysbench.opt.tables do stmt[t] = {} param[t] = {} end -- This function is a 'callback' defined by individual benchmark scripts prepare_statements() end function thread_done() -- Close prepared statements for t = 1, sysbench.opt.tables do for k, s in pairs(stmt[t]) do stmt[t][k]:close() end end if (stmt.begin ~= nil) then stmt.begin:close() end if (stmt.commit ~= nil) then stmt.commit:close() end con:disconnect() end function cleanup() local drv = sysbench.sql.driver() local con = drv:connect() for i = 1, sysbench.opt.tables do print(string.format("Dropping table 'sbtest%d'...", i)) con:query("DROP TABLE IF EXISTS sbtest" .. i ) end end local function get_table_num() return sysbench.rand.uniform(1, sysbench.opt.tables) end local function get_id() return string.format(sysbench.rand.default(1, sysbench.opt.table_size)) end function begin() for t = 1, sysbench.opt.tables do for k, s in pairs(stmt[t]) do -- print(k,s) end end stmt.begin:execute() end function commit() stmt.commit:execute() end function execute_point_selects() local tnum = get_table_num() local i for i = 1, sysbench.opt.point_selects do param[tnum].point_selects[1]:set(get_id()) stmt[tnum].point_selects:execute() end end local function execute_range(key) local tnum = get_table_num() for i = 1, sysbench.opt[key] do local id = get_id() param[tnum][key][1]:set(id) param[tnum][key][2]:set(guid()) stmt[tnum][key]:execute() end end function execute_simple_ranges() execute_range("simple_ranges") end function execute_sum_ranges() execute_range("sum_ranges") end function execute_order_ranges() execute_range("order_ranges") end function execute_distinct_ranges() execute_range("distinct_ranges") end function execute_index_updates() local tnum = get_table_num() for i = 1, sysbench.opt.index_updates do param[tnum].index_updates[1]:set(get_id()) stmt[tnum].index_updates:execute() end end function execute_non_index_updates() local tnum = get_table_num() for i = 1, sysbench.opt.non_index_updates do -- param[tnum].non_index_updates[1]:set_rand_str(c_value_template) -- param[tnum].non_index_updates[2]:set(guid()) param[tnum].non_index_updates[1]:set(get_id()) stmt[tnum].non_index_updates:execute() end end function execute_delete_inserts() local tnum = get_table_num() for i = 1, sysbench.opt.delete_inserts do -- print(i) local id = get_id() local k = get_id() local uid = get_id(); param[tnum].deletes[1]:set(uid) param[tnum].inserts[1]:set(uid) param[tnum].inserts[2]:set(k) param[tnum].inserts[3]:set_rand_str(c_value_template) param[tnum].inserts[4]:set_rand_str(pad_value_template) param[tnum].inserts[5]:set(uid) stmt[tnum].deletes:execute() stmt[tnum].inserts:execute() end end function guid() local seed = { '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j', 'k','l','m','n','o','p','q','r','s','t', 'u','v','w','x','y','z' } local sid = "" for i=1, 32 do -- math.randomseed(tostring(os.time()):reverse():sub(1, 6)) sid = sid .. seed[random()] end return string.format('%s%s%s%s%s', string.sub(sid, 1, 8), string.sub(sid, 9, 12), string.sub(sid, 13, 16), string.sub(sid, 17, 20), string.sub(sid, 21, 32) ) end function random() local ret=0 math.randomseed(os.time()) for i=1,3 do n = math.random(1,36) ret=n end return ret end function getTimeStamp() local t = os.time(); return os.date("%Y-%m-%d %H:%M:%S",t/1000) end ``` ## my_read_write.lua ``` #!/usr/bin/env sysbench -- Copyright (C) 2006-2017 Alexey Kopytov <akopytov@gmail.com> -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -- ---------------------------------------------------------------------- -- Read/Write OLTP benchmark -- ---------------------------------------------------------------------- require("my_common") function prepare_statements() if not sysbench.opt.skip_trx then prepare_begin() prepare_commit() end prepare_point_selects() if sysbench.opt.range_selects then prepare_simple_ranges() prepare_sum_ranges() prepare_order_ranges() prepare_distinct_ranges() end prepare_index_updates() prepare_non_index_updates() prepare_delete_inserts() end function event() if not sysbench.opt.skip_trx then begin() end execute_point_selects() if sysbench.opt.range_selects then execute_simple_ranges() execute_sum_ranges() execute_order_ranges() execute_distinct_ranges() end execute_index_updates() execute_non_index_updates() execute_delete_inserts() if not sysbench.opt.skip_trx then commit() end end ``` ## company_read_write.sh 执行脚本 ``` #!/bin/bash read -t 30 -p "请输入运行方式(prepare/run/cleanup)": status sysbench /data/lua/my_read_write.lua \ --mysql-host=127.0.0.1 \ --mysql-port=3306 \ --mysql-user=root \ --mysql-password='V5kNP47u28450Gpt' \ --mysql-db=sbtest \ --db-driver=mysql \ --tables=1 \ --table-size=1000000 \ --report-interval=10 \ --threads=128 \ --time=120 \ $status ```

揭开Java 泛型类型擦除神秘面纱

作者:微信小助手

<section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"></section> <section data-role="outer" label="Powered by 135editor.com" data-mpa-powered-by="yiban.io"> <section class="_135editor" data-tools="135编辑器" data-id="91525"> <section> <section> <p>泛型,一个孤独的守门者。</p> <p><br></p> </section> </section> </section> </section> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">大家可能会有疑问,我为什么叫做泛型是一个守门者。这其实是我个人的看法而已,我的意思是说泛型没有其看起来那么深不可测,它并不神秘与神奇。泛型是 Java 中一个很小巧的概念,但同时也是一个很容易让人迷惑的知识点,它让人迷惑的地方在于它的许多表现有点违反直觉。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">文章开始的地方,先给大家奉上一道经典的测试题。</p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">List</span>&lt;String&gt; l1 = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> ArrayList&lt;String&gt;();<br><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">List</span>&lt;Integer&gt; l2 = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> ArrayList&lt;Integer&gt;();<br><br>System.out.println(l1.getClass() == l2.getClass());</code></pre> </section> <p><br></p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">请问,上面代码最终结果输出的是什么?不了解泛型的和很熟悉泛型的同学应该能够答出来,而对泛型有所了解,但是了解不深入的同学可能会答错。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">正确答案是 true。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">上面的代码中涉及到了泛型,而输出的结果缘由是<span style="outline: 0px;font-weight: 700;word-break: break-all;">类型擦除</span>。先好好说说泛型。</p> <h1 style="outline: 0px;margin-top: 8px;margin-bottom: 16px;font-size: 28px;color: rgb(79, 79, 79);font-weight: 700;line-height: 36px;word-break: break-all;">泛型是什么?</h1> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">泛型的英文是 generics,generic 的意思是通用,而翻译成中文,泛应该意为广泛,型是类型。所以泛型就是能广泛适用的类型。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;"><span style="outline: 0px;font-weight: 700;word-break: break-all;">但泛型还有一种较为准确的说法就是为了参数化类型,或者说可以将类型当作参数传递给一个类或者是方法。</span></p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">那么,如何解释类型参数化呢?</p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">class</span> <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span> {<br> &nbsp; &nbsp;Object <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span>;<br><br> &nbsp; &nbsp;<span class="hljs-default-function" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 175px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> Object <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 56px;text-decoration: none;font-weight: 400;font-style: normal;">getValue</span>(<span class="hljs-default-params" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 0px;text-decoration: none;font-weight: 400;font-style: normal;"></span>) </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">return</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span>;<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;<span class="hljs-default-function" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 245px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">void</span> <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 56px;text-decoration: none;font-weight: 400;font-style: normal;">setValue</span>(<span class="hljs-default-params" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 84px;text-decoration: none;font-weight: 400;font-style: normal;">Object <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span></span>) </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">this</span>.<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span> = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">value</span>;<br> &nbsp; &nbsp;}<br><br>}</code></pre> </section> <p><br></p> <p><span style="color: rgb(79, 79, 79);">假设 Cache 能够存取任何类型的值,于是,我们可以这样使用它。</span></p> <p><span style="color: rgb(79, 79, 79);"></span></p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">cache</span> = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span>();<br>cache.setValue(<span class="hljs-default-number" style="color: rgb(209, 154, 102);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">134</span>);<br>int value = (int) cache.getValue();<br>cache.setValue(<span class="hljs-default-string" style="color: rgb(152, 195, 121);background: rgba(0, 0, 0, 0);display: inline;width: 49px;text-decoration: none;font-weight: 400;font-style: normal;">"hello"</span>);<br>String value1 = (String) cache.getValue();</code></pre> </section> <p><span style="color: rgb(79, 79, 79);"></span><br></p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">使用的方法也很简单,只要我们做正确的强制转换就好了。</p> <p style="outline: 0px;margin-bottom: 16px;font-size: 16px;color: rgb(79, 79, 79);line-height: 26px;text-align: justify;word-break: break-all;">但是,泛型却给我们带来了不一样的编程体验。</p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> <span class="hljs-default-class" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 112px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">class</span> <span class="hljs-default-title" style="color: rgb(230, 192, 123);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span>&lt;T&gt; {</span><br> &nbsp; &nbsp;T value;<br><br> &nbsp; &nbsp;<span class="hljs-default-function" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 175px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> Object <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 56px;text-decoration: none;font-weight: 400;font-style: normal;">getValue</span><span class="hljs-default-params" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 14px;text-decoration: none;font-weight: 400;font-style: normal;">()</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">return</span> value;<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;<span class="hljs-default-function" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 210px;text-decoration: none;font-weight: 400;font-style: normal;"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">public</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">void</span> <span class="hljs-default-title" style="color: rgb(97, 174, 238);background: rgba(0, 0, 0, 0);display: inline;width: 56px;text-decoration: none;font-weight: 400;font-style: normal;">setValue</span><span class="hljs-default-params" style="color: rgb(171, 178, 191);background: rgba(0, 0, 0, 0);display: inline;width: 63px;text-decoration: none;font-weight: 400;font-style: normal;">(T value)</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 28px;text-decoration: none;font-weight: 400;font-style: normal;">this</span>.value = value;<br> &nbsp; &nbsp;}<br><br>}</code></pre> </section> <p><br></p> <p><span style="color: rgb(79, 79, 79);">这就是泛型,它将 value 这个属性的类型也参数化了,这就是所谓的参数化类型。再看它的使用方法。</span></p> <p><span style="color: rgb(79, 79, 79);"></span></p> <section data-mpa-preserve-tpl-color="t" data-mpa-template="t" class="mpa-template" mpa-preserve="t"> <pre style="margin:0;padding:0;border-radius:none;background:none;"><code style="border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;background: rgb(40, 44, 52);color: rgb(171, 178, 191);display: block;padding: 6px;overflow-x: auto;white-space: nowrap;" class="hljs-default"><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span>&lt;<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">String</span>&gt; cache1 = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span>&lt;<span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 42px;text-decoration: none;font-weight: 400;font-style: normal;">String</span>&gt;();<br>cache1.setValue("123");<br>String value2 = cache1.getValue();<br><br><span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span>&lt;<span class="hljs-default-built_in" style="color: rgb(230, 192, 123);background: rgba(0, 0, 0, 0);display: inline;width: 49px;text-decoration: none;font-weight: 400;font-style: normal;">Integer</span>&gt; cache2 = <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 21px;text-decoration: none;font-weight: 400;font-style: normal;">new</span> <span class="hljs-default-keyword" style="color: rgb(198, 120, 221);background: rgba(0, 0, 0, 0);display: inline;width: 35px;text-decoration: none;font-weight: 400;font-style: normal;">Cache</span>&lt;<span class="hljs-default-built_in" style="color: rgb(230, 192, 123);background: rgba(0, 0, 0, 0);display: inline;width: 49px;text-decoration: none;font-weight: 400;font-style: normal;">Integer</span>&gt;();<br>cache2.setValue(456);<br>int value3 = cache2.getValue();</code></pre> </section> <p><span style="color: rgb(79, 79, 79);"></span><br></p> <p><span style="color: rgb(79, 79, 79);">最显而易见的好处就是它不再需要对取出来的结果进行强制转换了。但,还有另外一点不同。&nbsp;</span></p> <p><span style="color: rgb(79, 79, 79);"><br></span></p> <p><img class="" data-copyright="0" data-ratio="0.2890932982917214" data-s="300,640" data-type="png" data-w="761" src="/upload/f928dd3d710e77c19

一遍记住 8 种排序算法与 Java 代码实现

作者:微信小助手

<p style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;text-align: center;"><span class="" style="font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;max-width: 100%;white-space: pre-wrap;font-size: 14px;color: rgb(255, 41, 65);line-height: 22.4px;box-sizing: border-box !important;overflow-wrap: break-word !important;">(给</span><span style="font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;max-width: 100%;white-space: pre-wrap;font-size: 14px;line-height: 22.4px;color: rgb(0, 128, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">算法爱好者</span><span class="" style="font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;max-width: 100%;white-space: pre-wrap;font-size: 14px;color: rgb(255, 41, 65);line-height: 22.4px;box-sizing: border-box !important;overflow-wrap: break-word !important;">加星标,修炼编程内功</span><span class="" style="font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;max-width: 100%;white-space: pre-wrap;color: rgb(255, 41, 65);font-size: 14px;line-height: 22.4px;box-sizing: border-box !important;overflow-wrap: break-word !important;">)</span></p> <p style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;"><br></p> <blockquote style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;"> <p style="max-width: 100%;min-height: 1em;color: rgba(0, 0, 0, 0.5);font-size: 15px;white-space: normal;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">作者:KaelQ,</span></p> <p style="max-width: 100%;min-height: 1em;color: rgba(0, 0, 0, 0.5);font-size: 15px;white-space: normal;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">www.jianshu.com/p/5e171281a387</span></p> </blockquote> <p style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;"><span style="font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;font-size: 15px;"></span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">1.直接插入排序</strong></span></p> <blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="31" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">经常碰到这样一类排序问题:把新的数据插入到已经排好的数据列中。</p> </section> </blockquote> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将第一个数和第二个数排序,然后构成一个有序序列</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将第三个数插入进去,构成一个新的有序序列。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">对第四个数、第五个数……直到最后一个数,重复第二步。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.033282904689864" data-s="300,640" data-type="jpeg" data-w="661" src="/upload/682a4375bf02696564c83e8ab49373a3.jpg" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 661px !important;visibility: visible !important;"></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写写成代码:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">首先设定插入次数,即循环次数,for(int i=1;i&lt;length;i++),1个数的那次不用插入。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">设定插入数和得到已经排好序列的最后一个数的位数。insertNum和j=i-1。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">从最后一个数开始向前循环,如果插入数小于当前数,就将当前数向后移动一位。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将当前数放置到空着的位置,即j+1。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">insertSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] a)</span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> length=a.length;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//数组长度,将这个提取出来是为了提高速度。</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> insertNum;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//要插入的数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>;i&lt;length;i++){<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//插入的次数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> insertNum=a[i];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//要插入的数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> j=i<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//已经排序好的序列元素个数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">while</span>(j&gt;=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>&amp;&amp;a[j]&gt;insertNum){<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//序列从后到前循环,将大于insertNum的数向后移动一格</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>]=a[j];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//元素移动一格</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> j--;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>]=insertNum;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//将需要插入的数放在要插入的位置。</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">2.希尔排序</span></strong></span></p> <blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="18" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">对于直接插入排序问题,数据量巨大时。</p> </section> </blockquote> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将数的个数设为n,取奇数k=n/2,将下标差值为k的书分为一组,构成有序序列。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">再取k=k/2 ,将下标差值为k的书分为一组,构成有序序列。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第二步,直到k=1执行简单插入排序。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.332618025751073" data-s="300,640" data-type="jpeg" data-w="466" src="/upload/22c91e3f1857928356b0b6f700f48b3d.jpg" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 466px !important;visibility: visible !important;"></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写成代码:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">首先确定分的组数。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">然后对组中元素进行插入排序。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">然后将length/2,重复1,2步,直到length=0为止。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">sheelSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] a)</span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> d = a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">while</span> (d!=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> d=d/<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">2</span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> x = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>; x &lt; d; x++) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//分的组数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i = x + d; i &lt; a.length; i += d) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//组中的元素,从第二个数开始</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> j = i - d;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//j为有序序列最后一位的位数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> temp = a[i];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//要插入的元素</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (; j &gt;= <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span> &amp;&amp; temp &lt; a[j]; j -= d) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//从后往前遍历。</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j + d] = a[j];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//向后移动d位</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j + d] = temp;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">3.简单选择排序</strong></span></p> <blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="17" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">常用于取序列中最大最小的几个数时。</p> </section> </blockquote> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">(如果每次比较都交换,那么就是交换排序;如果每次比较完一个循环再交换,就是简单选择排序。)</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">遍历整个序列,将最小的数放在最前面。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">遍历剩下的序列,将最小的数放在最前面。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第二步,直到只剩下一个数。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.0056497175141244" data-s="300,640" data-type="jpeg" data-w="531" src="/upload/f2dc18a8e562698d53eba1ac06b6bb37.jpg" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 531px !important;visibility: visible !important;"></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写成代码:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">首先确定循环次数,并且记住当前数字和当前位置。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将当前位置后面所有的数与当前数字进行对比,小数赋值给key,并记住小数的位置。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">比对完成后,将最小的值与第一个数的值交换。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复2、3步。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">selectSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] a)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> length = a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>; i &lt; length; i++) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//循环次数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> key = a[i];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> position=i;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> j = i + <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>; j &lt; length; j++) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//选出最小的值和位置</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> (a[j] &lt; key) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> key = a[j];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> position = j;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[position]=a[i];<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//交换位置</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[i]=key;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">4.堆排序</span></strong></span></p> <blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="11" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"> <section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">对简单选择排序的优化。</p> </section> </blockquote> <ol class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将序列构建成大顶堆。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将根节点与最后一个节点交换,然后断开最后一个节点。</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第一、二步,直到所有节点断开。</span></p></li> </ol> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="0.7598752598752598" data-s="300,640" data-type="jpeg" data-w="962" src="/upload/835d5978cf7becbdfcc32ca5f2e5c27.jpg" style="box-sizing: border-box !important;overflow-wrap: break-word !important;width: 677px !important;visibility: visible !important;"></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">heapSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] a)</span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> System.out.println(<span class="" style="max-width: 100%;color: rgb(152, 195, 121);box-sizing: border-box !important;overflow-wrap: break-word !important;">"开始排序"</span>);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> arrayLength=a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//循环建堆</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>;i&lt;arrayLength<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>;i++){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//建堆</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> buildMaxHeap(a,arrayLength<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>-i);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//交换堆顶和最后一个元素</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> swap(a,<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>,arrayLength<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>-i);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> System.out.println(Arrays.toString(a));<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">private</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">swap</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] data, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> j)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">// TODO Auto-generated method stub</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> tmp=data[i];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> data[i]=data[j];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> data[j]=tmp;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//对data数组从0到lastIndex建大顶堆</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">private</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">buildMaxHeap</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] data, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> lastIndex)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">// TODO Auto-generated method stub</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//从lastIndex处节点(最后一个节点)的父节点开始</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i=(lastIndex<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-1</span>)/<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">2</span>;i&gt;=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">0</span>;i--){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//k保存正在判断的节点</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> k=i;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//如果当前k节点的子节点存在</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">while</span>(k*<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">2</span>+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>&lt;=lastIndex){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//k节点的左子节点的索引</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> biggerIndex=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">2</span>*k+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//如果biggerIndex小于lastIndex,即biggerIndex+1代表的k节点的右子节点存在</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span>(biggerIndex&lt;lastIndex){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//若果右子节点的值较大</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span>(data[biggerIndex]&lt;data[biggerIndex+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">1</span>]){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//biggerIndex总是记录较大子节点的索引</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> biggerIndex++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//如果k节点的值小于其较大的子节点的值</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span>(data[k]&lt;data[biggerIndex]){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//交换他们</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> swap(data,k,biggerIndex);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右子节点的值</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> k=biggerIndex;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">else</span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">break</span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p> <p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow

猝死前最后4分钟,他本还有一次机会活..

作者:微信小助手

<section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn" data-mpa-powered-by="yiban.io"></section> <h3 class="" style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;text-align: center;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="__bg_gif " data-copyright="0" data-ratio="0.4573333333333333" data-type="gif" data-w="750" src="/upload/d3c1e8315082599dc463204daf9a9695.null" style="font-size: 17px;letter-spacing: 0.544px;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;width: 677px !important;"><br style="max-width: 100%;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p><p neue="neue" pingfang="pingfang" sc="sc" hiragino="hiragino" gb="gb" ui="ui" microsoft="microsoft" arial="arial" sans-serif="sans-serif" px="px" normal="normal" em="em" important="important" rgb="rgb" helvetica="helvetica" sans="sans" yahei="yahei" break-word="break-word" border-box="border-box" style="max-width: 100%;min-height: 1em;letter-spacing: 0.54px;font-size: 17px;text-align: center;line-height: 1.75em;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p><p neue="neue" pingfang="pingfang" sc="sc" hiragino="hiragino" gb="gb" ui="ui" microsoft="microsoft" arial="arial" sans-serif="sans-serif" px="px" normal="normal" em="em" important="important" rgb="rgb" helvetica="helvetica" sans="sans" yahei="yahei" break-word="break-word" border-box="border-box" style="max-width: 100%;min-height: 1em;letter-spacing: 0.54px;font-size: 17px;text-align: center;line-height: 1.75em;user-select: text !important;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;user-select: text !i

一文学会Java死锁和CPU 100% 问题的排查技巧

作者:微信小助手

<section class="xmteditor" style="display:none;" data-tools="新媒体管家" data-label="powered by xmt.cn"></section> <p style="text-align: center;"><strong style="max-width: 100%;letter-spacing: 0.544px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 16px;white-space: pre-line;background-color: rgb(255, 255, 255);text-align: right;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;color: rgb(136, 136, 136);box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></strong></p> <p style="white-space: normal;text-align: center;"><span style="font-size: 14px;color: rgb(136, 136, 136);"></span></p> <p style="white-space: normal;text-align: center;"><span style="color: rgb(136, 136, 136);font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;font-size: 14px;letter-spacing: 0.544px;">点击蓝色“</span><span style="font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;font-size: 14px;letter-spacing: 0.544px;color: rgb(0, 128, 255);">程序猿DD</span><span style="color: rgb(136, 136, 136);font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;font-size: 14px;letter-spacing: 0.544px;">”关注我</span></p> <p style="white-space: normal;text-align: center;"><span style="color: rgb(136, 136, 136);font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;font-size: 14px;letter-spacing: 0.544px;">回复“</span><span style="font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;font-size: 14px;letter-spacing: 0.544px;color: rgb(0, 128, 255);">资源</span><span style="color: rgb(136, 136, 136);font-family: Georgia, &quot;Times New Roman&quot;, Times, &quot;Songti SC&quot;, serif;font-size: 14px;letter-spacing: 0.544px;">”获取独家整理的学习资料!</span></p> <h3 style="margin: 40px 10px 20px;font-weight: bold;font-size: 19.2px;max-width: 100%;box-sizing: border-box;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;text-align: left;color: rgb(63, 63, 63);line-height: 1.5;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">00 本文简介</h3> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">作为一名搞技术的程序猿或者是攻城狮,想必你应该是对下面这两个问题有所了解,说不定你在实际的工作或者面试就有遇到过:</p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;overflow-wrap: break-word !important;">第一个问题:Java死锁如何排查和解决?</strong></p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;overflow-wrap: break-word !important;">第二个问题:服务器CPU占用率高达到100%排查和解决?</strong></p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;overflow-wrap: break-word !important;">第三个问题:有哪些工具能够快速查看线程使用情况?</strong></p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">本文对这三个问题进行总结整理,通过实例演示讲解,精彩干货,不容错过啊!</p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">前戏就这么多,高潮会很多,做好了,让我们直奔主题,发动小船,Let's go!</p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><img class="__bg_gif" data-ratio="0.5643939393939394" src="/upload/b12547dc580a1dd63c08d540d3027b05.gif" data-type="gif" data-w="528" style="margin: 20px auto;box-sizing: border-box;line-height: 1.5;border-radius: 4px;display: block;overflow-wrap: break-word !important;width: 315px !important;visibility: visible !important;" title="null"></p> <h3 style="margin: 40px 10px 20px;font-weight: bold;font-size: 19.2px;max-width: 100%;box-sizing: border-box;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;text-align: left;color: rgb(63, 63, 63);line-height: 1.5;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">01 Java死锁排查和解决</h3> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">要排查和解决死锁,首先思考三个问题:</p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;overflow-wrap: break-word !important;">1. 什么是死锁?</strong></p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;overflow-wrap: break-word !important;">2. 为什么会出现死锁?</strong></p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;overflow-wrap: break-word !important;">3. 怎么排查代码中出现了死锁?</strong></p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;overflow-wrap: break-word !important;">4. 如何避免写出死锁的代码?</strong></p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">作为技术人员(工程师),在出现问题的时候,能够尽快的去解决这个问题。但是在学习技术知识的时候,还是脚踏实地,多问一些为什么,一个好的问题,能够让自己思考,这方面的能力也一定要锻炼锻炼哦,这样才能更好的理解和掌握知识,并探究/触碰到更深入的地方。</p> <h3 style="margin: 40px 10px 20px;font-weight: bold;font-size: 19.2px;max-width: 100%;box-sizing: border-box;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;text-align: left;color: rgb(63, 63, 63);line-height: 1.5;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">1、啥是死锁?</h3> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">死锁是指两个或两个以上的进程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程。[百度百科:死锁]</p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><img class="" data-ratio="0.8891129032258065" src="/upload/a55f916d05cad187e55e178aefa695c8.png" data-type="png" data-w="496" style="margin: 20px auto;box-sizing: border-box;line-height: 1.5;border-radius: 4px;display: block;overflow-wrap: break-word !important;width: 315px !important;visibility: visible !important;" title="null"></p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;overflow-wrap: break-word !important;">注:进程和线程都可以发生死锁,只要满足死锁的条件!</strong></p> <h3 style="margin: 40px 10px 20px;font-weight: bold;font-size: 19.2px;max-width: 100%;box-sizing: border-box;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;text-align: left;color: rgb(63, 63, 63);line-height: 1.5;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">2、为啥子会出现死锁?</h3> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">从上面的概念中我们知道</p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">(1)必须是两个或者两个以上进程(线程)</p> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">(2)必须有竞争资源</p> <h3 style="margin: 40px 10px 20px;font-weight: bold;font-size: 19.2px;max-width: 100%;box-sizing: border-box;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;text-align: left;color: rgb(63, 63, 63);line-height: 1.5;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">3、怎么排查代码中出现了死锁?【重点来了】</h3> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">首先整一个死锁的代码,看例子:</p> <p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages" data-backh="945" data-backw="654" data-before-oversubscription-url="https://mmbiz.qlogo.cn/mmbiz_png/yziabN7pK2YgTnbTywosjClia4ba6T4DEx5VCViaEqZ5ITMHu9MphgF6Zmd34RJUQFUGAqSCFicOr4PicWLEv6Obmow/?wx_fmt=png" data-ratio="1.44609375" data-s="300,640" src="/upload/a336e1d8e8d4520d4c334188fa7d0914.png" data-type="png" data-w="1280" style="width: 677px;box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;"><span style="max-width: 100%;color: rgb(63, 63, 63);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;font-size: 16px;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;">上面这段代码执行后,就会出现死锁,排查的姿势有如下几种,搞起来吧!</span></p> <h3 style="margin: 40px 10px 20px;font-weight: bold;font-size: 19.2px;max-width: 100%;box-sizing: border-box;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;text-align: left;color: rgb(63, 63, 63);line-height: 1.5;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;">第一个姿势:使用 jps + jstack</h3> <p style="margin: 10px;max-width: 100%;box-sizing: border-box;min-height: 1em;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;font-size: 16px;text-align: left;color: rgb(63, 63, 63);line-height: 1.6;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;overflow-wrap: break-word !important;">一</strong>:在windons命令窗口,使用<code style="padding: 3px 5px;max-width: 100%;box-sizing: border-box;color: rgb(255, 53, 2);line-height: 1.5;font-family: &quot;Operator Mono