千寻

道路很长, 开始了就别停下!

0%

前言

集合是Java中非常重要而且基础的内容,因为任何数据必不可少的就是该数据是如何存储的,集合的作用就是以一定的方式组织、存储数据。这里写的集合,一部分是比较常见的、一部分是不常用但是我个人平时见到过的,一些比较相似的集合(比如HashMap和Hashtable)就只讲一个,突出它们之间的区别即可。

阅读全文 »

Java中线程的状态分为6种。

    1. 初始(NEW):新创建了一个线程对象,但还没有调用start()方法。
    1. 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running)两种状态笼统的称为“运行”。
      线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中,获取CPU的使用权,此时处于就绪状态(ready)。就绪状态的线程在获得CPU时间片后变为运行中状态(running)。
  • 3.阻塞(BLOCKED):表示线程阻塞于锁。
  • 4.等待(WAITING):进入该状态的线程需要等待其他线程做出一些特定动作(通知或中断)。
  • 5.超时等待(TIMED_WAITING):该状态不同于WAITING,它可以在指定的时间后自行返回。
    1. 终止(TERMINATED):表示该线程已经执行完毕。

线程的状态图

1. 初始状态

实现Runnable接口和继承Thread可以得到一个线程类,new一个实例出来,线程就进入了初始状态。

2.1. 就绪状态

  1. 就绪状态只是说你资格运行,调度程序没有挑选到你,你就永远是就绪状态。
  2. 调用线程的start()方法,此线程进入就绪状态。
  3. 当前线程sleep()方法结束,其他线程join()结束,等待用户输入完毕,某个线程拿到对象锁,这些线程也将进入就绪状态。
  4. 当前线程时间片用完了,调用当前线程的yield()方法,当前线程进入就绪状态。
  5. 锁池里的线程拿到对象锁后,进入就绪状态。

2.2. 运行中状态

线程调度程序从可运行池中选择一个线程作为当前线程时线程所处的状态。这也是线程进入运行状态的唯一一种方式。

3. 阻塞状态

阻塞状态是线程阻塞在进入synchronized关键字修饰的方法或代码块(获取锁)时的状态。

4. 等待

处于这种状态的线程不会被分配CPU执行时间,它们要等待被显式地唤醒,否则会处于无限期等待的状态。

5. 超时等待

处于这种状态的线程不会被分配CPU执行时间,不过无须无限期等待被其他线程显示地唤醒,在达到一定时间后它们会自动唤醒。

6. 终止状态

  1. 当线程的run()方法完成时,或者主线程的main()方法完成时,我们就认为它终止了。这个线程对象也许是活的,但是,它已经不是一个单独执行的线程。线程一旦终止了,就不能复生。
  2. 在一个终止的线程上调用start()方法,会抛出java.lang.IllegalThreadStateException异常。

等待队列

  • 调用obj的wait(), notify()方法前,必须获得obj锁,也就是必须写在synchronized(obj) 代码段内。
  • 与等待队列相关的步骤和图

 

  • 1.线程1获取对象A的锁,正在使用对象A。
  • 2.线程1调用对象A的wait()方法。
  • 3.线程1释放对象A的锁,并马上进入等待队列。
  • 4.锁池里面的对象争抢对象A的锁。
  • 5.线程5获得对象A的锁,进入synchronized块,使用对象A。
  • 6.线程5调用对象A的notifyAll()方法,唤醒所有线程,所有线程进入同步队列。若线程5调用对象A的notify()方法,则唤醒一个线程,不知道会唤醒谁,被唤醒的那个线程进入同步队列。
  • 7.notifyAll()方法所在synchronized结束,线程5释放对象A的锁。
  • 8.同步队列的线程争抢对象锁,但线程1什么时候能抢到就不知道了。

同步队列状态

  • 当前线程想调用对象A的同步方法时,发现对象A的锁被别的线程占有,此时当前线程进入同步队列。简言之,同步队列里面放的都是想争夺对象锁的线程。
  • 当一个线程1被另外一个线程2唤醒时,1线程进入同步队列,去争夺对象锁。
  • 同步队列是在同步的环境下才有的概念,一个对象对应一个同步队列。

