diff --git a/.idea/misc.xml b/.idea/misc.xml index 4f305ef..220965d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,9 @@ + + + + diff --git a/pom.xml b/pom.xml index 234066f..3dee2e8 100644 --- a/pom.xml +++ b/pom.xml @@ -123,11 +123,39 @@ 4.1.39.Final + + net.jcip jcip-annotations 1.0 + + + javax.servlet + servlet-api + 2.5 + provided + + + + + org.mybatis + mybatis-spring + 1.3.2 + + + + redis.clients + jedis + 2.9.0 + + + \ No newline at end of file diff --git a/src/main/java/algorithm/Solution.java b/src/main/java/algorithm/Solution.java new file mode 100644 index 0000000..9c0919b --- /dev/null +++ b/src/main/java/algorithm/Solution.java @@ -0,0 +1,119 @@ +package algorithm; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。 + *

+ * 有效的 IP 地址正好由四个整数(每个整数位于 0 到 255 之间组成),整数之间用 '.' 分隔。 + *

+ * leetCode 93 + *

+ * 输入: "25525511135" + * 输出: ["255.255.11.135", "255.255.111.35"] + * + * @author Hikari + * @version 1.0.0 + * @ClassName Solution.java + * @createTime 2020年08月09日 10:12:00 + */ +public class Solution { + + private final static List res = new ArrayList<>(); + + private final static int DEF = 4; + + private final static int MIN_SIZE = 3; + + + public static void main(String[] args) { + new Solution().restoreIpAddresses("25525511135"); + } + + public List restoreIpAddresses(String s) { + List ret = new ArrayList<>(); + StringBuilder ip = new StringBuilder(); + for (int a = 1; a < 4; ++a) { + for (int b = 1; b < 4; ++b) { + for (int c = 1; c < 4; ++c) { + for (int d = 1; d < 4; ++d) { + if (a + b + c + d == s.length()) { + int n1 = Integer.parseInt(s.substring(0, a)); + int n2 = Integer.parseInt(s.substring(a, a + b)); + int n3 = Integer.parseInt(s.substring(a + b, a + b + c)); + int n4 = Integer.parseInt(s.substring(a + b + c)); + if (n1 <= 255 && n2 <= 255 && n3 <= 255 && n4 <= 255) { + ip.append(n1). + append('.'). + append(n2). + append('.'). + append(n3). + append('.').append(n4); + if (ip.length() == s.length() + 3) { + ret.add(ip.toString()); + } + ip.delete(0, ip.length()); + } + } + } + } + } + } + return ret; + } + + + /* public List restoreIpAddresses(String s) { + if (s.length() < DEF || Objects.isNull(s)) { + return res; + } + for (int i = 1; i < s.length() && i <= MIN_SIZE; ++i) { + for (int j = i; j < s.length() && j <= i + MIN_SIZE; ++j) { + for (int k = j; k < s.length() && k <= j + MIN_SIZE; ++k) { + String s1 = s.substring(0, i); + String s2 = s.substring(i, j); + String s3 = s.substring(j, k); + String s4 = s.substring(k); + if (trueOrFalse(s1) && + trueOrFalse(s2) && + trueOrFalse(s3) && + trueOrFalse(s4)) { + StringBuilder sb = new StringBuilder(); + sb.append(s1); + sb.append("."); + sb.append(s2); + sb.append("."); + sb.append(s3); + sb.append("."); + sb.append(s4); + res.add(sb.toString()); + } + } + } + } + return res; + } + */ + public static boolean trueOrFalse(String string) { + if (string.length() == 0) { + return false; + } + if (string.length() == 1) { + return true; + } + if (string.length() > MIN_SIZE) { + return false; + } + if (string.charAt(0) == '0') { + return true; + } + if (Integer.parseInt(string) <= 255) { + return true; + } + return false; + } +} + + diff --git a/src/main/java/algorithm/hash/ConsistenceHash.java b/src/main/java/algorithm/hash/ConsistenceHash.java new file mode 100644 index 0000000..8a881be --- /dev/null +++ b/src/main/java/algorithm/hash/ConsistenceHash.java @@ -0,0 +1,81 @@ +package algorithm.hash; + +import java.util.List; +import java.util.Map; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +/** + * @author 一方通行 + * @version 1.0 + * @description: 1.首先要解析服务器节点对应到哈希节点上 + * 2.针对 客户端节点ip找到对应的index + * 3.针对客户端的hash值 找到index节点的服务器 + * @date 2021/5/15 10:42 + */ +public class ConsistenceHash { + + public static void main(String[] args) { + haveNode(); + } + + /** + * 没有虚拟节点的hash + */ + private static void notHaveNode() { + String[] serverIps = new String[]{"121.111.12.01", "125.11.0.01", "111.15.12.11"}; + // 直接使用有序map + SortedMap ipMapping = new TreeMap<>(); + for (String ip : serverIps) { + // todo 这里暂时不考虑hashcode 的分散情况 + int serverHash = Math.abs(ip.hashCode()); + ipMapping.put(serverHash, ip); + } + String[] clientIps = new String[]{"100.111.12.01", "10.11.0.01", "10.78.12.12"}; + for (String clientIp : clientIps) { + int clientHash = Math.abs(clientIp.hashCode()); + //这里需要顺时针往下找最近的一个ip节点 + SortedMap integerStringSortedMap = ipMapping.tailMap(clientHash); + if (integerStringSortedMap.isEmpty()) { + String serverIp = ipMapping.get(ipMapping.firstKey()); + System.out.println("处理服务的ip :" + serverIp); + } else { + String serverIp = integerStringSortedMap.get(integerStringSortedMap.firstKey()); + System.out.println("处理服务的ip :" + serverIp); + } + } + } + + private static void haveNode() { + // 定义虚拟节点 + int count = 3; + String[] serverIps = new String[]{"121.111.12.21", "125.11.0.31", "111.15.12.101"}; + // 直接使用有序map + SortedMap ipMapping = new TreeMap<>(); + for (String ip : serverIps) { + // todo 这里暂时不考虑hashcode 的分散情况 + int serverHash = Math.abs(ip.hashCode()); + ipMapping.put(serverHash, ip); + // 这里还需要处理一下虚拟节点 + for (int a = 0; a < count; a++) { + //需要使用真实ip + index + int vHash = Math.abs((ip + "#" + a).hashCode()); + ipMapping.put(vHash, "虚拟节点: " + a + "映射请求 :" + ip); + } + } + String[] clientIps = new String[]{"110.111.12.01", "10.11.0.01", "10.78.12.12"}; + for (String clientIp : clientIps) { + int clientHash = Math.abs(clientIp.hashCode()); + //这里需要顺时针往下找最近的一个ip节点 + SortedMap integerStringSortedMap = ipMapping.tailMap(clientHash); + if (integerStringSortedMap.isEmpty()) { + String serverIp = ipMapping.get(ipMapping.firstKey()); + System.out.println("处理服务的ip :" + serverIp); + } else { + String serverIp = integerStringSortedMap.get(integerStringSortedMap.firstKey()); + System.out.println("处理服务的ip :" + serverIp); + } + } + } +} diff --git a/src/main/java/algorithm/package-info.java b/src/main/java/algorithm/package-info.java new file mode 100644 index 0000000..c337d35 --- /dev/null +++ b/src/main/java/algorithm/package-info.java @@ -0,0 +1,9 @@ +/** + * leetCode 每日一题 + * + * @ClassName package-info.java + * @author Hikari + * @version 1.0.0 + * @createTime 2020年08月09日 10:00:00 + */ +package algorithm; \ No newline at end of file diff --git a/src/main/java/com/basics/UnsafeTest/unsafe_voliate/UnsafeTestTwo.java b/src/main/java/com/basics/UnsafeTest/unsafe_voliate/UnsafeTestTwo.java index f859233..36cb99c 100644 --- a/src/main/java/com/basics/UnsafeTest/unsafe_voliate/UnsafeTestTwo.java +++ b/src/main/java/com/basics/UnsafeTest/unsafe_voliate/UnsafeTestTwo.java @@ -12,7 +12,7 @@ * @Date 2018/8/22 11:20 * @Version 1.0 * - * pageSize :4096 + * pageSize 内存页大小 :4096 * address :8 变量指针的长度(4/8 32/64系统) * anLong :12 **/ diff --git a/src/main/java/com/basics/collection/map/HashMapTest.java b/src/main/java/com/basics/collection/map/HashMapTest.java index d4d2111..cdfc2b6 100644 --- a/src/main/java/com/basics/collection/map/HashMapTest.java +++ b/src/main/java/com/basics/collection/map/HashMapTest.java @@ -1,5 +1,6 @@ package com.basics.collection.map; +import java.util.Arrays; import java.util.HashMap; /** @@ -17,6 +18,10 @@ public static void main(String[] args) { int i = 2147483647; int count = Math.max(2*8, 11); System.out.println(count); + byte[] b1= new byte[]{'c'}; + String s = Arrays.toString(b1); + byte[] b2 = s.getBytes(); + System.out.println(b1==b2); } } diff --git a/src/main/java/com/concurrent/map/ConcurrentHashMapTest.java b/src/main/java/com/concurrent/map/ConcurrentHashMapTest.java new file mode 100644 index 0000000..7b8b56b --- /dev/null +++ b/src/main/java/com/concurrent/map/ConcurrentHashMapTest.java @@ -0,0 +1,18 @@ +package com.concurrent.map; + +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName ConcurrentHashMapTest.java + * @createTime 2021年02月02日 22:22:00 + */ +public class ConcurrentHashMapTest { + + public static void main(String[] args) { + ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap<>(); + concurrentHashMap.put("测试", "陈胖子"); + + } +} diff --git a/src/main/java/com/concurrent/testsynchronized/TestSynchronized.java b/src/main/java/com/concurrent/testsynchronized/TestSynchronized.java new file mode 100644 index 0000000..92b1787 --- /dev/null +++ b/src/main/java/com/concurrent/testsynchronized/TestSynchronized.java @@ -0,0 +1,32 @@ +package com.concurrent.testsynchronized; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author 一方通行 + * @title: TestSynchronized + * @projectName JavaCode + * @date 2020/8/24 23:07 + */ +@SpringBootApplication +public class TestSynchronized { + + private final static Object lock = new Object(); + + public static void main(String[] args) throws InterruptedException { + new TestSynchronized().sayHello(); + } + + public void sayHello() throws InterruptedException { + synchronized (lock) { + System.out.println("hello word"); + wait(); + } + } + public void hha() throws InterruptedException { + synchronized (lock) { + System.out.println("hello word"); + // do xxx + } + } +} diff --git a/src/main/java/com/concurrent/testsynchronized/TestSynchronizedOne.java b/src/main/java/com/concurrent/testsynchronized/TestSynchronizedOne.java new file mode 100644 index 0000000..4589cec --- /dev/null +++ b/src/main/java/com/concurrent/testsynchronized/TestSynchronizedOne.java @@ -0,0 +1,18 @@ +package com.concurrent.testsynchronized; + +/** + * @author 一方通行 + * @title: TestSynchronizedOne + * @projectName JavaCode + * @date 2020/8/26 11:07 + */ +public class TestSynchronizedOne { + + public static void main(String[] args) { + + } + + public static void sayHello() { + + } +} diff --git a/src/main/java/com/concurrent/testsynchronized/package-info.java b/src/main/java/com/concurrent/testsynchronized/package-info.java new file mode 100644 index 0000000..59f6114 --- /dev/null +++ b/src/main/java/com/concurrent/testsynchronized/package-info.java @@ -0,0 +1,8 @@ +/** + * @title: package-info + * @projectName JavaCode + * @description: 测试synchronized + * @author crazy + * @date 2020/8/2423:07 + */ +package com.concurrent.testsynchronized; \ No newline at end of file diff --git a/src/main/java/com/concurrent/thread/wait/AddThread.java b/src/main/java/com/concurrent/thread/wait/AddThread.java new file mode 100644 index 0000000..1fd0d32 --- /dev/null +++ b/src/main/java/com/concurrent/thread/wait/AddThread.java @@ -0,0 +1,14 @@ +package com.concurrent.thread.wait; + +/** + * @author 一方通行 + * @title: AddThread + * @projectName JavaCode + * @date 2020/8/28 17:47 + */ +public class AddThread implements Runnable{ + @Override + public void run() { + + } +} diff --git a/src/main/java/com/concurrent/thread/wait/CountTest.java b/src/main/java/com/concurrent/thread/wait/CountTest.java new file mode 100644 index 0000000..31b3c90 --- /dev/null +++ b/src/main/java/com/concurrent/thread/wait/CountTest.java @@ -0,0 +1,51 @@ +package com.concurrent.thread.wait; + +import com.jvm.bytecode.A; + +/** + * @author 一方通行 + * @title: CountTest + * @projectName JavaCode + * @date 2020/8/28 17:37 + */ +public class CountTest { + + private static int count = 0; + + private static Object lock = new Object(); + + public static void main(String[] args) { + Thread addCount = new Thread(() -> { + addCount(); + }); + + Thread subtractionCount = new Thread(() -> { + try { + subtractionCount(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + addCount.start(); + subtractionCount.start(); + + } + + + public static void addCount() { + synchronized (lock) { + count++; + System.out.println(count); + lock.notify(); + } + } + + public static void subtractionCount() throws InterruptedException { + synchronized (lock) { + count--; + System.out.println(count); + lock.wait(); + } + } + +} diff --git a/src/main/java/com/concurrent/thread/wait/CountTestTwo.java b/src/main/java/com/concurrent/thread/wait/CountTestTwo.java new file mode 100644 index 0000000..cdaf168 --- /dev/null +++ b/src/main/java/com/concurrent/thread/wait/CountTestTwo.java @@ -0,0 +1,57 @@ +package com.concurrent.thread.wait; + +/** + * @author 一方通行 + * @title: CountTestTwo + * @projectName JavaCode + * @date 2020/8/28 17:59 + */ +public class CountTestTwo { + public static int counter; + + public static Object lock = new Object(); + + public static void increase() throws InterruptedException { + synchronized (lock) { + while (counter != 0) { + lock.wait(); + } + counter++; + System.out.println(counter); + lock.notify(); + } + } + + public static void decrease() throws InterruptedException { + synchronized (lock) { + while (counter == 0) { + lock.wait(); + } + counter--; + System.out.println(counter); + lock.notify(); + } + } + + public static void main(String[] args) { + Thread addCount = new Thread(() -> { + try { + increase(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + + Thread subtractionCount = new Thread(() -> { + try { + decrease(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + addCount.start(); + subtractionCount.start(); + + } + +} diff --git a/src/main/java/com/concurrent/thread/wait/TestWait.java b/src/main/java/com/concurrent/thread/wait/TestWait.java new file mode 100644 index 0000000..2a6c579 --- /dev/null +++ b/src/main/java/com/concurrent/thread/wait/TestWait.java @@ -0,0 +1,18 @@ +package com.concurrent.thread.wait; + +/** + * @author 一方通行 + * @title: TestWait + * @projectName JavaCode + * @date 2020/8/27 15:55 + */ +public class TestWait { + + public static void main(String[] args) throws InterruptedException { + new TestWait().ss(); + } + + public synchronized void ss () throws InterruptedException { + wait(); + } +} diff --git a/src/main/java/com/evething/SolutionTest.java b/src/main/java/com/evething/SolutionTest.java new file mode 100644 index 0000000..4061bad --- /dev/null +++ b/src/main/java/com/evething/SolutionTest.java @@ -0,0 +1,38 @@ +package com.evething; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName SolutionTest.java + * @createTime 2020年07月08日 15:32:00 + */ +public class SolutionTest { + + public static void main(String[] args) { + int[] arr = {1, 4, 23, 2, 6, 9, 13, 5, 45, 12}; + int k = 3; + System.out.println(new SolutionTest().findKth(arr, k)); + } + + public int findKth(int[] arr, int k) { + if (k == 0) { + return 0; + } + // 统计每个数字出现的次数 + int[] counter = new int[10000]; + for (int num : arr) { + counter[num] = counter[num] + 1; + } + int x = 0; + // 遍历 counter,查找第 k 小的奇数 + for (int num = 0; num < counter.length; num++) { + if (counter[num] > 0 && x < k && num % 2 == 1) { + x++; + } + if (x == k) { + return num; + } + } + return 0; + } +} diff --git a/src/main/java/com/jdk8/base/Apple.java b/src/main/java/com/jdk8/base/Apple.java new file mode 100644 index 0000000..3f62159 --- /dev/null +++ b/src/main/java/com/jdk8/base/Apple.java @@ -0,0 +1,30 @@ +package com.jdk8.base; + +import lombok.Data; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName Apple.java + * @createTime 2020年12月05日 17:42:00 + */ +@Data +public class Apple { + + private String color; + + private int weight; + + public Apple(String color, int weight) { + this.color = color; + this.weight = weight; + } + + public Apple(String color) { + this.color = color; + } + + public static boolean isGreen(Apple apple) { + return "green".equals(apple.getColor()); + } +} diff --git a/src/main/java/com/jdk8/base/FunctionApple.java b/src/main/java/com/jdk8/base/FunctionApple.java new file mode 100644 index 0000000..cc06b82 --- /dev/null +++ b/src/main/java/com/jdk8/base/FunctionApple.java @@ -0,0 +1,58 @@ +package com.jdk8.base; + + +import lombok.Data; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName FunctionApple.java + * @createTime 2020年12月05日 17:44:00 + */ +public class FunctionApple { + + + public static void main(String[] args) { + List req = Arrays.asList(new Apple("red", 100), + new Apple("red", 150), + new Apple("green", 100)); + + List apples = filterApple(req, FunctionApple::isGreen); + + Predicate predicate = s -> apples.add(new Apple("1", 2)); + System.out.println(predicate.test(new Apple("1", 2))); + + /* Function integerFunction = (String s) -> Integer.parseInt(s);*/ + Function integerFunction = Integer::parseInt; + System.out.println(integerFunction.apply("2")); + Function createApple = Apple::new; + System.out.println(createApple.apply("黄色").toString()); + BiFunction biFunction = Apple::new; + System.out.println(biFunction.apply("黄色", 200).toString()); + + } + + + private static boolean isGreen(Apple apple) { + return "green".equals(apple.getColor()); + } + + public static List filterApple(List list, Predicate predicate) { + List result = new ArrayList<>(); + list.forEach(apple -> { + if (predicate.test(apple)) { + result.add(apple); + } + }); + return result; + } + + +} diff --git a/src/main/java/com/jvm/gc/TestHandlePromotionFailure.java b/src/main/java/com/jvm/gc/TestHandlePromotionFailure.java new file mode 100644 index 0000000..96bd7da --- /dev/null +++ b/src/main/java/com/jvm/gc/TestHandlePromotionFailure.java @@ -0,0 +1,28 @@ +package com.jvm.gc; + +import javax.xml.crypto.Data; + +/** + * 测试分代担保机制 + * + * @author 一方通行 + * @title: TestHandlePromotionFailure + * @projectName JavaCode + * @date 2020/8/22 18:50 + */ +public class TestHandlePromotionFailure { + private static final int DEF_MB = 1024 * 1024; + + public static void testHandlePromotion() { + byte[] allocation1, allocation2, allocation3, allocation4; + allocation1 = new byte[2 * DEF_MB]; + allocation2 = new byte[2 * DEF_MB]; + allocation3 = new byte[2 * DEF_MB]; + allocation4 = new byte[4 * DEF_MB]; + } + + public static void main(String[] args) { + testHandlePromotion(); + } + +} diff --git a/src/main/java/com/jvm/gc/TestHandlePromotionFailure.md b/src/main/java/com/jvm/gc/TestHandlePromotionFailure.md new file mode 100644 index 0000000..d14bad9 --- /dev/null +++ b/src/main/java/com/jvm/gc/TestHandlePromotionFailure.md @@ -0,0 +1,33 @@ +## 分代担保学习 + +~~~~ java +private static final int DEF_MB = 1024 * 1024; + + public static void testHandlePromotion() { + byte[] allocation1, allocation2, allocation3, allocation4; + allocation1 = new byte[2 * DEF_MB]; + allocation2 = new byte[2 * DEF_MB]; + allocation3 = new byte[2 * DEF_MB]; + allocation4 = new byte[4 * DEF_MB]; + } + + public static void main(String[] args) { + testHandlePromotion(); + } +~~~~ +gc回收日志如下 + +~~~~ java +/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/bin/java -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=56007:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/lib/tools.jar:/Users/crazy/Desktop/code/JavaCode/target/classes:/Users/crazy/.m2/repository/org/springframework/spring-aop/4.3.8.RELEASE/spring-aop-4.3.8.RELEASE.jar:/Users/crazy/.m2/repository/org/springframework/spring-beans/4.3.8.RELEASE/spring-beans-4.3.8.RELEASE.jar:/Users/crazy/.m2/repository/org/springframework/spring-core/4.3.8.RELEASE/spring-core-4.3.8.RELEASE.jar:/Users/crazy/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/Users/crazy/.m2/repository/org/springframework/boot/spring-boot-starter-aop/1.5.11.RELEASE/spring-boot-starter-aop-1.5.11.RELEASE.jar:/Users/crazy/.m2/repository/org/springframework/boot/spring-boot-starter/1.5.11.RELEASE/spring-boot-starter-1.5.11.RELEASE.jar:/Users/crazy/.m2/repository/org/springframework/boot/spring-boot-starter-logging/1.5.11.RELEASE/spring-boot-starter-logging-1.5.11.RELEASE.jar:/Users/crazy/.m2/repository/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar:/Users/crazy/.m2/repository/ch/qos/logback/logback-core/1.1.11/logback-core-1.1.11.jar:/Users/crazy/.m2/repository/org/slf4j/slf4j-api/1.7.22/slf4j-api-1.7.22.jar:/Users/crazy/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.25/jcl-over-slf4j-1.7.25.jar:/Users/crazy/.m2/repository/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar:/Users/crazy/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.25/log4j-over-slf4j-1.7.25.jar:/Users/crazy/.m2/repository/org/yaml/snakeyaml/1.17/snakeyaml-1.17.jar:/Users/crazy/.m2/repository/org/aspectj/aspectjweaver/1.8.13/aspectjweaver-1.8.13.jar:/Users/crazy/.m2/repository/cglib/cglib/2.1_3/cglib-2.1_3.jar:/Users/crazy/.m2/repository/asm/asm/1.5.3/asm-1.5.3.jar:/Users/crazy/.m2/repository/cglib/cglib-nodep/2.2/cglib-nodep-2.2.jar:/Users/crazy/.m2/repository/redis/clients/jedis/2.9.0/jedis-2.9.0.jar:/Users/crazy/.m2/repository/org/apache/commons/commons-pool2/2.4.2/commons-pool2-2.4.2.jar:/Users/crazy/.m2/repository/junit/junit/4.13/junit-4.13.jar:/Users/crazy/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/crazy/.m2/repository/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar:/Users/crazy/.m2/repository/org/apache/commons/commons-lang3/3.8/commons-lang3-3.8.jar:/Users/crazy/.m2/repository/commons-codec/commons-codec/1.11/commons-codec-1.11.jar:/Users/crazy/.m2/repository/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1.jar:/Users/crazy/.m2/repository/org/springframework/boot/spring-boot-devtools/1.5.17.RELEASE/spring-boot-devtools-1.5.17.RELEASE.jar:/Users/crazy/.m2/repository/org/springframework/boot/spring-boot/1.5.17.RELEASE/spring-boot-1.5.17.RELEASE.jar:/Users/crazy/.m2/repository/org/springframework/spring-context/4.3.20.RELEASE/spring-context-4.3.20.RELEASE.jar:/Users/crazy/.m2/repository/org/springframework/spring-expression/4.3.20.RELEASE/spring-expression-4.3.20.RELEASE.jar:/Users/crazy/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.5.17.RELEASE/spring-boot-autoconfigure-1.5.17.RELEASE.jar:/Users/crazy/.m2/repository/org/projectlombok/lombok/1.16.14/lombok-1.16.14.jar:/Users/crazy/.m2/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar:/Users/crazy/.m2/repository/net/jcip/jcip-annotations/1.0/jcip-annotations-1.0.jar:/Users/crazy/Downloads/mysql-binlog-connector-java-0.21.0.jar com.jvm.gc.TestHandlePromotionFailure +[GC (Allocation Failure) [DefNew: 6465K->569K(9216K), 0.0037041 secs] 6465K->4665K(19456K), 0.0037317 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] +Heap + def new generation total 9216K, used 7035K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000) + eden space 8192K, 78% used [0x00000007bec00000, 0x00000007bf250710, 0x00000007bf400000) + from space 1024K, 55% used [0x00000007bf500000, 0x00000007bf58e538, 0x00000007bf600000) + to space 1024K, 0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000) + tenured generation total 10240K, used 4096K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000) + the space 10240K, 40% used [0x00000007bf600000, 0x00000007bfa00020, 0x00000007bfa00200, 0x00000007c0000000) + Metaspace used 3041K, capacity 4496K, committed 4864K, reserved 1056768K + class space used 329K, capacity 388K, committed 512K, reserved 1048576K + +~~~~ \ No newline at end of file diff --git a/src/main/java/com/jvm/gc/package-info.java b/src/main/java/com/jvm/gc/package-info.java new file mode 100644 index 0000000..f359a97 --- /dev/null +++ b/src/main/java/com/jvm/gc/package-info.java @@ -0,0 +1,8 @@ +/** + * @title: package-info + * @projectName JavaCode + * @description: gc相关的知识点 + * @author crazy + * @date 2020/8/2218:48 + */ +package com.jvm.gc; \ No newline at end of file diff --git a/src/main/java/com/jvm/quote/SoftReferenceTest.java b/src/main/java/com/jvm/quote/SoftReferenceTest.java new file mode 100644 index 0000000..f2aa67a --- /dev/null +++ b/src/main/java/com/jvm/quote/SoftReferenceTest.java @@ -0,0 +1,14 @@ +package com.jvm.quote; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName SoftRefeerenceTest.java + * @createTime 2020年07月16日 22:19:00 + */ +public class SoftReferenceTest { + + public static void main(String[] args) { + + } +} diff --git a/src/main/java/com/jvm/quote/package-info.java b/src/main/java/com/jvm/quote/package-info.java new file mode 100644 index 0000000..873b09c --- /dev/null +++ b/src/main/java/com/jvm/quote/package-info.java @@ -0,0 +1,8 @@ +/** + * 引用 + * @ClassName package-info.java + * @author Hikari + * @version 1.0.0 + * @createTime 2020年07月16日 22:18:00 + */ +package com.jvm.quote; \ No newline at end of file diff --git a/src/main/java/com/thread/demo/BlockingQueueForCondition.java b/src/main/java/com/thread/demo/BlockingQueueForCondition.java new file mode 100644 index 0000000..aa8f2ff --- /dev/null +++ b/src/main/java/com/thread/demo/BlockingQueueForCondition.java @@ -0,0 +1,68 @@ +package com.thread.demo; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @author crazy + * @title: BlockingQueueForCondition + * @projectName JavaCode + * @description: 自定义实现生产和消费者 + * @date 2020/7/20 14:43 + */ +public class BlockingQueueForCondition { + + private Queue queue; + + private int maxSize = 16; + + private ReentrantLock lock = new ReentrantLock(); + + private Condition isNull = lock.newCondition(); + + private Condition isFull = lock.newCondition(); + + public BlockingQueueForCondition(int maxSize) { + this.maxSize = maxSize; + queue = new LinkedList(); + } + + + public static void main(String[] args) { + + } + + public void putValue(String value) { + lock.lock(); + try { + while (queue.size() == maxSize) { + isFull.await(); + } + queue.add(value); + isNull.signalAll(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + lock.unlock(); + } + } + + public String getValue() { + lock.lock(); + try { + while (queue.isEmpty()) { + isNull.await(); + } + String value = (String) queue.remove(); + isFull.signalAll(); + return value; + } catch (Exception e) { + + } finally { + lock.unlock(); + } + return null; + } +} diff --git a/src/main/java/com/thread/demo/BlockingQueueForWaitAndNotify.java b/src/main/java/com/thread/demo/BlockingQueueForWaitAndNotify.java new file mode 100644 index 0000000..a820df8 --- /dev/null +++ b/src/main/java/com/thread/demo/BlockingQueueForWaitAndNotify.java @@ -0,0 +1,42 @@ +package com.thread.demo; + +import java.util.LinkedList; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName BlockingQueueForWaitAndNotify.java + * @createTime 2020年07月21日 22:30:00 + */ +public class BlockingQueueForWaitAndNotify { + + private int maxSize = 16; + + private LinkedList linkedList; + + public BlockingQueueForWaitAndNotify(int maxSize, LinkedList linkedList) { + this.maxSize = maxSize; + this.linkedList = linkedList; + } + + public static void main(String[] args) { + + } + + public synchronized void putValue(String value) throws Exception { + while (linkedList.size() == maxSize) { + wait(); + } + linkedList.add(value); + notifyAll(); + } + + public synchronized String getValue() throws Exception { + while (linkedList.size() == 0) { + wait(); + } + String value = linkedList.remove(); + notifyAll(); + return value; + } +} diff --git a/src/main/java/com/thread/demo/Consumer.java b/src/main/java/com/thread/demo/Consumer.java new file mode 100644 index 0000000..84df657 --- /dev/null +++ b/src/main/java/com/thread/demo/Consumer.java @@ -0,0 +1,46 @@ +package com.thread.demo; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingDeque; +import java.util.concurrent.BlockingQueue; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName Consumer.java + * @createTime 2020年07月17日 22:10:00 + */ +public class ConsumerConsumer { + + ArrayBlockingQueue storage; + + public Consumer(ArrayBlockingQueue storage) { + this.storage = storage; + } + + public boolean getValue() { + float defValue = 0.77f; + if (Math.random() > defValue) { + return false; + } + return true; + } + + public static void main(String[] args) throws InterruptedException { + ArrayBlockingQueue store = new ArrayBlockingQueue(8); + Producer producer = new Producer(store); + Thread producerThread = new Thread(producer); + producerThread.start(); + Thread.sleep(500); + Consumer consumer = new Consumer(store); + while (consumer.getValue()) { + System.out.println("哈哈哈 开始消费了" + consumer.storage.take()); + Thread.sleep(100); + } + System.out.println("消费结束了"); + /* producer.canceled = true;*/ + producerThread.interrupt(); + System.out.println(producer.canceled); + + } +} diff --git a/src/main/java/com/thread/demo/Producer.java b/src/main/java/com/thread/demo/Producer.java new file mode 100644 index 0000000..9e3cacc --- /dev/null +++ b/src/main/java/com/thread/demo/Producer.java @@ -0,0 +1,40 @@ +package com.thread.demo; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName Producer.java + * @createTime 2020年07月17日 22:04:00 + */ +public class Producer implements Runnable { + public volatile boolean canceled = false; + ArrayBlockingQueue storage; + + public Producer(ArrayBlockingQueue storage) { + this.storage = storage; + } + + @Override + public void run() { + int num = 0; + int max = 100000; + try { + while (num <= max && !Thread.currentThread().isInterrupted()) { + if (num % 50 == 0) { + storage.put(num); + System.out.println("50的倍数 :" + num); + } + num++; + } + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + System.out.println("运行结束"); + } + + + } +} diff --git a/src/main/java/com/thread/demo/ProducerDemon.java b/src/main/java/com/thread/demo/ProducerDemon.java new file mode 100644 index 0000000..070f2b0 --- /dev/null +++ b/src/main/java/com/thread/demo/ProducerDemon.java @@ -0,0 +1,42 @@ +package com.thread.demo; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +/** + * @author crazy + * @title: ProducerDemon + * @projectName JavaCode + * @description: 生产和消费者模式 + * @date 2020/7/20 11:35 + */ +public class ProducerDemon { + + + public static void main(String[] args) { + BlockingQueue blockingDeque = new ArrayBlockingQueue(10); + Runnable producer = () -> { + while (true) { + try { + blockingDeque.put("哈哈"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }; + new Thread(producer).start(); + new Thread(producer).start(); + + Runnable consumer = () -> { + while (true) { + try { + System.out.println(blockingDeque.take()); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }; + new Thread(consumer).start(); + new Thread(consumer).start(); + } +} diff --git a/src/main/java/com/thread/demo/ThreadJoin.java b/src/main/java/com/thread/demo/ThreadJoin.java new file mode 100644 index 0000000..e67828b --- /dev/null +++ b/src/main/java/com/thread/demo/ThreadJoin.java @@ -0,0 +1,11 @@ +package com.thread.demo; + +/** + * @author crazy + * @title: ThreadJoin + * @projectName JavaCode + * @description: TODO + * @date 2020/7/25 21:58 + */ +public class ThreadJoin { +} diff --git a/src/main/java/com/thread/demo/ThreadStatusTest.java b/src/main/java/com/thread/demo/ThreadStatusTest.java new file mode 100644 index 0000000..ee6ac6b --- /dev/null +++ b/src/main/java/com/thread/demo/ThreadStatusTest.java @@ -0,0 +1,37 @@ +package com.thread.demo; + +import lombok.SneakyThrows; + +/** + * @author crazy + * @title: ThreadStatusTest + * @projectName JavaCode + * @description: TODO + * @date 2020/7/18 15:02 + */ +public class ThreadStatusTest { + + public static void main(String[] args) throws InterruptedException { + Thread thread = new Thread(new ThreadStatus()); + System.out.println("new Thread status :" + thread.getState()); + thread.setName("thead name is test"); + System.out.println("main thread start run"); + System.out.println("main thead status :" + Thread.currentThread().getState()); + Thread.sleep(20000); + System.out.println("main thead status :" + Thread.currentThread().getState()); + System.out.println("thead status :" + thread.getState()); + thread.start(); + System.out.println("哈哈"); + + } + + private static class ThreadStatus implements Runnable { + @SneakyThrows + @Override + public void run() { + System.out.println("thread runnable "); + System.out.println("thead status :" + Thread.currentThread().getState()); + Thread.sleep(2000); + } + } +} diff --git a/src/main/java/com/thread/demo/WaitMethodTest.java b/src/main/java/com/thread/demo/WaitMethodTest.java new file mode 100644 index 0000000..256eefa --- /dev/null +++ b/src/main/java/com/thread/demo/WaitMethodTest.java @@ -0,0 +1,52 @@ +package com.thread.demo; + + +import java.util.LinkedList; +import java.util.Queue; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName WaitMethodTest.java + * @createTime 2020年07月19日 21:22:00 + */ +public class WaitMethodTest { + + public Queue blockingQueue = new LinkedList<>(); + + public static void main(String[] args) { + WaitMethodTest waitMethodTest = new WaitMethodTest(); + Runnable threadOne = () -> { + String putValue = "value"; + waitMethodTest.put(putValue); + }; + Runnable threadTwo = () -> { + String getValue = waitMethodTest.get(); + System.out.println(getValue); + }; + Thread putThread = new Thread(threadOne); + putThread.setName("putValueThread"); + Thread getThread = new Thread(threadTwo); + getThread.setName("getValueThread"); + putThread.start(); + getThread.start(); + + } + + public void put(String value) { + blockingQueue.add(value); + //唤醒等待的第一个 + notify(); + } + + public String get() { + while (blockingQueue.isEmpty()) { + try { + wait(); + } catch (InterruptedException e) { + System.out.println("哈哈的 等待超时"); + } + } + return blockingQueue.remove(); + } +} diff --git a/src/main/java/mysql/TTTT.java b/src/main/java/mysql/TTTT.java new file mode 100644 index 0000000..be5603f --- /dev/null +++ b/src/main/java/mysql/TTTT.java @@ -0,0 +1,25 @@ +package mysql; + +import java.io.File; +import java.io.IOException; + +/** + * @author crazy + * @title: TTTT + * @projectName JavaCode + * @description: TODO + * @date 2020/7/316:28 + */ +public class TTTT { + public static void main(String[] args) throws IOException { + /* String filePath="/Users/crazy/Downloads/mysql_bin.000304"; + File binlogFile = new File(filePath); + EventDeserializer eventDeserializer = new EventDeserializer(); + eventDeserializer.setChecksumType(ChecksumType.CRC32); + BinaryLogFileReader reader = new BinaryLogFileReader(binlogFile, eventDeserializer); + for (Event event; (event = reader.readEvent()) != null; ) { + System.out.println(event.toString()); + } + reader.close();*/ + } +} diff --git a/src/main/java/netty/demo/TimeServer.java b/src/main/java/netty/demo/TimeServer.java new file mode 100644 index 0000000..c96962c --- /dev/null +++ b/src/main/java/netty/demo/TimeServer.java @@ -0,0 +1,49 @@ +package netty.demo; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoop; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.sctp.nio.NioSctpServerChannel; +import io.netty.channel.socket.SocketChannel; + +/** + * @author crazy + * @title: TimeServer + * @projectName JavaCode + * @description: TODO + * @date 2020/7/1711:38 + */ +public class TimeServer { + + public static void main(String[] args) throws InterruptedException { + //创建boos线程 管理worker线程 + EventLoopGroup bossGroup = new NioEventLoopGroup(); + //创建work线程 + EventLoopGroup workGroup = new NioEventLoopGroup(); + //创建服务端 + ServerBootstrap bootstrap = new ServerBootstrap(); + // + bootstrap.group(bossGroup, workGroup) + //设置管道类型 + .channel(NioSctpServerChannel.class) + //设置tcp 参数, 一次1024字节大小 + .option(ChannelOption.SO_BACKLOG, 1024) + //设置处理channel的处理器 + .childHandler(new ChildChannelHandler()); + //绑定异步回调 + ChannelFuture future = bootstrap.bind().sync(); + future.channel().closeFuture(); + bossGroup.shutdownGracefully(); + workGroup.shutdownGracefully(); + } + + private static class ChildChannelHandler extends ChannelInitializer { + + + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new TimeServerHandler()); + } + } +} diff --git a/src/main/java/netty/demo/TimeServerHandler.java b/src/main/java/netty/demo/TimeServerHandler.java new file mode 100644 index 0000000..25719f8 --- /dev/null +++ b/src/main/java/netty/demo/TimeServerHandler.java @@ -0,0 +1,29 @@ +package netty.demo; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * @author crazy + * @title: TimeServerHandler + * @projectName JavaCode + * @description: TODO + * @date 2020/7/17 15:18 + */ +public class TimeServerHandler extends ChannelInboundHandlerAdapter { + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf byteBuf = (ByteBuf) msg; + byte[] reg = new byte[byteBuf.readableBytes()]; + byteBuf.readBytes(reg); + String body = new String(reg, StandardCharsets.UTF_8); + + } + +} diff --git a/src/main/java/netty/demo/im/AutoBindPort.java b/src/main/java/netty/demo/im/AutoBindPort.java new file mode 100644 index 0000000..fc81932 --- /dev/null +++ b/src/main/java/netty/demo/im/AutoBindPort.java @@ -0,0 +1,49 @@ +package netty.demo.im; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelDuplexHandler; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.nio.NioEventLoop; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.GenericFutureListener; +import netty.demo.im.handler.ServerHandler; + +import static jdk.nashorn.internal.objects.NativeFunction.bind; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName AutoBindPort.java + * @createTime 2020年08月07日 22:27:00 + */ +public class AutoBindPort { + + public static void main(String[] args) { + NioEventLoopGroup boss = new NioEventLoopGroup(); + NioEventLoopGroup work = new NioEventLoopGroup(); + ServerBootstrap serverBootstrap = new ServerBootstrap(); + serverBootstrap.group(boss, work) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel ch) { + ch.pipeline().addLast(new ServerHandler()); + } + }); + autoBindPort(serverBootstrap, 8080); + } + + public static void autoBindPort(final ServerBootstrap serverBootstrap, int port) { + serverBootstrap.bind(port).addListener((GenericFutureListener) future -> { + if (future.isSuccess()) { + System.out.println("端口[" + port + "]绑定成功!"); + } else { + System.err.println("端口[" + port + "]绑定失败!"); + bind(serverBootstrap, port + 1); + } + }); + } +} diff --git a/src/main/java/netty/demo/im/NettyClient.java b/src/main/java/netty/demo/im/NettyClient.java new file mode 100644 index 0000000..6c4f150 --- /dev/null +++ b/src/main/java/netty/demo/im/NettyClient.java @@ -0,0 +1,40 @@ +package netty.demo.im; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import netty.demo.im.handler.ClientHandler; + + +/** + * @author crazy + * @title: NettyClient + * @projectName JavaCode + * @description: TODO + * @date 2020/8/9 16:45 + */ +public class NettyClient { + + public static void main(String[] args) { + NioEventLoopGroup workGroup = new NioEventLoopGroup(); + Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(workGroup) + .channel(NioSocketChannel.class) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new ClientHandler()); + } + }).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 500); + bootstrap.connect("localhost", 8080).addListener(future -> { + if (future.isSuccess()) { + System.out.println("连接成功!"); + } else { + System.err.println("连接失败!"); + } + }); + } +} diff --git a/src/main/java/netty/demo/im/handler/ClientHandler.java b/src/main/java/netty/demo/im/handler/ClientHandler.java new file mode 100644 index 0000000..cc82da2 --- /dev/null +++ b/src/main/java/netty/demo/im/handler/ClientHandler.java @@ -0,0 +1,39 @@ +package netty.demo.im.handler; + + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +import java.nio.charset.StandardCharsets; +import java.util.Date; + +/** + * @author 一方通行 + * @title: ClientHandler + * @projectName JavaCode + * @date 2020/8/9 17:01 + */ +public class ClientHandler extends ChannelInboundHandlerAdapter { + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + System.out.println(new Date() + ": 客户端写出数据"); + ByteBuf byteBuf = getByteBuf(ctx); + ctx.channel().writeAndFlush(byteBuf); + } + + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + ByteBuf byteBuf = (ByteBuf) msg; + System.out.println(new Date() + ": 客户端读到数据 -> " + byteBuf.toString(StandardCharsets.UTF_8)); + } + + private ByteBuf getByteBuf(ChannelHandlerContext channelHandlerContext) { + ByteBuf byteBuf = channelHandlerContext.alloc().buffer(); + byte[] bytes = "哈哈 小胖的测试!".getBytes(StandardCharsets.UTF_8); + byteBuf.writeBytes(bytes); + return byteBuf; + } +} diff --git a/src/main/java/netty/demo/im/handler/ServerHandler.java b/src/main/java/netty/demo/im/handler/ServerHandler.java new file mode 100644 index 0000000..fc9f526 --- /dev/null +++ b/src/main/java/netty/demo/im/handler/ServerHandler.java @@ -0,0 +1,35 @@ +package netty.demo.im.handler; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Date; + +/** + * @author 一方通行 + * @title: ServerHandler + * @projectName JavaCode + * @date 2020/8/9 17:11 + */ +public class ServerHandler extends ChannelInboundHandlerAdapter { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + ByteBuf byteBuf = (ByteBuf) msg; + System.out.println(new Date() + ": 服务端读到数据 -> " + byteBuf.toString(StandardCharsets.UTF_8)); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + ByteBuf byteBuf = getByteBuf(ctx); + ctx.writeAndFlush(byteBuf); + } + + private ByteBuf getByteBuf(ChannelHandlerContext handlerContext) { + ByteBuf byteBuf = handlerContext.alloc().buffer(); + byte[] bytes = "哈哈哈 小胖服务器 写回".getBytes(StandardCharsets.UTF_8); + return byteBuf.writeBytes(bytes); + } +} diff --git a/src/main/java/netty/http/HttpHelloWorldServer.java b/src/main/java/netty/http/HttpHelloWorldServer.java index 96ada66..5f20880 100644 --- a/src/main/java/netty/http/HttpHelloWorldServer.java +++ b/src/main/java/netty/http/HttpHelloWorldServer.java @@ -16,14 +16,15 @@ * @author crazy * @title: HttpHelloWorldServer * @projectName JavaCode - * @description: netty自定义service + * @description: netty自定义webService 简陋版 + * * @date 2020/5/3014:43 */ public class HttpHelloWorldServer { private static final int THREAD_NUMBER = 1; - public static void main(String[] args) { + public static void main(String[] args) { try { EventLoopGroup bossGroup = new NioEventLoopGroup(THREAD_NUMBER); EventLoopGroup workerGroup = new NioEventLoopGroup(); diff --git a/src/main/java/netty/java/TimeClient.java b/src/main/java/netty/java/TimeClient.java new file mode 100644 index 0000000..5df4b7a --- /dev/null +++ b/src/main/java/netty/java/TimeClient.java @@ -0,0 +1,51 @@ +package netty.java; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; +import java.util.Objects; + +/** + * @ClassName TimeClinet + * @Description 客户端 + * @Author Crazy + * @Date 2020/6/18 23:13 + */ +public class TimeClient { + + private static final int PORT = 8085; + + public static void main(String[] args) { + Socket socket = null; + BufferedReader in = null; + PrintWriter out = null; + try { + socket = new Socket("127.0.0.1", PORT); + in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + out = new PrintWriter(socket.getOutputStream()); + out.println("query time order"); + String resp = in.readLine(); + System.err.println("now is :" + resp); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (Objects.nonNull(out)) { + out.flush(); + out.close(); + out = null; + } + if (Objects.nonNull(in)) { + try { + in.close(); + in = null; + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + } +} diff --git a/src/main/java/netty/java/TimeServer.java b/src/main/java/netty/java/TimeServer.java new file mode 100644 index 0000000..a44b465 --- /dev/null +++ b/src/main/java/netty/java/TimeServer.java @@ -0,0 +1,42 @@ +package netty.java; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * @ClassName TimeServer + * @Description TODO + * @Author Crazy + * @Date 2020/6/18 23:25 + */ +public class TimeServer { + public static void main(String[] args) throws IOException { + int port = 8085; + ServerSocket server = null; + TimeServerHandlerExecutePool serverHandlerExecutePool = + new TimeServerHandlerExecutePool(50, 100); + try { + // 创建服务 + server = new ServerSocket(port); + System.out.println("The time server is start in port :" + port); + Socket socket = null; + while (true) { + // 监听 + socket = server.accept(); + /* new Thread(new TimeServerHandler(socket)).start();*/ + serverHandlerExecutePool.execute(new TimeServerHandler(socket)); + } + + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (server != null) { + server.close(); + System.out.println("The time server is close!"); + server = null; + } + } + } + +} diff --git a/src/main/java/netty/java/TimeServerHandler.java b/src/main/java/netty/java/TimeServerHandler.java new file mode 100644 index 0000000..fbcea3e --- /dev/null +++ b/src/main/java/netty/java/TimeServerHandler.java @@ -0,0 +1,60 @@ +package netty.java; + +import lombok.Data; +import lombok.extern.slf4j.Slf4j; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; +import java.util.Objects; + +/** + * @ClassName TimeServerHandler + * @Description 自定义客户端连接 + * @Author Crazy + * @Date 2020/6/18 22:58 + */ +@Slf4j +public class TimeServerHandler implements Runnable { + + private final Socket socket; + + public TimeServerHandler(Socket socket) { + this.socket = socket; + } + + + @Override + public void run() { + BufferedReader bufferedReader = null; + PrintWriter out = null; + try { + bufferedReader = new BufferedReader(new InputStreamReader( + socket.getInputStream() + )); + out = new PrintWriter(socket.getOutputStream()); + String body = null; + while (true) { + //循环一直读取 + body = bufferedReader.readLine(); + if (Objects.isNull(body)) { + break; + } + System.err.println("time Server Handler is :" + body); + out.println("哈哈哈 你想干什么啊"); + } + } catch (IOException e) { + System.err.println(e); + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (IOException ioException) { + ioException.printStackTrace(); + } + } + } + + } +} diff --git a/src/main/java/netty/java/TimeServerHandlerExecutePool.java b/src/main/java/netty/java/TimeServerHandlerExecutePool.java new file mode 100644 index 0000000..5964113 --- /dev/null +++ b/src/main/java/netty/java/TimeServerHandlerExecutePool.java @@ -0,0 +1,26 @@ +package netty.java; + +import sun.nio.ch.ThreadPool; + +import java.util.concurrent.*; + +/** + * @ClassName TimeServerHandlerExecutePool + * @Description TODO + * @Author Crazy + * @Date 2020/6/19 22:39 + */ +public class TimeServerHandlerExecutePool { + + private ExecutorService executors; + + public TimeServerHandlerExecutePool(int maxPoolSize, int queueSize) { + executors = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + , maxPoolSize, 120L, TimeUnit.SECONDS, new ArrayBlockingQueue(queueSize)); + } + + public void execute(Runnable task) { + executors.execute(task); + + } +} diff --git a/src/main/java/netty/java/nio/BufferTest.java b/src/main/java/netty/java/nio/BufferTest.java new file mode 100644 index 0000000..bf52813 --- /dev/null +++ b/src/main/java/netty/java/nio/BufferTest.java @@ -0,0 +1,37 @@ +package netty.java.nio; + +import java.nio.CharBuffer; + +/** + * @author crazy + * @title: BufferTest + * @projectName JavaCode + * @description: TODO + * @date 2020/6/2414:18 + */ +public class BufferTest { + + public static void main(String[] args) { + //创建8个长度的 + CharBuffer buffer = CharBuffer.allocate(8); + System.out.println("capacity :" + buffer.capacity()); + System.out.println("limit : " + buffer.limit()); + System.out.println("position : " + buffer.position()); + System.out.println("make : " + buffer.mark()); + buffer.put('a'); + buffer.put('b'); + buffer.put('c'); + buffer.reset(); + System.out.println("加入三个元素 position :" + buffer.position()); + buffer.flip(); + System.out.println("执行flip 后:limit = " + buffer.limit()); + System.out.println("取出第一个元素position=0 " + + buffer.get()); + System.out.println("取出第一个元素后position : " + buffer.position()); + buffer.clear(); + System.out.println("clear limit : "+ buffer.limit()); + System.out.println("clear position : "+ buffer.position()); + System.out.println("clear buffer : "+ buffer.get(2)); + System.out.println("执行index读取后:" + buffer.position()); + } +} diff --git a/src/main/java/netty/java/nio/ByteBufferTest.java b/src/main/java/netty/java/nio/ByteBufferTest.java new file mode 100644 index 0000000..ac73248 --- /dev/null +++ b/src/main/java/netty/java/nio/ByteBufferTest.java @@ -0,0 +1,31 @@ +package netty.java.nio; + +import java.nio.ByteBuffer; + +/** + * @author crazy + * @title: ByteBufferTest + * @projectName JavaCode + * @description: TODO + * @date 2020/6/2311:46 + */ +public class ByteBufferTest { + + public static void main(String[] args) { + /*ByteBuffer byteBuffer = ByteBuffer.allocate();*/ + ByteBuffer byteBuffer = ByteBuffer.allocate(1024); + byte[] bytes = new byte[10]; + bytes[0] = 1; + byteBuffer.put(bytes); + System.out.println(byteBuffer.capacity()); + System.out.println(byteBuffer.limit()); + System.out.println(byteBuffer.position()); + byteBuffer.limit(100); + System.out.println(byteBuffer.limit()); + System.out.println(byteBuffer.position()); + System.out.println(byteBuffer.mark()); + System.out.println(byteBuffer.remaining()); + + + } +} diff --git a/src/main/java/netty/java/nio/ByteBufferTest1.java b/src/main/java/netty/java/nio/ByteBufferTest1.java new file mode 100644 index 0000000..0d42742 --- /dev/null +++ b/src/main/java/netty/java/nio/ByteBufferTest1.java @@ -0,0 +1,19 @@ +package netty.java.nio; + + +import java.nio.ByteBuffer; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName ByteBufferTest1.java + * @createTime 2020年07月26日 17:19:00 + */ +public class ByteBufferTest1 { + + public static void main(String[] args) throws ClassNotFoundException { + //直接缓冲区 直接在内存汇总 + ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024); + + } +} diff --git a/src/main/java/netty/java/nio/DirectByteBufferTest.java b/src/main/java/netty/java/nio/DirectByteBufferTest.java new file mode 100644 index 0000000..710dad9 --- /dev/null +++ b/src/main/java/netty/java/nio/DirectByteBufferTest.java @@ -0,0 +1,27 @@ +package netty.java.nio; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName DirectByteBufferTestr.java + * @createTime 2020年07月27日 22:22:00 + */ +public class DirectByteBufferTest { + + public static void main(String[] args) { + test(); + } + + + public static void test() { + List list = new ArrayList(); + while (true) { + ByteBuffer buffer = ByteBuffer.allocateDirect(1 * 1024 * 1024); + list.add(buffer); + } + } +} diff --git a/src/main/java/netty/java/nio/TimeClient.java b/src/main/java/netty/java/nio/TimeClient.java new file mode 100644 index 0000000..1c24db6 --- /dev/null +++ b/src/main/java/netty/java/nio/TimeClient.java @@ -0,0 +1,29 @@ +package netty.java.nio; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; + +/** + * @ClassName TimeClient + * @Description Java nio client + * @Author Crazy + * @Date 2020/6/22 22:29 + */ +public class TimeClient { + + private static final int PORT = 9093; + + public static void main(String[] args) throws IOException { + SocketAddress socketAddress = new InetSocketAddress("localhost", PORT); + SocketChannel socketChannel = SocketChannel.open(); + SocketChannel channel = SocketChannel.open(); + channel.bind(socketAddress); + channel.connect(socketAddress); + //非阻塞模式 + channel.configureBlocking(false); + System.out.println(channel.isOpen()); + } +} diff --git a/src/main/java/netty/java/nio/file/ChannelMapBufferTest.java b/src/main/java/netty/java/nio/file/ChannelMapBufferTest.java new file mode 100644 index 0000000..9a88ea4 --- /dev/null +++ b/src/main/java/netty/java/nio/file/ChannelMapBufferTest.java @@ -0,0 +1,35 @@ +package netty.java.nio.file; + +import java.io.IOException; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName ChannelMapBufferTest.java + * @createTime 2020年08月03日 21:42:00 + */ +public class ChannelMapBufferTest { + + public static void main(String[] args) { + new ChannelMapBufferTest().testMapBuffer(); + } + + public void testMapBuffer() { + try (FileChannel open = FileChannel + .open(Paths.get("C:\\Users\\Crazy\\Desktop\\gc.log"), + StandardOpenOption.READ); + FileChannel out = FileChannel. + open(Paths.get("C:\\Users\\Crazy\\Desktop\\1.log"), + StandardOpenOption.CREATE, StandardOpenOption.WRITE, + StandardOpenOption.READ)) { + MappedByteBuffer mappedByteBuffer = open.map(FileChannel.MapMode.READ_ONLY, 0, open.size()); + out.write(mappedByteBuffer); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/netty/java/nio/file/ChannelTest.java b/src/main/java/netty/java/nio/file/ChannelTest.java new file mode 100644 index 0000000..5c68c50 --- /dev/null +++ b/src/main/java/netty/java/nio/file/ChannelTest.java @@ -0,0 +1,31 @@ +package netty.java.nio.file; + +import java.io.IOException; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.OpenOption; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.HashSet; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName ChannelTest.java + * @createTime 2020年06月30日 23:12:00 + */ +public class ChannelTest { + + public static void main(String[] args) throws IOException { + FileChannel fileChannel = FileChannel.open + (Paths.get("/Users/crazy/java_error_in_idea_14558.log"), StandardOpenOption.READ); + HashSet hashSet = new HashSet(); + hashSet.add(StandardOpenOption.WRITE); + FileChannel outChannel = FileChannel.open(Paths.get("Users/crazy/Desktop/poi_brand.sql"), hashSet); + MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, + 0, fileChannel.size()); + /*MappedByteBuffer outBuffer = outChannel.map(FileChannel.MapMode.READ_WRITE,0,fileChannel.size());*/ + mappedByteBuffer.flip(); + outChannel.write(mappedByteBuffer); + } +} diff --git a/src/main/java/netty/java/nio/file/FileChannelTest.java b/src/main/java/netty/java/nio/file/FileChannelTest.java new file mode 100644 index 0000000..6cc6b0c --- /dev/null +++ b/src/main/java/netty/java/nio/file/FileChannelTest.java @@ -0,0 +1,40 @@ +package netty.java.nio.file; + +import java.io.FileNotFoundException; +import java.io.RandomAccessFile; +import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Path; + +/** + * @author crazy + * @title: FileChannelTest + * @projectName JavaCode + * @description: TODO + * @date 2020/6/2419:52 + */ +public class FileChannelTest { + + public static void main(String[] args) throws Exception { + RandomAccessFile randomAccessFile + = new RandomAccessFile("/Users/crazy/Downloads/", "rws"); + FileChannel channel = randomAccessFile.getChannel(); + ByteBuffer buf = ByteBuffer.allocate(48); + ByteBuffer buffer = ByteBuffer.allocate(48); + ByteBuffer[] bufferArray = {buf, buffer}; + long read = channel.read(bufferArray); + while (read != -1) { + System.out.println("Read " + bufferArray); + buf.flip(); + buffer.flip(); + while (buf.hasRemaining()) { + System.out.print((char) buf.get()); + } + buf.clear(); + buffer.clear(); + read = channel.read(bufferArray); + } + randomAccessFile.close(); + } +} diff --git a/src/main/java/netty/java/nio/file/FileChannelTestTwo.java b/src/main/java/netty/java/nio/file/FileChannelTestTwo.java new file mode 100644 index 0000000..fcb8923 --- /dev/null +++ b/src/main/java/netty/java/nio/file/FileChannelTestTwo.java @@ -0,0 +1,25 @@ +package netty.java.nio.file; + +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; + +/** + * @author crazy + * @title: FileChannelTestTwo + * @projectName JavaCode + * @description: 管道的双详情 + * @date 2020/6/3017:36 + */ +public class FileChannelTestTwo { + + public static void main(String[] args) throws Exception { + RandomAccessFile fromFile = new RandomAccessFile("/Users/crazy/Downloads/fromFile.txt", "rw"); + FileChannel fromChannel = fromFile.getChannel(); + RandomAccessFile toFile = new RandomAccessFile("/Users/crazy/Downloads/toFile.txt", "rw"); + FileChannel toChannel = toFile.getChannel(); + long position = 0; + long count = fromChannel.size(); + toChannel.transferFrom(fromChannel, position, count); + + } +} diff --git a/src/main/java/netty/java/nio/selector/EPollClient.java b/src/main/java/netty/java/nio/selector/EPollClient.java new file mode 100644 index 0000000..287a560 --- /dev/null +++ b/src/main/java/netty/java/nio/selector/EPollClient.java @@ -0,0 +1,31 @@ +package netty.java.nio.selector; + +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; + +/** + * @author crazy + * @title: EPollClient + * @projectName JavaCode + * @description: TODO + * @date 2020/7/611:36 + */ +public class EPollClient { + public static void main(String[] args) throws Exception { + SocketChannel socketChannel = SocketChannel.open(); + socketChannel.connect(new InetSocketAddress("127.0.0.1", 8000)); + + ByteBuffer writeBuffer = ByteBuffer.allocate(32); + ByteBuffer readBuffer = ByteBuffer.allocate(32); + writeBuffer.put("hello".getBytes()); + writeBuffer.flip(); + while (true) { + writeBuffer.rewind(); + writeBuffer.flip(); + socketChannel.write(writeBuffer); + readBuffer.clear(); + socketChannel.read(readBuffer); + } + } +} diff --git a/src/main/java/netty/java/nio/selector/EPollServer.java b/src/main/java/netty/java/nio/selector/EPollServer.java new file mode 100644 index 0000000..5378056 --- /dev/null +++ b/src/main/java/netty/java/nio/selector/EPollServer.java @@ -0,0 +1,62 @@ +package netty.java.nio.selector; + +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.util.Iterator; +import java.util.Set; + +/** + * @author crazy + * @title: EpollServer + * @projectName JavaCode + * @description: TODO + * @date 2020/7/611:16 + */ +public class EPollServer { + public static void main(String[] args) throws Exception { + ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); + serverSocketChannel.bind(new InetSocketAddress("localhost", 8000)); + serverSocketChannel.configureBlocking(false); + Selector selector = Selector.open(); + serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); + ByteBuffer readBuff = ByteBuffer.allocate(1024); + ByteBuffer writeBuff = ByteBuffer.allocate(128); + writeBuff.put("received".getBytes()); + writeBuff.flip(); + while (true) { + System.out.println(selector.select()); + int nReady = selector.select(); + Set keySet = selector.selectedKeys(); + Iterator iterator = keySet.iterator(); + while (iterator.hasNext()) { + SelectionKey key = iterator.next(); + iterator.remove(); + if (key.isAcceptable()) { + // 创建新的连接,并且把连接注册到selector上,而且, + // 声明这个channel只对读操作感兴趣。 + SocketChannel socketChannel = serverSocketChannel.accept(); + socketChannel.configureBlocking(false); + socketChannel.register(selector, SelectionKey.OP_READ); + } else if (key.isReadable()) { + System.out.println("read "); + SocketChannel socketChannel = (SocketChannel) key.channel(); + readBuff.clear(); + socketChannel.read(readBuff); + readBuff.flip(); + System.out.println("received : " + new String(readBuff.array())); + key.interestOps(SelectionKey.OP_WRITE); + } else if (key.isWritable()) { + System.out.println("写时间 "); + writeBuff.rewind(); + SocketChannel socketChannel = (SocketChannel) key.channel(); + socketChannel.write(writeBuff); + key.interestOps(SelectionKey.OP_READ); + } + } + } + } +} diff --git a/src/main/java/netty/java/nio/selector/PipeTest.java b/src/main/java/netty/java/nio/selector/PipeTest.java new file mode 100644 index 0000000..2e29337 --- /dev/null +++ b/src/main/java/netty/java/nio/selector/PipeTest.java @@ -0,0 +1,17 @@ +package netty.java.nio.selector; + +import java.nio.channels.Pipe; + +/** + * @author crazy + * @title: PipeTest + * @projectName JavaCode + * @description: TODO + * @date 2020/8/5 15:12 + */ +public class PipeTest { + + public static void main(String[] args) { + Pipe pipe; + } +} diff --git a/src/main/java/netty/java/nio/selector/ServerSocketChannelTest.java b/src/main/java/netty/java/nio/selector/ServerSocketChannelTest.java new file mode 100644 index 0000000..b0227fb --- /dev/null +++ b/src/main/java/netty/java/nio/selector/ServerSocketChannelTest.java @@ -0,0 +1,34 @@ +package netty.java.nio.selector; + +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; + +/** + * @author crazy + * @title: ServerSocketChannelTest + * @projectName JavaCode + * @description: nio Test + * @date 2020/7/921:56 + */ +public class ServerSocketChannelTest { + + private static final String ADDRESS = "localhost"; + + private static final int PORT = 8800; + + + public static void main(String[] args) throws Exception { + ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); + // 绑定一个地址 + serverSocketChannel.bind(new InetSocketAddress(ADDRESS, PORT)); + //设置是否阻塞 + serverSocketChannel.configureBlocking(false); + while (true) { + //创建基本监听 + SocketChannel serverSocket = serverSocketChannel.accept(); + } + + } +} diff --git a/src/main/java/thread/badtaste/LazyInitRace.java b/src/main/java/thread/badtaste/LazyInitRace.java new file mode 100644 index 0000000..d598181 --- /dev/null +++ b/src/main/java/thread/badtaste/LazyInitRace.java @@ -0,0 +1,41 @@ +package thread.badtaste; + +import designpattern.observer.impl.Subject; +import net.jcip.annotations.NotThreadSafe; + +/** + * @author crazy + * @title: LazyInitRace + * @projectName JavaCode + * @description: 惰性初始化, 非线程安全 + * @date 2020/6/1117:16 + */ +@NotThreadSafe +public class LazyInitRace { + + private Subject subject; + + private static Subject subject1 = getSubject(); + + private static Subject getSubject() { + return new Subject(); + } + + public static Subject getInstance() { + return LazyInitRaceOne.subject; + } + + public static class LazyInitRaceOne { + private static Subject subject = new Subject(); + } + + + /*public Subject getInstance() { + if (subject == null) { + subject = new Subject(); + } + return subject; + }*/ + + +} diff --git a/src/main/java/thread/badtaste/UnsafeCountingFactory.java b/src/main/java/thread/badtaste/UnsafeCountingFactory.java new file mode 100644 index 0000000..d889969 --- /dev/null +++ b/src/main/java/thread/badtaste/UnsafeCountingFactory.java @@ -0,0 +1,51 @@ +package thread.badtaste; + +import net.jcip.annotations.NotThreadSafe; + +import javax.servlet.*; +import java.io.IOException; + +/** + * @author crazy + * @title: UnsafeCountingFactory + * @projectName JavaCode + * @description: 非线程安全 + * @date 2020/6/11 17:01 + */ +@NotThreadSafe +public class UnsafeCountingFactory implements Servlet { + + private int count; + + @Override + public void init(ServletConfig servletConfig) throws ServletException { + + } + + @Override + public ServletConfig getServletConfig() { + return null; + } + + @Override + public void service(ServletRequest servletRequest, ServletResponse servletResponse) { + /** + * count++; = count=count+1; + * 这里操作分三步 + * 1.读 + * 2.改 + * 3.写 + */ + count++; + } + + @Override + public String getServletInfo() { + return null; + } + + @Override + public void destroy() { + + } +} diff --git a/src/main/java/thread/package-info.java b/src/main/java/thread/package-info.java new file mode 100644 index 0000000..36f24c5 --- /dev/null +++ b/src/main/java/thread/package-info.java @@ -0,0 +1,8 @@ +/** + * @title: package-info + * @projectName JavaCode + * @description: 🍓 重新学习Thread + * @author crazy + * @date 2020/6/1116:59 + */ +package thread; \ No newline at end of file