//package base;
//
//import com.yanzuoguang.util.exception.CodeException;
//import com.yanzuoguang.util.thread.ThreadHelper;
//import org.junit.Test;
//
//import java.util.Map;
//import java.util.WeakHashMap;
//
//public class WeakHashMapFinishTest {
//
//    public static class MyKeyInner {
//        @Override
//        protected void finalize() throws Throwable {
//            System.out.println("MyKeyInner 开始终结");
//        }
//    }
//
//    public static class MyKey implements AutoCloseable {
//        private MyKeyInner myKeyInner = new MyKeyInner();
//
//        public MyKey() {
//            // throw new CodeException("构造异常");
//        }
//
//        @Override
//        protected void finalize() throws Throwable {
//            System.out.println("开始终结");
//            throw new CodeException("MyKey 终止异常");
//            // 数据库连接在关闭;
//        }
//
//        @Override
//        public void close() throws Exception {
//            System.out.println("MyKey close");
//
//        }
//    }
//
//    @Test
//    public void test() throws InterruptedException {
//        Map<MyKey, Integer> map = new WeakHashMap<>();
//        test1(map);
//        System.out.println(map.size());
//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    System.gc();
//                    System.out.println(map.size());
//                    ThreadHelper.sleep(100);
//                }
//            }
//        });
//        thread.start();
//        thread.join();
//    }
//
//    private void test1(Map<MyKey, Integer> map) {
//        try (MyKey myKey = new MyKey()) {
//            map.put(myKey, 1);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//}