几个方法的比较

  1. Thread.sleep(long millis),一定是当前线程调用此方法,当前线程进入TIMED_WAITING状态,但不释放对象锁,millis后线程自动苏醒进入就绪状态。作用:给其它线程执行机会的最佳方式。
  2. Thread.yield(),一定是当前线程调用此方法,当前线程放弃获取的CPU时间片,但不释放锁资源,由运行状态变为就绪状态,让OS再次选择线程。作用:让相同优先级的线程轮流执行,但并不保证一定会轮流执行。实际中无法保证yield()达到让步目的,因为让步的线程还有可能被线程调度程序再次选中。Thread.yield()不会导致阻塞。该方法与sleep()类似,只是不能由用户指定暂停多长时间。
  3. t.join()/t.join(long millis),当前线程里调用其它线程t的join方法,当前线程进入WAITING/TIMED_WAITING状态,当前线程不会释放已经持有的对象锁。线程t执行完毕或者millis时间到,当前线程进入就绪状态。
  4. obj.wait(),当前线程调用对象的wait()方法,当前线程释放对象锁,进入等待队列。依靠notify()/notifyAll()唤醒或者wait(long timeout) timeout时间到自动唤醒。
  5. obj.notify()唤醒在此对象监视器上等待的单个线程,选择是任意性的。notifyAll()唤醒在此对象监视器上等待的所有线程。

前言

以前写过介绍HashMap的文章,文中提到过HashMap在put的时候,插入的元素超过了容量(由负载因子决定)的范围就会触发扩容操作,就是rehash,这个会重新将原数组的内容重新hash到新的扩容数组中,在多线程的环境下,存在同时其他的元素也在进行put操作,如果hash值相同,可能出现同时在同一数组下用链表表示,造成闭环,导致在get时会出现死循环,所以HashMap是线程不安全的。

阅读全文 »

并发 (Concurrency):一个处理器“同时”处理多个任务
并行 (Parallelism):多个处理器 “同时”处理多个任务

阅读全文 »

1.Runtime类

  • jvm虚拟机注册一个勾子,当虚拟机要关闭时,会执行预先注册的线程任务。
阅读全文 »

Mysql中bigint、int、mediumint、smallint 和 tinyint的取值范围


引言

社区这边的业务就遇到过这个坑,由于是用的开源框架,很多表id的字段用的mediumint类型,随着业务增长,数据量暴增,结果有一天超过id的上限,结果insert db就报错了,影响部分业务功能。

阅读全文 »

表字段修复

1
2
3
4
 
SELECT TABLE_SCHEMA '数据库',TABLE_NAME '表',COLUMN_NAME '字段',CHARACTER_SET_NAME '原字符集',COLLATION_NAME '原排序规则',CONCAT('ALTER TABLE ', TABLE_SCHEMA,'.',TABLE_NAME, ' MODIFY COLUMN ',COLUMN_NAME,' ',COLUMN_TYPE,' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') '修正SQL'
FROM information_schema.`COLUMNS`
WHERE COLLATION_NAME RLIKE 'latin1';

表修复

1
2
3
4
 
SELECT TABLE_SCHEMA '数据库',TABLE_NAME '表',TABLE_COLLATION '原排序规则',CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.', TABLE_NAME, ' COLLATE=utf8mb4_general_ci;') '修正SQL'
FROM information_schema.`TABLES`
WHERE TABLE_COLLATION RLIKE 'latin1';

表修复

1
2
3
SELECT SCHEMA_NAME '数据库',DEFAULT_CHARACTER_SET_NAME '原字符集',DEFAULT_COLLATION_NAME '原排序规则',CONCAT('ALTER DATABASE ',SCHEMA_NAME,' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') '修正SQL'
FROM information_schema.`SCHEMATA`
WHERE DEFAULT_COLLATION_NAME RLIKE 'unicode_ci';

