1 swap 数据交换
public static void swap(int a, int b){
int t = a^b;
b = t ^ b;
a = t ^ a;
System.out.println(a);
System.out.println(b);
}
2. Cloneable 接口
@Data
@AllArgsConstructor
@NoArgsConstructor
static class A implements Cloneable{
int a;
int b;
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone(); // 调用Object.clone()
}
}
3. 老炮问题== 和equals 的区别, 那个hashcode 方法实际返回的不是真实物理地址,两个hashocode 返回的hashcode 值相同 并且equals 也相同但是他两个照样不相等
public static boolean binarySearch(int [] arr, int target){
int left = 0;
int right = arr.length;
while(left < right){
int middle = left + ((right - left) >> 1); // 课本中一般是除以2
if(arr[middle] == target){
return true;
}else if(arr[middle] > target){
right--;
}else {
left++;
}
}
return false;
}
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
A aa = new A(1,2);
Object clone = aa.clone();
System.out.println(aa.equals(clone));
System.out.println(aa.hashCode());
System.out.println(clone.hashCode());
System.out.println(System.identityHashCode(aa));;
System.out.println(System.identityHashCode(clone));
Integer num1 = 129;
Integer num11 = 129;
Integer num2 = new Integer(129);
Integer num3 = new Integer(129);
System.out.println(num1 == num11); // true
System.out.println(num1 == num2); // false
System.out.println(num2 == num3);// false
System.out.println(num1.equals(num11)); // true
System.out.println(num2.equals(num3));// true
System.out.println(num1.equals(num2));// true
System.out.println(System.identityHashCode(num1));
System.out.println(System.identityHashCode(num11));
System.out.println(System.identityHashCode(num2));
自动拆箱-127-128 == 和equals相同 Integer a=2 和 Integer b = 2
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
String s1 = "a";
String s2 = "a";
String s3 = new String("a");
String s4 = s3.intern();
System.out.println(s1 == s1); // true
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3));// true
System.out.println(s1 == s3); // false
System.out.println(s3.equals(s4)); // true
System.out.println(s3 == s4); // false
System.out.println(System.identityHashCode(s1)); //1940030785
System.out.println(System.identityHashCode(s2));// 1940030785
System.out.println(System.identityHashCode(s3));// 1869997857
System.out.println(System.identityHashCode(s4));//1940030785
jdk8 版本这个value 数据 是char ,后来版本改成byte 数组了为啥节省空间
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
}
常用String API 算法题必本,拆分字符串
String s5 = new String(s1.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);
System.out.println(s5);
StringBuffer sb = new StringBuffer(s5);
String s6 = sb.reverse().toString();
Pattern p = Pattern.compile(pattern2);
Matcher matcher = p.matcher("abcde@163.com,12345dd@,d1235");
while(matcher.find()){
String group = matcher.group();
System.out.println(group);
}
4.BigDecimal类
public static void main(String[] args) {
BigDecimal a = BigDecimal.valueOf(0.2);
BigDecimal b = BigDecimal.valueOf(0.20);
BigDecimal c = BigDecimal.valueOf(0.1);
BigDecimal d = b.add(c);
BigDecimal e = BigDecimal.valueOf(57).divide(BigDecimal.valueOf(100));
System.out.println(d); // 0.3
System.out.println(e); // 0.57
System.out.println(a.scale()); // 1
System.out.println(a.signum());// 1
System.out.println(b.scale()); //1
System.out.println(b.signum()); // 1
System.out.println(a.hashCode()); //63
System.out.println(b.hashCode()); // 63
System.out.println(c.hashCode()); // 32
System.out.println(d.hashCode()); // 94
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
System.out.println(a.compareTo(b)== 0 ? true: false); // true
}
5. jstack 如何查看线程栈
jstack [-l] <pid>
(to connect to running process)
jstack -F [-m] [-l] <pid>
(to connect to a hung process)
jstack [-m] [-l] <executable> <core>
(to connect to a core file)
jstack [-m] [-l] [server_id@]<remote server IP or hostname>
(to connect to a remote debug server)
Options:
-F to force a thread dump. Use when jstack <pid> does not respond (process is hung)
-m to print both java and native frames (mixed mode)
-l long listing. Prints additional information about locks
-h or -help to print this help message
(base) PS D:\code\springboot-starter-demo\rocketmq-start-demo>
jmap -heap <pid>
jmap -histo:live <pid> | head -n 20 # 仅显示前20行
jmap -dump:live,format=b,file=heap.hprof <pid>
mysql 死锁排查
1. 查看当前运行的事务
SELECT * FROM information_schema.INNODB_TRX;
这个查询会显示当前所有正在运行的事务,包括事务ID、事务状态、开始时间、锁等待状态等信息。
2. 查看锁等待情况
SELECT * FROM information_schema.INNODB_LOCK_WAITS;
这个查询显示当前正在等待锁的事务和被阻塞的事务之间的关系。
3. 查看锁信息
SELECT * FROM information_schema.INNODB_LOCKS;
这个查询显示当前持有的锁和请求的锁的详细信息。
4. 查看进程列表
SHOW PROCESSLIST;
这个命令显示当前所有连接的状态,可以帮助你识别长时间运行的事务或锁等待。
5. 性能模式(Performance Schema)中的锁信息
SELECT * FROM performance_schema.events_waits_current
WHERE EVENT_NAME LIKE '%lock%';
6. 查看事务隔离级别
SELECT @@transaction_isolation;
实用查询组合
以下是一个更全面的查询,可以查看事务和锁的详细信息:
SELECT
r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread,
b.trx_query blocking_query
FROM
information_schema.innodb_lock_waits w
INNER JOIN
information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id
INNER JOIN
information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id;