File tree Expand file tree Collapse file tree 5 files changed +106
-0
lines changed Expand file tree Collapse file tree 5 files changed +106
-0
lines changed Original file line number Diff line number Diff line change 7
7
<content url =" file://$MODULE_DIR$" >
8
8
<sourceFolder url =" file://$MODULE_DIR$/src/main/java" isTestSource =" false" />
9
9
<sourceFolder url =" file://$MODULE_DIR$/src/test/java" isTestSource =" true" />
10
+ <sourceFolder url =" file://$MODULE_DIR$/src/main/resource" type =" java-resource" />
10
11
<excludeFolder url =" file://$MODULE_DIR$/target" />
11
12
</content >
12
13
<orderEntry type =" inheritedJdk" />
Original file line number Diff line number Diff line change 16
16
<version >5.1.30</version >
17
17
</dependency >
18
18
19
+ <dependency >
20
+ <groupId >org.projectlombok</groupId >
21
+ <artifactId >lombok</artifactId >
22
+ <optional >true</optional >
23
+ </dependency >
19
24
<!-- 日志文件管理包 -->
20
25
<!-- log start -->
21
26
<dependency >
Original file line number Diff line number Diff line change
1
+ package com .retry ;
2
+
3
+
4
+ import org .apache .log4j .Logger ;
5
+
6
+ import java .util .Random ;
7
+
8
+ /**
9
+ * 重试
10
+ *
11
+ * 服务不稳定的时候,可以重试。
12
+ *
13
+ */
14
+ public class RetryDemo {
15
+ private static final Logger log = Logger .getLogger (RetryDemo .class .getName ());
16
+
17
+ public static void main (String [] args ) throws InterruptedException {
18
+ retryDoSomething ();
19
+ }
20
+
21
+ public static void retryDoSomething () throws InterruptedException {
22
+ log .info ("retryDoSomething start." );
23
+ //重试次数
24
+ int maxTimes = 5 ;
25
+ //每次重试间隔时间
26
+ int interval = 500 ;
27
+
28
+ for (int i = 0 ; i <= maxTimes ; i ++) {
29
+ boolean isOk = doSomething ();
30
+ //成功了就停止
31
+ if (isOk ) {
32
+ break ;
33
+ }
34
+ //失败就间隔一段时间后再执行
35
+ Thread .sleep (interval );
36
+ }
37
+ log .info ("retryDoSomething end." );
38
+ }
39
+
40
+
41
+ /**
42
+ * 执行逻辑,成功就返回 true,报错返回 false
43
+ *
44
+ * @return
45
+ */
46
+ public static boolean doSomething () {
47
+ try {
48
+ //执行逻辑
49
+ doSth ();
50
+ } catch (Exception e ) {
51
+ log .error ("service.doSomething() error." , e );
52
+ return false ;
53
+ }
54
+ return true ;
55
+ }
56
+
57
+ /**
58
+ * 执行逻辑的方法
59
+ *
60
+ * 以下是示例,可以将示例替换成自己的逻辑
61
+ *
62
+ */
63
+ private static void doSth () {
64
+ Random random = new Random ();
65
+ //随机产生一个[0-100]之间的随机数,由于是随机,每次执行的结果可能不一样
66
+ int num = random .nextInt (101 );
67
+ if (num % 5 ==0 ) {
68
+ log .info ("doSth num:" + num );
69
+ } else {
70
+ //模拟失败,抛异常
71
+ throw new NumberFormatException ();
72
+ }
73
+
74
+ }
75
+ }
Original file line number Diff line number Diff line change
1
+ package test .com .bit ;
2
+
3
+ public class HashTest {
4
+ public static void main (String [] args ) {
5
+ int hash = 19 ;
6
+ int i = 15 & hash ;
7
+ System .out .println (hash %15 );
8
+ System .out .println (i );
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ package test .com .bit ;
2
+
3
+ import com .concurrent .ConcurrentHashMapDemo ;
4
+
5
+ import java .util .concurrent .ConcurrentHashMap ;
6
+
7
+ public class PutValTest {
8
+ public static void main (String [] args ) {
9
+
10
+ ConcurrentHashMap <String ,String > concurrentHashMap = new ConcurrentHashMap <>();
11
+ concurrentHashMap .put ("abcd" , "cccc" );
12
+ System .out .println (concurrentHashMap .get ("abcd" ));
13
+
14
+ }
15
+ }
You can’t perform that action at this time.
0 commit comments