This commit is contained in:
hz21056617
2022-04-09 22:30:00 +08:00
parent c68f07d453
commit 2640a2c61c
7 changed files with 198 additions and 1 deletions
@@ -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;
}
}
}
@@ -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;
}
}
@@ -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<Order> 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);
}
}
}
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>JavaBasiceDemo</artifactId>
<groupId>com.wyl.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>file-read</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
@@ -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 "";
}
}
@@ -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();
}
}
}
+7
View File
@@ -11,6 +11,7 @@
<module>dynamic-proxy</module>
<module>elasticsearch</module>
<module>concurrent</module>
<module>file-read</module>
</modules>
<packaging>pom</packaging>
<properties>
@@ -32,5 +33,11 @@
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>