From 2640a2c61c610d81e93e7d29ee8b85bb339e31a8 Mon Sep 17 00:00:00 2001 From: hz21056617 <1IB76X4g7T> Date: Sat, 9 Apr 2022 22:30:00 +0800 Subject: [PATCH] 1 --- .../src/main/java/concurrent/queue/Order.java | 45 +++++++++++++++++ .../java/concurrent/hashmap/HashMapTest.java | 5 +- .../java/concurrent/queue/DelayQueueTest.java | 37 ++++++++++++++ file-read/pom.xml | 19 +++++++ .../com/wyl/file/sequence/Sequential.java | 49 +++++++++++++++++++ .../src/test/java/com/wyl/file/FileTest.java | 37 ++++++++++++++ pom.xml | 7 +++ 7 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 concurrent/src/main/java/concurrent/queue/Order.java create mode 100644 concurrent/src/test/java/concurrent/queue/DelayQueueTest.java create mode 100644 file-read/pom.xml create mode 100644 file-read/src/main/java/com/wyl/file/sequence/Sequential.java create mode 100644 file-read/src/test/java/com/wyl/file/FileTest.java diff --git a/concurrent/src/main/java/concurrent/queue/Order.java b/concurrent/src/main/java/concurrent/queue/Order.java new file mode 100644 index 0000000..4e6dd76 --- /dev/null +++ b/concurrent/src/main/java/concurrent/queue/Order.java @@ -0,0 +1,45 @@ +package concurrent.queue; + +import lombok.Data; + +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +/** + * 订单信息 + * @ClassName: Order + * @Date: 2022/4/8 13:57 + * @author wangyl + * @version V1.0 + */ +@Data +public class Order implements Delayed { + /** + * 延迟时间 + */ + private long time; + private String name; + private String timestamp = System.currentTimeMillis() + ""; + + + public Order(String name, long time, TimeUnit unit) { + this.name = name; + this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0); + } + + @Override + public long getDelay(TimeUnit unit) { + return time - System.currentTimeMillis(); + } + + @Override + public int compareTo(Delayed o) { + Order Order = (Order) o; + long diff = this.time - Order.time; + if (diff <= 0) { + return -1; + } else { + return 1; + } + } +} \ No newline at end of file diff --git a/concurrent/src/test/java/concurrent/hashmap/HashMapTest.java b/concurrent/src/test/java/concurrent/hashmap/HashMapTest.java index 831cd56..d3729a0 100644 --- a/concurrent/src/test/java/concurrent/hashmap/HashMapTest.java +++ b/concurrent/src/test/java/concurrent/hashmap/HashMapTest.java @@ -3,6 +3,10 @@ package concurrent.hashmap; import org.junit.jupiter.api.Test; +import java.util.concurrent.DelayQueue; +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + public class HashMapTest { static final int MAXIMUM_CAPACITY = 1 << 30; @@ -21,5 +25,4 @@ public class HashMapTest { int t = (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; return t; } - } diff --git a/concurrent/src/test/java/concurrent/queue/DelayQueueTest.java b/concurrent/src/test/java/concurrent/queue/DelayQueueTest.java new file mode 100644 index 0000000..d7be6e7 --- /dev/null +++ b/concurrent/src/test/java/concurrent/queue/DelayQueueTest.java @@ -0,0 +1,37 @@ +package concurrent.queue; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.concurrent.DelayQueue; +import java.util.concurrent.TimeUnit; + +public class DelayQueueTest { + @Test + public void delayQueueTest() throws InterruptedException { + Order Order1 = new Order("Order1", 5, TimeUnit.SECONDS); + Order Order2 = new Order("Order2", 10, TimeUnit.SECONDS); + Order Order3 = new Order("Order3", 15, TimeUnit.SECONDS); + DelayQueue delayQueue = new DelayQueue<>(); + delayQueue.put(Order1); + delayQueue.put(Order2); + delayQueue.put(Order3); + + System.out.println("订单延迟队列开始时间:" + LocalDateTime.now() + .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + while (delayQueue.size() != 0) { + /** + * 取队列头部元素是否过期 + */ +// Order task = delayQueue.poll(); + Order task = delayQueue.take(); + delayQueue.peek(); + if (task != null) { + System.out.format("订单:{%s}被取消, 放入时间{%s},取消时间:{%s}\n", task.getName(), task.getTimestamp(), LocalDateTime.now() + .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + } + Thread.sleep(1000); + } + } +} diff --git a/file-read/pom.xml b/file-read/pom.xml new file mode 100644 index 0000000..3155bc0 --- /dev/null +++ b/file-read/pom.xml @@ -0,0 +1,19 @@ + + + + JavaBasiceDemo + com.wyl.example + 1.0-SNAPSHOT + + 4.0.0 + + file-read + + + 8 + 8 + + + \ No newline at end of file diff --git a/file-read/src/main/java/com/wyl/file/sequence/Sequential.java b/file-read/src/main/java/com/wyl/file/sequence/Sequential.java new file mode 100644 index 0000000..955dc1e --- /dev/null +++ b/file-read/src/main/java/com/wyl/file/sequence/Sequential.java @@ -0,0 +1,49 @@ +package com.wyl.file.sequence; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; + +public class Sequential { + public static int fileWrite(String filePath, String content, int index) { + File file = new File(filePath); + RandomAccessFile randomAccessTargetFile; + MappedByteBuffer map; + try { + randomAccessTargetFile = new RandomAccessFile(file, "rw"); + FileChannel targetFileChannel = randomAccessTargetFile.getChannel(); + map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, (long) 1024 * 1024 * 1024); + map.position(index); + map.put(content.getBytes()); + return map.position(); + } + catch (IOException e) { + e.printStackTrace(); + } + finally { + } + return 0; + } + + public static String fileRead(String filePath, long index) { + File file = new File(filePath); + RandomAccessFile randomAccessTargetFile; + MappedByteBuffer map; + try { + randomAccessTargetFile = new RandomAccessFile(file, "rw"); + FileChannel targetFileChannel = randomAccessTargetFile.getChannel(); + map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, index); + byte[] byteArr = new byte[10 * 1024]; + map.get(byteArr, 0, (int) index); + return new String(byteArr); + } + catch (IOException e) { + e.printStackTrace(); + } + finally { + } + return ""; + } +} \ No newline at end of file diff --git a/file-read/src/test/java/com/wyl/file/FileTest.java b/file-read/src/test/java/com/wyl/file/FileTest.java new file mode 100644 index 0000000..d5c653d --- /dev/null +++ b/file-read/src/test/java/com/wyl/file/FileTest.java @@ -0,0 +1,37 @@ +package com.wyl.file; + +import com.wyl.file.sequence.Sequential; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.RandomAccessFile; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.util.concurrent.atomic.AtomicLong; + +public class FileTest { + AtomicLong pos = new AtomicLong(0L); + + @Test + public void SequentialWriteFile() { + Integer index = 0; + + + File file = new File("./wyl.log"); + MappedByteBuffer map; + try (RandomAccessFile randomAccessTargetFile = new RandomAccessFile(file, "rw")) { + FileChannel targetFileChannel = randomAccessTargetFile.getChannel(); + map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, pos.get(), (long) 1024 * 1024 * 1024); + if (map.isLoaded()) { + + } + map.position(index); + map.put("123\n".getBytes()); + final int position = map.position(); + } + catch (Exception e) { + e.printStackTrace(); + } + + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 4ac5b20..9ec444a 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,7 @@ dynamic-proxy elasticsearch concurrent + file-read pom @@ -32,5 +33,11 @@ lombok 1.18.20 + + org.junit.jupiter + junit-jupiter-api + 5.8.2 + test + \ No newline at end of file