ALTER TABLE attendance1.attendance_apply MODIFY COLUMN time_span varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE attendance1.attendance_apply MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE attendance1.attendance_approve MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE attendance1.attendance_approve MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE attendance1.attendance_copy_user MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE attendance1.attendance_copy_user MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE attendance1.attendance_file MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE attendance1.attendance_file MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE attendance1.attendance_working MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE attendance1.attendance_working MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE emergency.company_manage MODIFY COLUMN update_user varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE emergency.file_Info MODIFY COLUMN file_name varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE emergency.file_Info MODIFY COLUMN model_name varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE emergency.polling MODIFY COLUMN org_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE emergency.polling MODIFY COLUMN charge_phone varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE emergency.polling MODIFY COLUMN picture varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE emergency.polling MODIFY COLUMN abarbeitung varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE emergency.team MODIFY COLUMN adderss varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE emergency.team_type MODIFY COLUMN value varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE iot_device1.video_client MODIFY COLUMN username varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE iot_device1.video_client MODIFY COLUMN password varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE iot_device1.video_client MODIFY COLUMN company_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE iot_device1.video_client MODIFY COLUMN creator varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE iot_device1.video_client MODIFY COLUMN update_user varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN apply_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN company_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN current_approve_node varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN reason varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN remark varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN time_span varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN departure varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN destination varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply_file MODIFY COLUMN company_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply_file MODIFY COLUMN file_save_name varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply_file MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_apply_file MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_approve MODIFY COLUMN approve_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_approve MODIFY COLUMN company_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_approve MODIFY COLUMN approve_full_opinion varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_approve MODIFY COLUMN approve_remark varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_approve MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_approve MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_copy_user MODIFY COLUMN copy_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_copy_user MODIFY COLUMN company_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_copy_user MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_copy_user MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_settings MODIFY COLUMN company_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_settings MODIFY COLUMN name varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_settings MODIFY COLUMN address varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_settings MODIFY COLUMN longitude varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_settings MODIFY COLUMN latitude varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_user MODIFY COLUMN company_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_user MODIFY COLUMN user_id varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_working MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.attendance_working MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.dangerous MODIFY COLUMN company_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.dangerous MODIFY COLUMN manage_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.dictionary MODIFY COLUMN code varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.dictionary MODIFY COLUMN field varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.dictionary MODIFY COLUMN field_value varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.dictionary MODIFY COLUMN type varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.dictionary MODIFY COLUMN remark varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.dictionary MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.dictionary MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.focus_user MODIFY COLUMN file_src varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.storage_tank MODIFY COLUMN creator varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.storage_tank MODIFY COLUMN update_user varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.storage_tank MODIFY COLUMN design_stock varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.template_attendance_record MODIFY COLUMN user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.template_attendance_record MODIFY COLUMN user_type varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.template_attendance_record MODIFY COLUMN clock_in_type varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.template_attendance_record MODIFY COLUMN created_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE share.template_attendance_record MODIFY COLUMN modified_user_id varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

ALTER TABLE emergency.company_category COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.company_label COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.company_manage COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.file_Info COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.garden COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.garden_company COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.label COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.polling COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.sys_dict COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.team COLLATE=utf8mb4_general_ci;
ALTER TABLE emergency.team_type COLLATE=utf8mb4_general_ci;
ALTER TABLE iot_device1.video_client COLLATE=utf8mb4_general_ci;
ALTER TABLE share.apply_user_company COLLATE=utf8mb4_general_ci;
ALTER TABLE share.attendance_apply COLLATE=utf8mb4_general_ci;
ALTER TABLE share.attendance_apply_file COLLATE=utf8mb4_general_ci;
ALTER TABLE share.attendance_approve COLLATE=utf8mb4_general_ci;
ALTER TABLE share.attendance_copy_user COLLATE=utf8mb4_general_ci;
ALTER TABLE share.attendance_settings COLLATE=utf8mb4_general_ci;
ALTER TABLE share.attendance_user COLLATE=utf8mb4_general_ci;
ALTER TABLE share.attendance_working COLLATE=utf8mb4_general_ci;
ALTER TABLE share.company_expand COLLATE=utf8mb4_general_ci;
ALTER TABLE share.dangerous COLLATE=utf8mb4_general_ci;
ALTER TABLE share.dictionary COLLATE=utf8mb4_general_ci;
ALTER TABLE share.focus_user COLLATE=utf8mb4_general_ci;
ALTER TABLE share.notice_company COLLATE=utf8mb4_general_ci;
ALTER TABLE share.notice_user COLLATE=utf8mb4_general_ci;
ALTER TABLE share.storage_tank COLLATE=utf8mb4_general_ci;
ALTER TABLE share.sys_dict COLLATE=utf8mb4_general_ci;
ALTER TABLE share.template_attendance_record COLLATE=utf8mb4_general_ci;
ALTER TABLE share.user_issue COLLATE=utf8mb4_general_ci;
ALTER TABLE share.warehouse COLLATE=utf8mb4_general_ci;

ALTER DATABASE attendance1 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER DATABASE attendance10 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER DATABASE attendance2 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER DATABASE attendance3 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER DATABASE attendance4 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER DATABASE attendance5 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER DATABASE attendance6 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER DATABASE attendance7 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER DATABASE attendance8 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER DATABASE attendance9 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

图中红色部分为受检查异常。它们必须被捕获,或者在函数中声明为抛出该异常。

阅读全文 »