update at 2021-02-20 18:02:28 by ehlxr

This commit is contained in:
ehlxr 2021-02-20 18:02:28 +08:00
parent d32a075c5a
commit 55d8f3b68f
2 changed files with 130 additions and 10 deletions

View File

@ -0,0 +1,90 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.queue;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
/**
* 订单延迟队列
*
* @author ehlxr
* @since 2021-02-20 16:28.
*/
public class Order implements Delayed {
/**
* 延迟时间
*/
private long time;
String name;
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 delayed) {
Order order = (Order) delayed;
long diff = this.time - order.time;
if (diff <= 0) {
return -1;
} else {
return 1;
}
}
public static void main(String[] args) 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(order3);
delayQueue.put(order2);
System.out.println("订单延迟队列开始时间:" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
while (delayQueue.size() != 0) {
/*
* 取队列头部元素是否过期
*/
Order task = delayQueue.take(); // 阻塞
// Order task = delayQueue.poll(); // 非阻塞
// if (task != null) {
System.out.format("订单:{%s}被取消, 取消时间:{%s}\n", task.name, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
// }
// TimeUnit.SECONDS.sleep(1);
}
}
}

View File

@ -24,23 +24,27 @@
package io.github.ehlxr.test; package io.github.ehlxr.test;
import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
/** /**
* Created by ehlxr on 2016/12/15. * Created by ehlxr on 2016/12/15.
*/ */
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
String s0 = "kvill"; // String s0 = "kvill";
String s1 = "kvill"; // String s1 = "kvill";
String s2 = "kvill"; // String s2 = "kvill";
System.out.println(s0 == s1); // System.out.println(s0 == s1);
System.out.println("**********"); // System.out.println("**********");
s1.intern(); // s1.intern();
s2 = s2.intern(); //把常量池中"kvill"的引用赋给s2 // s2 = s2.intern(); //把常量池中"kvill"的引用赋给s2
System.out.println(s0 == s1); // System.out.println(s0 == s1);
System.out.println(s0 == s1.intern()); // System.out.println(s0 == s1.intern());
System.out.println(s0 == s2); // System.out.println(s0 == s2);
// LinkedHashMap // LinkedHashMap
@ -54,5 +58,31 @@ public class Test {
map.put("a", "555"); map.put("a", "555");
System.out.println(map); System.out.println(map);
// The String "[George:Sally:Fred]" may be constructed as follows:
StringJoiner sj = new StringJoiner(":", "[", "]");
sj.add("George").add("Sally").add("Fred");
System.out.println(sj);
// A StringJoiner may be employed to create formatted output from a java.util.stream.Stream using java.util.stream.Collectors.joining(CharSequence). For example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
String commaSeparatedNumbers = numbers.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
System.out.println(commaSeparatedNumbers);
System.out.println("------------" + (-1 >>> Integer.numberOfLeadingZeros(3)));
String s = new String("1");
String t = new String("1");
String s1 = s.intern();
String s2 = "1";
System.out.println(s == s1);
System.out.println(s1 == s2);
System.out.println(s == t); // false
System.out.println(s.intern() == t.intern()); // true
} }
} }