Showing
1 changed file
with
14 additions
and
6 deletions
@@ -16,6 +16,7 @@ import org.apache.commons.lang3.RandomUtils; | @@ -16,6 +16,7 @@ import org.apache.commons.lang3.RandomUtils; | ||
16 | import org.springframework.beans.factory.annotation.Autowired; | 16 | import org.springframework.beans.factory.annotation.Autowired; |
17 | import org.springframework.web.bind.annotation.RequestBody; | 17 | import org.springframework.web.bind.annotation.RequestBody; |
18 | import org.springframework.web.bind.annotation.RequestMapping; | 18 | import org.springframework.web.bind.annotation.RequestMapping; |
19 | +import org.springframework.web.bind.annotation.RequestParam; | ||
19 | import org.springframework.web.bind.annotation.RestController; | 20 | import org.springframework.web.bind.annotation.RestController; |
20 | 21 | ||
21 | import java.util.UUID; | 22 | import java.util.UUID; |
@@ -57,12 +58,12 @@ public class OrderHelpController { | @@ -57,12 +58,12 @@ public class OrderHelpController { | ||
57 | @IgnoreSignature | 58 | @IgnoreSignature |
58 | @InnerApi | 59 | @InnerApi |
59 | @RequestMapping(value = "/lock") | 60 | @RequestMapping(value = "/lock") |
60 | - public ApiResponse lock(String key) { | 61 | + public ApiResponse lock(String key, @RequestParam(required = false, defaultValue = "100") int taskNum) { |
61 | ExecutorService executorService = new ThreadPoolExecutor(5, 10, | 62 | ExecutorService executorService = new ThreadPoolExecutor(5, 10, |
62 | 60, TimeUnit.SECONDS, | 63 | 60, TimeUnit.SECONDS, |
63 | new ArrayBlockingQueue<>(1000), NamedThreadFactory.newThreadFactory("test")); | 64 | new ArrayBlockingQueue<>(1000), NamedThreadFactory.newThreadFactory("test")); |
64 | 65 | ||
65 | - IntStream.range(0, 100) | 66 | + IntStream.range(0, taskNum) |
66 | .forEach(i -> executorService.submit(() -> lockTest(key))); | 67 | .forEach(i -> executorService.submit(() -> lockTest(key))); |
67 | ExecutorServiceUtils.shutdownAndAwaitTermination(executorService); | 68 | ExecutorServiceUtils.shutdownAndAwaitTermination(executorService); |
68 | return new ApiResponse.ApiResponseBuilder() | 69 | return new ApiResponse.ApiResponseBuilder() |
@@ -75,18 +76,25 @@ public class OrderHelpController { | @@ -75,18 +76,25 @@ public class OrderHelpController { | ||
75 | RedisKeyBuilder redisLockKey = RedisKeyBuilder.newInstance() | 76 | RedisKeyBuilder redisLockKey = RedisKeyBuilder.newInstance() |
76 | .appendFixed("ufo:order:lock:test:") | 77 | .appendFixed("ufo:order:lock:test:") |
77 | .appendVar(key); | 78 | .appendVar(key); |
78 | - RedisLock lock = redisLockFactory.newLock(redisLockKey,5, TimeUnit.SECONDS); | 79 | + RedisLock lock = redisLockFactory.newLock(redisLockKey, 5, TimeUnit.SECONDS); |
79 | if (lock.tryLock()) { | 80 | if (lock.tryLock()) { |
80 | try { | 81 | try { |
81 | log.info("lock test {}, i got the lock", key); | 82 | log.info("lock test {}, i got the lock", key); |
82 | - Thread.sleep(RandomUtils.nextInt(10, 50)); | ||
83 | - } catch (InterruptedException e) { | ||
84 | - | 83 | + sleep(RandomUtils.nextInt(20, 50)); |
85 | } finally { | 84 | } finally { |
86 | lock.unlock(); | 85 | lock.unlock(); |
87 | } | 86 | } |
88 | } else { | 87 | } else { |
89 | log.info("lock test {}, i not got the lock", key); | 88 | log.info("lock test {}, i not got the lock", key); |
89 | + sleep(RandomUtils.nextInt(20, 30)); | ||
90 | + } | ||
91 | + } | ||
92 | + | ||
93 | + private void sleep(long millis) { | ||
94 | + try { | ||
95 | + Thread.sleep(millis); | ||
96 | + } catch (InterruptedException e) { | ||
97 | + | ||
90 | } | 98 | } |
91 | } | 99 | } |
92 | 100 |
-
Please register or login to post a comment