From 1cc4c023aca2f04f898cf657c809a0f03625aad6 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Fri, 19 Jun 2020 15:13:39 +0800 Subject: [PATCH 01/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E6=83=B0=E6=80=A7?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 9 ++++ .../java/thread/badtaste/LazyInitRace.java | 41 +++++++++++++++ .../badtaste/UnsafeCountingFactory.java | 51 +++++++++++++++++++ src/main/java/thread/package-info.java | 8 +++ 4 files changed, 109 insertions(+) create mode 100644 src/main/java/thread/badtaste/LazyInitRace.java create mode 100644 src/main/java/thread/badtaste/UnsafeCountingFactory.java create mode 100644 src/main/java/thread/package-info.java diff --git a/pom.xml b/pom.xml index 234066f..ef2acdc 100644 --- a/pom.xml +++ b/pom.xml @@ -129,5 +129,14 @@ 1.0 + + + javax.servlet + servlet-api + 2.5 + provided + + + \ No newline at end of file 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 From 9b6c0df51bc9df4a23dfa6bb305685dabda0cd1f Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Fri, 19 Jun 2020 20:51:21 +0800 Subject: [PATCH 02/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=BB=83=E6=89=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 4 ++ .../java/netty/http/HttpHelloWorldServer.java | 5 +- src/main/java/netty/java/TimeClient.java | 51 ++++++++++++++++ src/main/java/netty/java/TimeServer.java | 39 ++++++++++++ .../java/netty/java/TimeServerHandler.java | 60 +++++++++++++++++++ 5 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/main/java/netty/java/TimeClient.java create mode 100644 src/main/java/netty/java/TimeServer.java create mode 100644 src/main/java/netty/java/TimeServerHandler.java 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/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..69a9156 --- /dev/null +++ b/src/main/java/netty/java/TimeServer.java @@ -0,0 +1,39 @@ +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; + 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(); + } + + } 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(); + } + } + } + + } +} From 3a56918e33d6e8b11e1469f93deade479c7cc5a5 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Fri, 19 Jun 2020 23:24:14 +0800 Subject: [PATCH 03/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=BB=83=E6=89=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/netty/java/TimeServer.java | 5 +++- .../java/TimeServerHandlerExecutePool.java | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/main/java/netty/java/TimeServerHandlerExecutePool.java diff --git a/src/main/java/netty/java/TimeServer.java b/src/main/java/netty/java/TimeServer.java index 69a9156..a44b465 100644 --- a/src/main/java/netty/java/TimeServer.java +++ b/src/main/java/netty/java/TimeServer.java @@ -14,6 +14,8 @@ 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); @@ -22,7 +24,8 @@ public static void main(String[] args) throws IOException { while (true) { // 监听 socket = server.accept(); - new Thread(new TimeServerHandler(socket)).start(); + /* new Thread(new TimeServerHandler(socket)).start();*/ + serverHandlerExecutePool.execute(new TimeServerHandler(socket)); } } catch (IOException e) { 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); + + } +} From aa375799442e70c418f334e3814121b9ec9b5375 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Wed, 24 Jun 2020 11:52:15 +0800 Subject: [PATCH 04/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0netty=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/netty/java/nio/ByteBufferTest.java | 30 +++++++++++++++++++ src/main/java/netty/java/nio/TimeClient.java | 22 ++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/netty/java/nio/ByteBufferTest.java create mode 100644 src/main/java/netty/java/nio/TimeClient.java 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..a4fa0bd --- /dev/null +++ b/src/main/java/netty/java/nio/ByteBufferTest.java @@ -0,0 +1,30 @@ +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.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/TimeClient.java b/src/main/java/netty/java/nio/TimeClient.java new file mode 100644 index 0000000..d27d696 --- /dev/null +++ b/src/main/java/netty/java/nio/TimeClient.java @@ -0,0 +1,22 @@ +package netty.java.nio; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.channels.SocketChannel; + +/** + * @author crazy + * @title: TimeClient + * @projectName JavaCode + * @description: + * @date 2020/6/2310:50 + */ +public class TimeClient { + + public static void main(String[] args) throws IOException { + /* SocketAddress address = new InetSocketAddress(); + SocketChannel socketChannel = SocketChannel.open();*/ + + } +} From 9151c29a33535f4319ebc2f34f002cd02f7cb364 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Wed, 24 Jun 2020 19:38:18 +0800 Subject: [PATCH 05/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0netty=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/netty/java/nio/BufferTest.java | 35 +++++++++++++++++++ .../java/netty/java/nio/ByteBufferTest.java | 1 + 2 files changed, 36 insertions(+) create mode 100644 src/main/java/netty/java/nio/BufferTest.java 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..5f91acc --- /dev/null +++ b/src/main/java/netty/java/nio/BufferTest.java @@ -0,0 +1,35 @@ +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()); + buffer.put('a'); + buffer.put('b'); + buffer.put('c'); + 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 index a4fa0bd..ac73248 100644 --- a/src/main/java/netty/java/nio/ByteBufferTest.java +++ b/src/main/java/netty/java/nio/ByteBufferTest.java @@ -17,6 +17,7 @@ public static void main(String[] args) { 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); From 405b696ff1e12ce57bc02b2192b16c9801b1c06c Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Tue, 30 Jun 2020 18:07:24 +0800 Subject: [PATCH 06/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0netty=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../netty/java/nio/file/FileChannelTest.java | 40 +++++++++++++++++++ .../java/nio/file/FileChannelTestTwo.java | 26 ++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/main/java/netty/java/nio/file/FileChannelTest.java create mode 100644 src/main/java/netty/java/nio/file/FileChannelTestTwo.java 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..07356ce --- /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/陈卓后端简历.pdf", "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..316a56d --- /dev/null +++ b/src/main/java/netty/java/nio/file/FileChannelTestTwo.java @@ -0,0 +1,26 @@ +package netty.java.nio.file; + +import java.io.FileNotFoundException; +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); + + } +} From 45b763c7678f70ffd47e93e6b3415eeb21f99fb6 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Mon, 6 Jul 2020 11:08:03 +0800 Subject: [PATCH 07/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=A7=A3=E6=9E=90bin?= =?UTF-8?q?log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++++ src/main/java/mysql/TTTT.java | 30 +++++++++++++++++++ .../java/nio/file/FileChannelTestTwo.java | 1 - 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/main/java/mysql/TTTT.java diff --git a/pom.xml b/pom.xml index ef2acdc..c32dc25 100644 --- a/pom.xml +++ b/pom.xml @@ -123,6 +123,12 @@ 4.1.39.Final + + net.jcip jcip-annotations diff --git a/src/main/java/mysql/TTTT.java b/src/main/java/mysql/TTTT.java new file mode 100644 index 0000000..0e5befb --- /dev/null +++ b/src/main/java/mysql/TTTT.java @@ -0,0 +1,30 @@ +package mysql; + +import com.github.shyiko.mysql.binlog.BinaryLogFileReader; +import com.github.shyiko.mysql.binlog.event.Event; +import com.github.shyiko.mysql.binlog.event.deserialization.ChecksumType; +import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer; + +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/java/nio/file/FileChannelTestTwo.java b/src/main/java/netty/java/nio/file/FileChannelTestTwo.java index 316a56d..fcb8923 100644 --- a/src/main/java/netty/java/nio/file/FileChannelTestTwo.java +++ b/src/main/java/netty/java/nio/file/FileChannelTestTwo.java @@ -1,6 +1,5 @@ package netty.java.nio.file; -import java.io.FileNotFoundException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; From 04da4dd81abe54fe96059d229a1c4c932099b0ff Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Mon, 6 Jul 2020 17:54:22 +0800 Subject: [PATCH 08/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=A7=A3=E6=9E=90bin?= =?UTF-8?q?log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../netty/java/nio/selector/EPollClient.java | 31 ++++++++++ .../netty/java/nio/selector/EPollServer.java | 62 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/main/java/netty/java/nio/selector/EPollClient.java create mode 100644 src/main/java/netty/java/nio/selector/EPollServer.java 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); + } + } + } + } +} From 7f9d5d42acfebd6bc6a7ea963c53762963cf22af Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Wed, 8 Jul 2020 16:00:30 +0800 Subject: [PATCH 09/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=BB=83=E6=89=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/evething/SolutionTest.java | 38 +++++++++++++++++++ src/main/java/mysql/TTTT.java | 9 +---- src/main/java/netty/java/nio/TimeClient.java | 23 +++++++---- .../java/netty/java/nio/file/ChannelTest.java | 10 +++++ 4 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/evething/SolutionTest.java create mode 100644 src/main/java/netty/java/nio/file/ChannelTest.java 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/mysql/TTTT.java b/src/main/java/mysql/TTTT.java index 0e5befb..be5603f 100644 --- a/src/main/java/mysql/TTTT.java +++ b/src/main/java/mysql/TTTT.java @@ -1,10 +1,5 @@ package mysql; -import com.github.shyiko.mysql.binlog.BinaryLogFileReader; -import com.github.shyiko.mysql.binlog.event.Event; -import com.github.shyiko.mysql.binlog.event.deserialization.ChecksumType; -import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer; - import java.io.File; import java.io.IOException; @@ -17,7 +12,7 @@ */ public class TTTT { public static void main(String[] args) throws IOException { - String filePath="/Users/crazy/Downloads/mysql_bin.000304"; + /* String filePath="/Users/crazy/Downloads/mysql_bin.000304"; File binlogFile = new File(filePath); EventDeserializer eventDeserializer = new EventDeserializer(); eventDeserializer.setChecksumType(ChecksumType.CRC32); @@ -25,6 +20,6 @@ public static void main(String[] args) throws IOException { for (Event event; (event = reader.readEvent()) != null; ) { System.out.println(event.toString()); } - reader.close(); + reader.close();*/ } } diff --git a/src/main/java/netty/java/nio/TimeClient.java b/src/main/java/netty/java/nio/TimeClient.java index d27d696..1c24db6 100644 --- a/src/main/java/netty/java/nio/TimeClient.java +++ b/src/main/java/netty/java/nio/TimeClient.java @@ -3,20 +3,27 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketAddress; +import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; /** - * @author crazy - * @title: TimeClient - * @projectName JavaCode - * @description: - * @date 2020/6/2310:50 + * @ClassName TimeClient + * @Description Java nio client + * @Author Crazy + * @Date 2020/6/22 22:29 */ public class TimeClient { - public static void main(String[] args) throws IOException { - /* SocketAddress address = new InetSocketAddress(); - SocketChannel socketChannel = SocketChannel.open();*/ + 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/ChannelTest.java b/src/main/java/netty/java/nio/file/ChannelTest.java new file mode 100644 index 0000000..eba4986 --- /dev/null +++ b/src/main/java/netty/java/nio/file/ChannelTest.java @@ -0,0 +1,10 @@ +package netty.java.nio.file; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName ChannelTest.java + * @createTime 2020年06月30日 23:12:00 + */ +public class ChannelTest { +} From a13c3a98b23ec3f9808e1b119547c8cca374928a Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Fri, 17 Jul 2020 22:43:19 +0800 Subject: [PATCH 10/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=BB=83=E6=89=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/jvm/quote/SoftReferenceTest.java | 14 ++++++ src/main/java/com/jvm/quote/package-info.java | 8 ++++ src/main/java/com/thread/demo/Consumer.java | 46 +++++++++++++++++++ src/main/java/com/thread/demo/Producer.java | 40 ++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/main/java/com/jvm/quote/SoftReferenceTest.java create mode 100644 src/main/java/com/jvm/quote/package-info.java create mode 100644 src/main/java/com/thread/demo/Consumer.java create mode 100644 src/main/java/com/thread/demo/Producer.java 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/Consumer.java b/src/main/java/com/thread/demo/Consumer.java new file mode 100644 index 0000000..2b04c7b --- /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 Consumer { + + 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("运行结束"); + } + + + } +} From e7d8a56a06fa82c24d2437529793320a24588e01 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sat, 18 Jul 2020 20:55:35 +0800 Subject: [PATCH 11/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=A7=A3=E6=9E=90bin?= =?UTF-8?q?log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/thread/demo/ThreadStatusTest.java | 37 ++++++++++++++ src/main/java/netty/demo/TimeServer.java | 49 +++++++++++++++++++ .../java/netty/demo/TimeServerHandler.java | 29 +++++++++++ .../nio/selector/ServerSocketChannelTest.java | 34 +++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 src/main/java/com/thread/demo/ThreadStatusTest.java create mode 100644 src/main/java/netty/demo/TimeServer.java create mode 100644 src/main/java/netty/demo/TimeServerHandler.java create mode 100644 src/main/java/netty/java/nio/selector/ServerSocketChannelTest.java 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/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/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(); + } + + } +} From 1379714c2051d3829ba0fbfd4b615a76679a74fa Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sun, 19 Jul 2020 21:47:47 +0800 Subject: [PATCH 12/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=BB=83=E6=89=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/thread/demo/WaitMethodTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/main/java/com/thread/demo/WaitMethodTest.java 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(); + } +} From 46487443918b9d173761fadb5be3f386d86eac2d Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Tue, 21 Jul 2020 10:04:36 +0800 Subject: [PATCH 13/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=A7=A3=E6=9E=90bin?= =?UTF-8?q?log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/BlockingQueueForCondition.java | 68 +++++++++++++++++++ .../java/com/thread/demo/ProducerDemon.java | 42 ++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/main/java/com/thread/demo/BlockingQueueForCondition.java create mode 100644 src/main/java/com/thread/demo/ProducerDemon.java 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/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(); + } +} From 225bcf0cb225a015f0b2dc11e98874818dd22321 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Tue, 21 Jul 2020 23:01:17 +0800 Subject: [PATCH 14/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0wait=20=E5=92=8Cnotif?= =?UTF-8?q?yAll?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/BlockingQueueForWaitAndNotify.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/com/thread/demo/BlockingQueueForWaitAndNotify.java 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; + } +} From 7588fd218d60fccfd2c516347400ed928ae2f5e9 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Mon, 3 Aug 2020 21:37:48 +0800 Subject: [PATCH 15/26] =?UTF-8?q?1.=E5=AF=B9=E5=A4=96=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=95=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unsafe_voliate/UnsafeTestTwo.java | 2 +- src/main/java/netty/java/nio/BufferTest.java | 2 ++ .../java/netty/java/nio/ByteBufferTest1.java | 20 ++++++++++++++ .../netty/java/nio/DirectByteBufferTest.java | 27 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/main/java/netty/java/nio/ByteBufferTest1.java create mode 100644 src/main/java/netty/java/nio/DirectByteBufferTest.java 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/netty/java/nio/BufferTest.java b/src/main/java/netty/java/nio/BufferTest.java index 5f91acc..bf52813 100644 --- a/src/main/java/netty/java/nio/BufferTest.java +++ b/src/main/java/netty/java/nio/BufferTest.java @@ -17,9 +17,11 @@ public static void main(String[] args) { 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()); 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..40ae71b --- /dev/null +++ b/src/main/java/netty/java/nio/ByteBufferTest1.java @@ -0,0 +1,20 @@ +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); + + Class c = java.nio.Bits.class; + } +} 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); + } + } +} From d4d7804948daedeeb6370439b6ebd5ba803d79ec Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Tue, 4 Aug 2020 23:12:36 +0800 Subject: [PATCH 16/26] =?UTF-8?q?1.=E5=AF=B9=E5=A4=96=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=95=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/netty/java/nio/ByteBufferTest1.java | 1 - .../java/nio/file/ChannelMapBufferTest.java | 36 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/main/java/netty/java/nio/file/ChannelMapBufferTest.java diff --git a/src/main/java/netty/java/nio/ByteBufferTest1.java b/src/main/java/netty/java/nio/ByteBufferTest1.java index 40ae71b..0d42742 100644 --- a/src/main/java/netty/java/nio/ByteBufferTest1.java +++ b/src/main/java/netty/java/nio/ByteBufferTest1.java @@ -15,6 +15,5 @@ public static void main(String[] args) throws ClassNotFoundException { //直接缓冲区 直接在内存汇总 ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024); - Class c = java.nio.Bits.class; } } 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..c06907b --- /dev/null +++ b/src/main/java/netty/java/nio/file/ChannelMapBufferTest.java @@ -0,0 +1,36 @@ +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(); + } + } +} From 5692745941a0a63cc0650a722d67a39a84d50760 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Wed, 5 Aug 2020 10:34:26 +0800 Subject: [PATCH 17/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=A7=A3=E6=9E=90bin?= =?UTF-8?q?log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/thread/demo/ThreadJoin.java | 11 ++++++++++ .../java/netty/java/nio/file/ChannelTest.java | 21 +++++++++++++++++++ .../netty/java/nio/file/FileChannelTest.java | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/thread/demo/ThreadJoin.java 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/netty/java/nio/file/ChannelTest.java b/src/main/java/netty/java/nio/file/ChannelTest.java index eba4986..5c68c50 100644 --- a/src/main/java/netty/java/nio/file/ChannelTest.java +++ b/src/main/java/netty/java/nio/file/ChannelTest.java @@ -1,5 +1,13 @@ 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 @@ -7,4 +15,17 @@ * @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 index 07356ce..6cc6b0c 100644 --- a/src/main/java/netty/java/nio/file/FileChannelTest.java +++ b/src/main/java/netty/java/nio/file/FileChannelTest.java @@ -18,7 +18,7 @@ public class FileChannelTest { public static void main(String[] args) throws Exception { RandomAccessFile randomAccessFile - = new RandomAccessFile("/Users/crazy/Downloads/陈卓后端简历.pdf", "rws"); + = new RandomAccessFile("/Users/crazy/Downloads/", "rws"); FileChannel channel = randomAccessFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(48); ByteBuffer buffer = ByteBuffer.allocate(48); From 1e468273231f9ef34262e9833dbb6a08735bcba2 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sat, 8 Aug 2020 20:43:48 +0800 Subject: [PATCH 18/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0netty=20=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nio/file/ChannelMapBufferTest.java | 1 - .../java/netty/java/nio/selector/PipeTest.java | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/main/java/netty/java/nio/selector/PipeTest.java diff --git a/src/main/java/netty/java/nio/file/ChannelMapBufferTest.java b/src/main/java/netty/java/nio/file/ChannelMapBufferTest.java index c06907b..9a88ea4 100644 --- a/src/main/java/netty/java/nio/file/ChannelMapBufferTest.java +++ b/src/main/java/netty/java/nio/file/ChannelMapBufferTest.java @@ -16,7 +16,6 @@ public class ChannelMapBufferTest { public static void main(String[] args) { new ChannelMapBufferTest().testMapBuffer(); - } public void testMapBuffer() { 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; + } +} From d3f2c19faa7b9683229e8e2e057c8ab4bd492015 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sun, 9 Aug 2020 15:37:37 +0800 Subject: [PATCH 19/26] =?UTF-8?q?1.=E5=AF=B9=E5=A4=96=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=95=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/algorithm/Solution.java | 83 +++++++++++++++++++ src/main/java/algorithm/package-info.java | 9 ++ src/main/java/netty/demo/AutoBindPort.java | 47 +++++++++++ .../netty/java/nio/selector/PipeTest.java | 15 ++++ 4 files changed, 154 insertions(+) create mode 100644 src/main/java/algorithm/Solution.java create mode 100644 src/main/java/algorithm/package-info.java create mode 100644 src/main/java/netty/demo/AutoBindPort.java create mode 100644 src/main/java/netty/java/nio/selector/PipeTest.java diff --git a/src/main/java/algorithm/Solution.java b/src/main/java/algorithm/Solution.java new file mode 100644 index 0000000..93476f5 --- /dev/null +++ b/src/main/java/algorithm/Solution.java @@ -0,0 +1,83 @@ +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 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/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/netty/demo/AutoBindPort.java b/src/main/java/netty/demo/AutoBindPort.java new file mode 100644 index 0000000..f4ec684 --- /dev/null +++ b/src/main/java/netty/demo/AutoBindPort.java @@ -0,0 +1,47 @@ +package netty.demo; + +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 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) { + } + }); + autoBindPort(serverBootstrap,1); + } + + 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/java/nio/selector/PipeTest.java b/src/main/java/netty/java/nio/selector/PipeTest.java new file mode 100644 index 0000000..f9ff473 --- /dev/null +++ b/src/main/java/netty/java/nio/selector/PipeTest.java @@ -0,0 +1,15 @@ +package netty.java.nio.selector; + +/** + * @author Hikari + * @version 1.0.0 + * @ClassName PipeTest.java + * @createTime 2020年08月06日 15:21:00 + */ +public class PipeTest { + + public static void main(String[] args) { + + + } +} From d19939572973e98624344b15e96bd3883785209b Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sun, 9 Aug 2020 17:56:07 +0800 Subject: [PATCH 20/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0netty=20=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/algorithm/Solution.java | 84 +++++++++++++------ .../netty/demo/{ => im}/AutoBindPort.java | 6 +- src/main/java/netty/demo/im/NettyClient.java | 40 +++++++++ .../netty/demo/im/handler/ClientHandler.java | 39 +++++++++ .../netty/demo/im/handler/ServerHandler.java | 35 ++++++++ 5 files changed, 178 insertions(+), 26 deletions(-) rename src/main/java/netty/demo/{ => im}/AutoBindPort.java (90%) create mode 100644 src/main/java/netty/demo/im/NettyClient.java create mode 100644 src/main/java/netty/demo/im/handler/ClientHandler.java create mode 100644 src/main/java/netty/demo/im/handler/ServerHandler.java diff --git a/src/main/java/algorithm/Solution.java b/src/main/java/algorithm/Solution.java index 93476f5..9c0919b 100644 --- a/src/main/java/algorithm/Solution.java +++ b/src/main/java/algorithm/Solution.java @@ -28,38 +28,74 @@ public class Solution { private final static int MIN_SIZE = 3; + public static void main(String[] args) { + new Solution().restoreIpAddresses("25525511135"); + } 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()); + 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 res; + 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; diff --git a/src/main/java/netty/demo/AutoBindPort.java b/src/main/java/netty/demo/im/AutoBindPort.java similarity index 90% rename from src/main/java/netty/demo/AutoBindPort.java rename to src/main/java/netty/demo/im/AutoBindPort.java index f4ec684..fc81932 100644 --- a/src/main/java/netty/demo/AutoBindPort.java +++ b/src/main/java/netty/demo/im/AutoBindPort.java @@ -1,4 +1,4 @@ -package netty.demo; +package netty.demo.im; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelDuplexHandler; @@ -9,6 +9,7 @@ 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; @@ -29,9 +30,10 @@ public static void main(String[] args) { .childHandler(new ChannelInitializer() { @Override protected void initChannel(NioSocketChannel ch) { + ch.pipeline().addLast(new ServerHandler()); } }); - autoBindPort(serverBootstrap,1); + autoBindPort(serverBootstrap, 8080); } public static void autoBindPort(final ServerBootstrap serverBootstrap, int port) { 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); + } +} From a8180e613b5e4fa433ff5cb552322fe4892647d3 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Mon, 24 Aug 2020 22:15:38 +0800 Subject: [PATCH 21/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E6=8B=85=E4=BF=9D?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E7=9A=84=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jvm/gc/TestHandlePromotionFailure.java | 28 +++++++++++++++ .../com/jvm/gc/TestHandlePromotionFailure.md | 34 +++++++++++++++++++ src/main/java/com/jvm/gc/package-info.java | 8 +++++ 3 files changed, 70 insertions(+) create mode 100644 src/main/java/com/jvm/gc/TestHandlePromotionFailure.java create mode 100644 src/main/java/com/jvm/gc/TestHandlePromotionFailure.md create mode 100644 src/main/java/com/jvm/gc/package-info.java 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..42ea54a --- /dev/null +++ b/src/main/java/com/jvm/gc/TestHandlePromotionFailure.md @@ -0,0 +1,34 @@ +## 分代担保学习 + + +~~~~ 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 From 2887a0c90643b62e6bb42c72a5b0a1a8e55e145b Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sun, 6 Sep 2020 19:18:33 +0800 Subject: [PATCH 22/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E6=8B=85=E4=BF=9D?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E7=9A=84=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../basics/collection/map/HashMapTest.java | 5 ++ .../testsynchronized/TestSynchronized.java | 32 +++++++++++ .../testsynchronized/TestSynchronizedOne.java | 18 ++++++ .../testsynchronized/package-info.java | 8 +++ .../com/concurrent/thread/wait/AddThread.java | 14 +++++ .../com/concurrent/thread/wait/CountTest.java | 51 +++++++++++++++++ .../concurrent/thread/wait/CountTestTwo.java | 57 +++++++++++++++++++ .../com/concurrent/thread/wait/TestWait.java | 18 ++++++ .../com/jvm/gc/TestHandlePromotionFailure.md | 1 - 9 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/concurrent/testsynchronized/TestSynchronized.java create mode 100644 src/main/java/com/concurrent/testsynchronized/TestSynchronizedOne.java create mode 100644 src/main/java/com/concurrent/testsynchronized/package-info.java create mode 100644 src/main/java/com/concurrent/thread/wait/AddThread.java create mode 100644 src/main/java/com/concurrent/thread/wait/CountTest.java create mode 100644 src/main/java/com/concurrent/thread/wait/CountTestTwo.java create mode 100644 src/main/java/com/concurrent/thread/wait/TestWait.java 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/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/jvm/gc/TestHandlePromotionFailure.md b/src/main/java/com/jvm/gc/TestHandlePromotionFailure.md index 42ea54a..d14bad9 100644 --- a/src/main/java/com/jvm/gc/TestHandlePromotionFailure.md +++ b/src/main/java/com/jvm/gc/TestHandlePromotionFailure.md @@ -1,6 +1,5 @@ ## 分代担保学习 - ~~~~ java private static final int DEF_MB = 1024 * 1024; From 9bb77c2756bd20434aeb6e5db2484984654234d6 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sat, 12 Dec 2020 16:00:55 +0800 Subject: [PATCH 23/26] 1 --- src/main/java/com/jdk8/base/Apple.java | 30 ++++++++++ .../java/com/jdk8/base/FunctionApple.java | 58 +++++++++++++++++++ src/main/java/com/thread/demo/Consumer.java | 2 +- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/jdk8/base/Apple.java create mode 100644 src/main/java/com/jdk8/base/FunctionApple.java 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/thread/demo/Consumer.java b/src/main/java/com/thread/demo/Consumer.java index 2b04c7b..84df657 100644 --- a/src/main/java/com/thread/demo/Consumer.java +++ b/src/main/java/com/thread/demo/Consumer.java @@ -10,7 +10,7 @@ * @ClassName Consumer.java * @createTime 2020年07月17日 22:10:00 */ -public class Consumer { +public class ConsumerConsumer { ArrayBlockingQueue storage; From 4d444373fdf7f8d05bb4bb3747d34f4ebf81b96f Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sat, 15 May 2021 11:43:08 +0800 Subject: [PATCH 24/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0hash=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 23 ++++-- .../java/algorithm/hash/ConsistenceHash.java | 80 +++++++++++++++++++ 2 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 src/main/java/algorithm/hash/ConsistenceHash.java diff --git a/pom.xml b/pom.xml index c32dc25..3dee2e8 100644 --- a/pom.xml +++ b/pom.xml @@ -123,11 +123,11 @@ 4.1.39.Final - + net.jcip @@ -143,6 +143,19 @@ 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/hash/ConsistenceHash.java b/src/main/java/algorithm/hash/ConsistenceHash.java new file mode 100644 index 0000000..ae3fed1 --- /dev/null +++ b/src/main/java/algorithm/hash/ConsistenceHash.java @@ -0,0 +1,80 @@ +package algorithm.hash; + +import java.util.List; +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); + } + } + } +} From ceda29ff36534de4cec23bda029048e97c70d569 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sat, 15 May 2021 11:43:56 +0800 Subject: [PATCH 25/26] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0hash=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../concurrent/map/ConcurrentHashMapTest.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/com/concurrent/map/ConcurrentHashMapTest.java 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("测试", "陈胖子"); + + } +} From 455b19cad0069f548a74c5f463cb4f2330ac27c6 Mon Sep 17 00:00:00 2001 From: chenzhuo Date: Sun, 16 May 2021 20:15:09 +0800 Subject: [PATCH 26/26] =?UTF-8?q?1.=E5=88=A0=E9=99=A4=E6=B2=A1=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/algorithm/hash/ConsistenceHash.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/algorithm/hash/ConsistenceHash.java b/src/main/java/algorithm/hash/ConsistenceHash.java index ae3fed1..8a881be 100644 --- a/src/main/java/algorithm/hash/ConsistenceHash.java +++ b/src/main/java/algorithm/hash/ConsistenceHash.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Map; +import java.util.Map; import java.util.SortedMap; import java.util.TreeMap;