文档更新:字符串、字节和void数据类型信息已添加到NumPy文档的常规介绍部分,与现有的数值类型信息并列。
错误修复:用户创建操作现在将正确分配UUID,由于SQL查询中参数顺序的问题已得到解决,确保用户可以成功创建。 功能优化:EntityClass的重新加载机制将遵循`flush`参数的指示,防止在刷新设置为false时刷新新实体。
This commit is contained in:
parent
b8cbf111fe
commit
33dffb6b07
10
src/main/java/cn/whaifree/interview/qz/huachen/P1.java
Normal file
10
src/main/java/cn/whaifree/interview/qz/huachen/P1.java
Normal file
@ -0,0 +1,10 @@
|
||||
package cn.whaifree.interview.qz.huachen;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/19 18:55
|
||||
* @注释
|
||||
*/
|
||||
public class P1 {
|
||||
}
|
137
src/main/java/cn/whaifree/interview/qz/zzyc/p1.java
Normal file
137
src/main/java/cn/whaifree/interview/qz/zzyc/p1.java
Normal file
@ -0,0 +1,137 @@
|
||||
package cn.whaifree.interview.qz.zzyc;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/20 0:35
|
||||
* @注释
|
||||
*/
|
||||
public class p1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Integer> list = Arrays.asList(0, 11, 12, 13,1);
|
||||
System.out.println(new p1().isStraight(new ArrayList<>(list)));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* <img src="https://pic.leetcode-cn.com/1599885716-MGMODX-Picture1.png"/>
|
||||
* 1. 排序
|
||||
*
|
||||
*
|
||||
* 给定的5张牌是否是顺子
|
||||
* @param nums int整型ArrayList 扑克牌对应的数字集合
|
||||
* @return bool布尔型
|
||||
*/
|
||||
public boolean isStraight (ArrayList<Integer> nums) {
|
||||
// write code here
|
||||
|
||||
Collections.sort(nums);
|
||||
|
||||
|
||||
if (inEdge(nums)) { // 如果出现1 10 11 12 13 这种情况
|
||||
return true;
|
||||
}
|
||||
|
||||
int joker = -1;
|
||||
for (int i = 0; i < nums.size() - 1; i++) {
|
||||
if (nums.get(i) == 0) {
|
||||
joker++;
|
||||
} else if (nums.get(i).equals(nums.get(i + 1))) {
|
||||
return false; // 排序后出现重复,不可能是顺子
|
||||
}
|
||||
}
|
||||
|
||||
// 比如 5 4 3 2 1 最大的减去最小在范围内
|
||||
return nums.get(4) - nums.get(joker + 1) < 5;
|
||||
}
|
||||
|
||||
|
||||
static ArrayList<ArrayList<Integer>> o = new ArrayList<>();
|
||||
static {
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 11, 12, 13, 1)));
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 10, 12, 13, 1)));
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 10, 11, 13, 1)));
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 10, 11, 12, 1)));
|
||||
o.add(new ArrayList<>(Arrays.asList(0, 10, 11, 12, 13)));
|
||||
o.add(new ArrayList<>(Arrays.asList(1, 10, 11, 12, 13)));
|
||||
}
|
||||
public static boolean inEdge(ArrayList<Integer> list) {
|
||||
for (ArrayList<Integer> integers : o) {
|
||||
if (integers.equals(list)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public boolean checkDynasty(int[] places) {
|
||||
Set<Integer> repeat = new HashSet<>();
|
||||
int max = 0, min = 14;
|
||||
for(int place : places) {
|
||||
if(place == 0) continue; // 跳过未知朝代
|
||||
max = Math.max(max, place); // 最大编号朝代
|
||||
min = Math.min(min, place); // 最小编号朝代
|
||||
if(repeat.contains(place)) {
|
||||
return false; // 若有重复,提前返回 false
|
||||
}
|
||||
repeat.add(place); // 添加此朝代至 Set
|
||||
}
|
||||
return max - min < 5; // 最大编号朝代 - 最小编号朝代 < 5 则连续
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class P{
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Integer> list = Arrays.asList(1, 4, 6, 6, 11);
|
||||
|
||||
System.out.println(prize(new ArrayList<>(list), 12));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
|
||||
*
|
||||
*
|
||||
* @param res int整型ArrayList
|
||||
* @param target int整型
|
||||
* @return int整型
|
||||
*/
|
||||
public static int prize (ArrayList<Integer> res, int target) {
|
||||
// write code here
|
||||
|
||||
// for (int i = 0; i < res.size(); i++) {
|
||||
// if (res.get(i) >= target) {
|
||||
// return i;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
int left = 0;
|
||||
int right = res.size() - 1;
|
||||
while (left <= right) {
|
||||
int mid = (left + right) / 2;
|
||||
if (res.get(mid) > target) {
|
||||
right = mid - 1;
|
||||
} else if (res.get(mid) == target) {
|
||||
return mid + 1;
|
||||
}else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
if (left < res.size() && res.get(left) > target) {
|
||||
return left + 1;
|
||||
}
|
||||
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
68
src/main/java/cn/whaifree/leetCode/Array/LeetCode167.java
Normal file
68
src/main/java/cn/whaifree/leetCode/Array/LeetCode167.java
Normal file
@ -0,0 +1,68 @@
|
||||
package cn.whaifree.leetCode.Array;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/19 23:02
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode167 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Solution1 solution = new Solution1();
|
||||
int[] ints = solution.twoSum(new int[]{2,7,11,15}, 9);
|
||||
System.out.println(ints[0] + " " + ints[1]);
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
int tar = target - numbers[i];
|
||||
int j = binarySearch(numbers, tar);
|
||||
if (j != -1 && i != j) {
|
||||
return i < j ? new int[]{i + 1, j + 1} : new int[]{j + 1, i + 1};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int binarySearch(int[] nums, int target) {
|
||||
int left = 0;
|
||||
int right = nums.length - 1;
|
||||
while (left <= right) {
|
||||
int mid = (left + right) / 2;
|
||||
if (nums[mid] == target) {
|
||||
return mid;
|
||||
} else if (nums[mid] < target) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution1 {
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
int left = 0;
|
||||
int right = numbers.length - 1;
|
||||
while (left < right) {
|
||||
int sum = numbers[left] + numbers[right];
|
||||
if (sum == target) {
|
||||
return new int[]{left + 1, right + 1};
|
||||
} else if (sum < target) {
|
||||
left++;
|
||||
}else {
|
||||
right--;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package cn.whaifree.leetCode.Dynamic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
@ -10,12 +12,19 @@ public class LeetCode139 {
|
||||
|
||||
class Solution {
|
||||
|
||||
// public boolean wordBreak(String s, List<String> wordDict) {
|
||||
// // wordDict放入s的背包
|
||||
// char[] chars = s.toCharArray();
|
||||
//
|
||||
//
|
||||
// }
|
||||
/**
|
||||
*
|
||||
* @param s
|
||||
* @param wordDict
|
||||
* @return
|
||||
*/
|
||||
public boolean wordBreak(String s, List<String> wordDict) {
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
45
src/main/java/cn/whaifree/leetCode/Hash/LeetCode128.java
Normal file
45
src/main/java/cn/whaifree/leetCode/Hash/LeetCode128.java
Normal file
@ -0,0 +1,45 @@
|
||||
package cn.whaifree.leetCode.Hash;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/21 13:53
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode128 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
int[] nums = {4, 1, 3, 2};
|
||||
System.out.println(longestConsecutive(nums));
|
||||
}
|
||||
|
||||
public int longestConsecutive(int[] nums) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
for (int num : nums) {
|
||||
set.add(num);
|
||||
}
|
||||
|
||||
|
||||
int longest = 0;
|
||||
// ON
|
||||
for (int num : set) {
|
||||
|
||||
// 这里是关键,说明这个元素i是某一组排序的开头,不存在i-1的元素
|
||||
if (!set.contains(num-1)){
|
||||
int len = 1;
|
||||
while (set.contains(++num)) {
|
||||
len++;
|
||||
}
|
||||
longest = Math.max(longest, len);
|
||||
}
|
||||
|
||||
}
|
||||
return longest;
|
||||
}
|
||||
}
|
68
src/main/java/cn/whaifree/leetCode/Hash/LeetCode205.java
Normal file
68
src/main/java/cn/whaifree/leetCode/Hash/LeetCode205.java
Normal file
@ -0,0 +1,68 @@
|
||||
package cn.whaifree.leetCode.Hash;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/21 13:32
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode205 {
|
||||
@Test
|
||||
public void test() {
|
||||
String s = "badc";
|
||||
String t = "baba";
|
||||
System.out.println(new Solution().isIsomorphic(s, t));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public boolean isIsomorphic(String s, String t) {
|
||||
return is(s, t) && is(t, s);
|
||||
}
|
||||
|
||||
public boolean is(String s, String t) {
|
||||
HashMap<Character, Character> map = new HashMap<>();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char key = s.charAt(i);
|
||||
if (!map.containsKey(key)) {
|
||||
map.put(key, t.charAt(i));
|
||||
} else if (map.get(key) != t.charAt(i)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
class Solution1 {
|
||||
/**
|
||||
* 每个字符第一次出现的位置都一样,就能保证同构成
|
||||
* abc c和d第一次出现的 位置为1和2,不一样会false
|
||||
* cdd
|
||||
*
|
||||
* indexOf时间复杂度oN
|
||||
* @param s
|
||||
* @param t
|
||||
* @return
|
||||
*/
|
||||
public boolean isIsomorphic(String s, String t) {
|
||||
if (s.length() != t.length()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char s1 = s.charAt(i);
|
||||
char t1 = t.charAt(i);
|
||||
if (s.indexOf(s1) != t.indexOf(t1)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package cn.whaifree.leetCode.LinkedList;
|
||||
|
||||
import cn.whaifree.leetCode.model.ListNode;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/21 14:11
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode92 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
new Solution().reverseBetween(ListNode.listNodeFromArray(new int[]{1, 2, 3, 4, 5}
|
||||
), 1, 5).printList();
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public ListNode reverseBetween(ListNode head, int left, int right) {
|
||||
if (left == right) {
|
||||
return head;
|
||||
}
|
||||
ListNode pre = new ListNode(-1, head);
|
||||
ListNode tmpLeft = pre;
|
||||
ListNode tmpRight = pre;
|
||||
for (int i = 0; i < left - 1; i++) {
|
||||
tmpLeft = tmpLeft.next;
|
||||
tmpRight = tmpRight.next;
|
||||
}
|
||||
for (int i = 0; i < right - left + 2; i++) {
|
||||
tmpRight = tmpRight.next;
|
||||
}
|
||||
|
||||
ListNode tmpNext = tmpLeft.next;
|
||||
tmpLeft.next = reverse(null, tmpNext, right - left + 1);
|
||||
|
||||
tmpNext.next = tmpRight;
|
||||
|
||||
return pre.next;
|
||||
}
|
||||
|
||||
|
||||
public ListNode reverse(ListNode pre, ListNode after, int k) {
|
||||
if (k == 0) {
|
||||
return pre;
|
||||
}
|
||||
if (after == null) {
|
||||
return pre;
|
||||
}
|
||||
ListNode tmp = after.next;
|
||||
after.next = pre;
|
||||
return reverse(after, tmp, k - 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
ListNode pre = ListNode.listNodeFromArray(new int[]{1, 2, 3});
|
||||
new Solution().reverse(null, pre, 3).printList();
|
||||
}
|
||||
}
|
82
src/main/java/cn/whaifree/leetCode/String/LeetCode468.java
Normal file
82
src/main/java/cn/whaifree/leetCode/String/LeetCode468.java
Normal file
@ -0,0 +1,82 @@
|
||||
package cn.whaifree.leetCode.String;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/19 22:08
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode468 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
System.out.println(5 & 0xFF);
|
||||
|
||||
Solution solution = new Solution();
|
||||
System.out.println(solution.validIPAddress("20EE:Fb8:85a3:0:0:8A2E:0370:7334:12"));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public String validIPAddress(String queryIP) {
|
||||
boolean ipv6 = queryIP.contains(":");
|
||||
if (ipv6 && isIpv6(queryIP)) {
|
||||
return "IPv6";
|
||||
} else if (!ipv6 && isIpv4(queryIP)){
|
||||
return "IPv4";
|
||||
}
|
||||
return "Neither";
|
||||
}
|
||||
|
||||
|
||||
public boolean isIpv6(String s) {
|
||||
String[] split = s.split(":", -1);
|
||||
if (split.length != 8) {
|
||||
return false;
|
||||
}
|
||||
for (String item : split) {
|
||||
if (item.length() > 4 || item.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
int i = Integer.parseInt(item, 16);// 16进制
|
||||
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isIpv4(String s) {
|
||||
String[] split = s.split("\\.");
|
||||
if (split.length != 4) {
|
||||
return false;
|
||||
}
|
||||
for (String item : split) {
|
||||
if (item.length() > 3 || item.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
int i = Integer.parseInt(item);
|
||||
if (i == 0 && item.length() != 1) {
|
||||
return false;
|
||||
}
|
||||
if (item.startsWith("0") && i != 0) { // 00.0.0.0.
|
||||
return false;
|
||||
}
|
||||
if (i < 0 || i > 255) {
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
59
src/main/java/cn/whaifree/test/StampedLockDemo.java
Normal file
59
src/main/java/cn/whaifree/test/StampedLockDemo.java
Normal file
@ -0,0 +1,59 @@
|
||||
package cn.whaifree.test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/21 16:24
|
||||
* @注释
|
||||
*/
|
||||
import java.util.concurrent.locks.StampedLock;
|
||||
|
||||
public class StampedLockDemo {
|
||||
private int count = 0;
|
||||
private final StampedLock lock = new StampedLock();
|
||||
|
||||
public void increment() {
|
||||
long stamp = lock.writeLock(); // 获取写锁
|
||||
try {
|
||||
count++;
|
||||
} finally {
|
||||
lock.unlockWrite(stamp); // 释放写锁
|
||||
}
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
long stamp = lock.tryOptimisticRead(); // 获取乐观读锁
|
||||
int c = count;
|
||||
if (!lock.validate(stamp)) { // 检查乐观读锁是否有效, 如果乐观锁无效,使用悲观锁
|
||||
stamp = lock.readLock(); // 获取悲观读锁
|
||||
try {
|
||||
c = count;
|
||||
} finally {
|
||||
lock.unlockRead(stamp); // 释放悲观读锁
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
StampedLockDemo demo = new StampedLockDemo();
|
||||
Thread writer = new Thread(() -> {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
demo.increment();
|
||||
}
|
||||
});
|
||||
Thread reader = new Thread(() -> {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
System.out.println(demo.getCount());
|
||||
}
|
||||
});
|
||||
writer.start();
|
||||
reader.start();
|
||||
try {
|
||||
writer.join();
|
||||
reader.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -5,9 +5,7 @@ import org.junit.Test;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@ -135,6 +133,7 @@ public class TestCacheThreadPool {
|
||||
private String message = "等待消息";
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
MultiConditionDemo demo = new MultiConditionDemo();
|
||||
new Thread(() -> demo.producer()).start();
|
||||
new Thread(() -> demo.consumer()).start();
|
||||
|
@ -0,0 +1,63 @@
|
||||
package cn.whaifree.test.mulThread;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicStampedReference;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/21 21:08
|
||||
* @注释
|
||||
*/
|
||||
public class AtomicStampedReferenceDemo {
|
||||
|
||||
static class User{
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public User(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
User user = new User("张三", 18);
|
||||
AtomicStampedReference<User> atomicStampedReference = new AtomicStampedReference<>(user, 1);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
User newUser = new User("李四", 20);
|
||||
boolean result = update(atomicStampedReference, newUser);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static boolean update(AtomicStampedReference<User> atomicStampedReference, User newUser) {
|
||||
User oldUser = atomicStampedReference.getReference();
|
||||
int stamp = atomicStampedReference.getStamp();
|
||||
|
||||
while (!atomicStampedReference.compareAndSet(oldUser, newUser, stamp, stamp + 1)) {
|
||||
// 获取失败,更新User和stamp
|
||||
oldUser = atomicStampedReference.getReference();
|
||||
stamp = atomicStampedReference.getStamp();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package cn.whaifree.test.threadpool;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/21 16:40
|
||||
* @注释
|
||||
*/
|
||||
public class ScheduledThreadPoolTest {
|
||||
volatile static Integer i = 0;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
|
||||
scheduledExecutorService.schedule(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("delay:" + i);
|
||||
}
|
||||
}, 10, TimeUnit.SECONDS);
|
||||
// 10秒后才执行
|
||||
scheduledExecutorService.shutdown();
|
||||
|
||||
|
||||
ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||
for (int j = 0; j < 10; j++) {
|
||||
int finalJ = j;
|
||||
executorService.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println(finalJ);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user