This commit is contained in:
whaifree 2024-08-27 01:02:39 +08:00
commit 030780afa8

View File

@ -1,9 +1,12 @@
package cn.whaifree.test; package cn.whaifree.test;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
public class ThreadDemo1 { public class ThreadDemo1 {
@ -189,6 +192,57 @@ class p2{
} }
class mockException{
public static void main(String[] args) {
ThreadLocal<Object> objectThreadLocal = new InheritableThreadLocal<>();
objectThreadLocal.set("test");
objectThreadLocal.get();
ReentrantLock lock = new ReentrantLock();
lock.tryLock();
lock.lock();
/**
* lock 方法
* 阻塞等待调用 lock() 方法时如果锁已经被其他线程持有当前线程将阻塞等待直到获得锁
* 不可中断默认情况下lock() 方法不会响应中断信号这意味着即使当前线程被中断它仍然会继续等待锁
* 保证获取锁只要线程最终没有被中断或终止它总会获取到锁
* tryLock 方法
* 非阻塞调用 tryLock() 方法时如果锁可用则立即获取锁并返回 true如果锁已被其他线程持有则立即返回 false 而不会阻塞等待
* 可选超时tryLock 还有一个带超时参数的版本 tryLock(long time, TimeUnit unit)允许线程在指定时间内等待锁如果在超时时间内锁仍未被获取则返回 false
* 可中断tryLock(long time, TimeUnit unit) 方法可以响应中断信号如果线程在等待过程中被中断则抛出 InterruptedException
*/
CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
try {
int num = 1 / 0;
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "";
}
});
String s = "-";
try {
s = stringCompletableFuture.get(1, TimeUnit.SECONDS);
} catch (TimeoutException e) {
// System.out.println("超时了");
s = "超时了";
} catch (Exception e) {
System.out.println(e.getCause().getMessage());
throw new RuntimeException(e);
}
System.out.println(s);
}
}
class MyException extends RuntimeException { class MyException extends RuntimeException {