Skip to content

Commit 3b8c09d

Browse files
authored
Merge pull request crossoverJie#76 from crossoverJie/fix
2 parents 738d23e + 2b84f09 commit 3b8c09d

File tree

7 files changed

+304
-1
lines changed

7 files changed

+304
-1
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
<version>1.1.40</version>
9696
</dependency>
9797

98+
<dependency>
99+
<groupId>com.netflix.hystrix</groupId>
100+
<artifactId>hystrix-core</artifactId>
101+
<version>1.5.2</version>
102+
</dependency>
103+
98104
</dependencies>
99105

100106
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.crossoverjie.algorithm;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* Function: 层序遍历,需要将遍历的节点串联起来
7+
*
8+
* @author crossoverJie
9+
* Date: 2018/7/27 23:37
10+
* @since JDK 1.8
11+
*/
12+
public class BinaryNodeTravel {
13+
14+
private Object data ;
15+
private BinaryNodeTravel left ;
16+
private BinaryNodeTravel right ;
17+
public BinaryNodeTravel next;
18+
19+
public BinaryNodeTravel() {
20+
}
21+
22+
public BinaryNodeTravel(Object data, BinaryNodeTravel left, BinaryNodeTravel right) {
23+
this.data = data;
24+
this.left = left;
25+
this.right = right;
26+
}
27+
28+
public Object getData() {
29+
return data;
30+
}
31+
32+
public void setData(Object data) {
33+
this.data = data;
34+
}
35+
36+
public BinaryNodeTravel getLeft() {
37+
return left;
38+
}
39+
40+
public void setLeft(BinaryNodeTravel left) {
41+
this.left = left;
42+
}
43+
44+
public BinaryNodeTravel getRight() {
45+
return right;
46+
}
47+
48+
public void setRight(BinaryNodeTravel right) {
49+
this.right = right;
50+
}
51+
52+
53+
public BinaryNodeTravel createNode(){
54+
BinaryNodeTravel nodeA = new BinaryNodeTravel("A",null,null) ;
55+
BinaryNodeTravel nodeB = new BinaryNodeTravel("B",null,null) ;
56+
BinaryNodeTravel nodeC = new BinaryNodeTravel("C",null,null) ;
57+
BinaryNodeTravel nodeD = new BinaryNodeTravel("D",null,null) ;
58+
BinaryNodeTravel nodeE = new BinaryNodeTravel("E",null,null) ;
59+
BinaryNodeTravel nodeF = new BinaryNodeTravel("F",null,null) ;
60+
61+
nodeA.setLeft(nodeB);
62+
nodeB.setLeft(nodeD);
63+
nodeA.setRight(nodeC);
64+
nodeC.setLeft(nodeE);
65+
nodeC.setRight(nodeF);
66+
67+
return nodeA ;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return "BinaryNode{" +
73+
"data=" + data +
74+
", left=" + left +
75+
", right=" + right +
76+
'}';
77+
}
78+
79+
80+
/**
81+
* 二叉树的层序遍历 借助于队列来实现 借助队列的先进先出的特性
82+
*
83+
* 首先将根节点入队列 然后遍历队列。
84+
*
85+
* 暂时把上一个节点存起来,每次都把上一节点的 next 指向当前节点
86+
*
87+
* 首先将根节点打印出来,接着判断左节点是否为空 不为空则加入队列
88+
* @param node
89+
*/
90+
public BinaryNodeTravel levelIterator(BinaryNodeTravel node){
91+
LinkedList<BinaryNodeTravel> queue = new LinkedList<>() ;
92+
93+
94+
//暂时存放的上一节点
95+
BinaryNodeTravel pre = null;
96+
97+
//先将根节点入队
98+
queue.offer(node) ;
99+
BinaryNodeTravel current ;
100+
while (!queue.isEmpty()){
101+
current = queue.poll();
102+
103+
//将上一节点指向当前节点
104+
if (pre == null){
105+
pre = current ;
106+
}else {
107+
pre.next = current ;
108+
pre = current;
109+
}
110+
111+
if (current.getLeft() != null){
112+
queue.offer(current.getLeft()) ;
113+
}
114+
if (current.getRight() != null){
115+
queue.offer(current.getRight()) ;
116+
}
117+
}
118+
119+
return node ;
120+
}
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.crossoverjie.hystrix;
2+
3+
import com.netflix.hystrix.*;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* Function:订单服务
11+
*
12+
* @author crossoverJie
13+
* Date: 2018/7/28 16:43
14+
* @since JDK 1.8
15+
*/
16+
public class CommandOrder extends HystrixCommand<String> {
17+
18+
private final static Logger LOGGER = LoggerFactory.getLogger(CommandOrder.class);
19+
20+
private String orderName;
21+
22+
public CommandOrder(String orderName) {
23+
24+
25+
super(Setter.withGroupKey(
26+
//服务分组
27+
HystrixCommandGroupKey.Factory.asKey("OrderGroup"))
28+
//线程分组
29+
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("OrderPool"))
30+
31+
//线程池配置
32+
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
33+
.withCoreSize(10)
34+
.withKeepAliveTimeMinutes(5)
35+
.withMaxQueueSize(10)
36+
.withQueueSizeRejectionThreshold(10000))
37+
38+
.andCommandPropertiesDefaults(
39+
HystrixCommandProperties.Setter()
40+
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
41+
)
42+
;
43+
this.orderName = orderName;
44+
}
45+
46+
47+
@Override
48+
public String run() throws Exception {
49+
50+
LOGGER.info("orderName=[{}]", orderName);
51+
52+
TimeUnit.MILLISECONDS.sleep(100);
53+
return "OrderName=" + orderName;
54+
}
55+
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.crossoverjie.hystrix;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.concurrent.Future;
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* Function: 线程隔离测试
11+
*
12+
* @author crossoverJie
13+
* Date: 2018/7/28 16:58
14+
* @since JDK 1.8
15+
*/
16+
public class CommandTest {
17+
18+
private final static Logger LOGGER = LoggerFactory.getLogger(CommandTest.class);
19+
20+
21+
public static void main(String[] args) throws Exception {
22+
CommandOrder commandPhone = new CommandOrder("手机");
23+
CommandOrder command = new CommandOrder("电视");
24+
25+
26+
//阻塞方式执行
27+
String execute = commandPhone.execute();
28+
LOGGER.info("execute=[{}]", execute);
29+
30+
//异步非阻塞方式
31+
Future<String> queue = command.queue();
32+
String value = queue.get(200, TimeUnit.MILLISECONDS);
33+
LOGGER.info("value=[{}]", value);
34+
35+
36+
CommandUser commandUser = new CommandUser("张三");
37+
String name = commandUser.execute();
38+
LOGGER.info("name=[{}]", name);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.crossoverjie.hystrix;
2+
3+
import com.netflix.hystrix.*;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* Function:用户服务
11+
*
12+
* @author crossoverJie
13+
* Date: 2018/7/28 16:43
14+
* @since JDK 1.8
15+
*/
16+
public class CommandUser extends HystrixCommand<String> {
17+
18+
private final static Logger LOGGER = LoggerFactory.getLogger(CommandUser.class);
19+
20+
private String userName;
21+
22+
public CommandUser(String userName) {
23+
24+
25+
super(Setter.withGroupKey(
26+
//服务分组
27+
HystrixCommandGroupKey.Factory.asKey("UserGroup"))
28+
//线程分组
29+
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("UserPool"))
30+
31+
//线程池配置
32+
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
33+
.withCoreSize(10)
34+
.withKeepAliveTimeMinutes(5)
35+
.withMaxQueueSize(10)
36+
.withQueueSizeRejectionThreshold(10000))
37+
38+
//线程池隔离
39+
.andCommandPropertiesDefaults(
40+
HystrixCommandProperties.Setter()
41+
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
42+
)
43+
;
44+
this.userName = userName;
45+
}
46+
47+
48+
@Override
49+
public String run() throws Exception {
50+
51+
LOGGER.info("userName=[{}]", userName);
52+
53+
TimeUnit.MILLISECONDS.sleep(100);
54+
return "userName=" + userName;
55+
}
56+
57+
58+
}

src/test/java/com/crossoverjie/algorithm/BinaryNodeTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public void test1(){
1111
node = node.createNode() ;
1212
System.out.println(node);
1313

14-
//中序遍历二叉树
14+
//层序遍历二叉树
1515
node.levelIterator(node) ;
1616

1717
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.crossoverjie.algorithm;
2+
3+
import org.junit.Test;
4+
5+
public class BinaryNodeTravelTest {
6+
@Test
7+
public void levelIterator() throws Exception {
8+
BinaryNodeTravel node = new BinaryNodeTravel() ;
9+
//创建二叉树
10+
node = node.createNode() ;
11+
12+
//层序遍历二叉树
13+
BinaryNodeTravel binaryNodeTravel = node.levelIterator(node);
14+
15+
while (binaryNodeTravel != null){
16+
System.out.print(binaryNodeTravel.getData() +"--->");
17+
binaryNodeTravel = binaryNodeTravel.next;
18+
}
19+
}
20+
21+
}

0 commit comments

Comments
 (0)