Skip to content

Commit

Permalink
Merge pull request crossoverJie#76 from crossoverJie/fix
Browse files Browse the repository at this point in the history
  • Loading branch information
crossoverJie authored Jul 28, 2018
2 parents 738d23e + 2b84f09 commit 3b8c09d
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<version>1.1.40</version>
</dependency>

<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.2</version>
</dependency>

</dependencies>

<build>
Expand Down
121 changes: 121 additions & 0 deletions src/main/java/com/crossoverjie/algorithm/BinaryNodeTravel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.crossoverjie.algorithm;

import java.util.LinkedList;

/**
* Function: 层序遍历,需要将遍历的节点串联起来
*
* @author crossoverJie
* Date: 2018/7/27 23:37
* @since JDK 1.8
*/
public class BinaryNodeTravel {

private Object data ;
private BinaryNodeTravel left ;
private BinaryNodeTravel right ;
public BinaryNodeTravel next;

public BinaryNodeTravel() {
}

public BinaryNodeTravel(Object data, BinaryNodeTravel left, BinaryNodeTravel right) {
this.data = data;
this.left = left;
this.right = right;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

public BinaryNodeTravel getLeft() {
return left;
}

public void setLeft(BinaryNodeTravel left) {
this.left = left;
}

public BinaryNodeTravel getRight() {
return right;
}

public void setRight(BinaryNodeTravel right) {
this.right = right;
}


public BinaryNodeTravel createNode(){
BinaryNodeTravel nodeA = new BinaryNodeTravel("A",null,null) ;
BinaryNodeTravel nodeB = new BinaryNodeTravel("B",null,null) ;
BinaryNodeTravel nodeC = new BinaryNodeTravel("C",null,null) ;
BinaryNodeTravel nodeD = new BinaryNodeTravel("D",null,null) ;
BinaryNodeTravel nodeE = new BinaryNodeTravel("E",null,null) ;
BinaryNodeTravel nodeF = new BinaryNodeTravel("F",null,null) ;

nodeA.setLeft(nodeB);
nodeB.setLeft(nodeD);
nodeA.setRight(nodeC);
nodeC.setLeft(nodeE);
nodeC.setRight(nodeF);

return nodeA ;
}

@Override
public String toString() {
return "BinaryNode{" +
"data=" + data +
", left=" + left +
", right=" + right +
'}';
}


/**
* 二叉树的层序遍历 借助于队列来实现 借助队列的先进先出的特性
*
* 首先将根节点入队列 然后遍历队列。
*
* 暂时把上一个节点存起来,每次都把上一节点的 next 指向当前节点
*
* 首先将根节点打印出来,接着判断左节点是否为空 不为空则加入队列
* @param node
*/
public BinaryNodeTravel levelIterator(BinaryNodeTravel node){
LinkedList<BinaryNodeTravel> queue = new LinkedList<>() ;


//暂时存放的上一节点
BinaryNodeTravel pre = null;

//先将根节点入队
queue.offer(node) ;
BinaryNodeTravel current ;
while (!queue.isEmpty()){
current = queue.poll();

//将上一节点指向当前节点
if (pre == null){
pre = current ;
}else {
pre.next = current ;
pre = current;
}

if (current.getLeft() != null){
queue.offer(current.getLeft()) ;
}
if (current.getRight() != null){
queue.offer(current.getRight()) ;
}
}

return node ;
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/crossoverjie/hystrix/CommandOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.crossoverjie.hystrix;

import com.netflix.hystrix.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
* Function:订单服务
*
* @author crossoverJie
* Date: 2018/7/28 16:43
* @since JDK 1.8
*/
public class CommandOrder extends HystrixCommand<String> {

private final static Logger LOGGER = LoggerFactory.getLogger(CommandOrder.class);

private String orderName;

public CommandOrder(String orderName) {


super(Setter.withGroupKey(
//服务分组
HystrixCommandGroupKey.Factory.asKey("OrderGroup"))
//线程分组
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("OrderPool"))

//线程池配置
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withCoreSize(10)
.withKeepAliveTimeMinutes(5)
.withMaxQueueSize(10)
.withQueueSizeRejectionThreshold(10000))

.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
)
;
this.orderName = orderName;
}


@Override
public String run() throws Exception {

LOGGER.info("orderName=[{}]", orderName);

TimeUnit.MILLISECONDS.sleep(100);
return "OrderName=" + orderName;
}


}
40 changes: 40 additions & 0 deletions src/main/java/com/crossoverjie/hystrix/CommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.crossoverjie.hystrix;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
* Function: 线程隔离测试
*
* @author crossoverJie
* Date: 2018/7/28 16:58
* @since JDK 1.8
*/
public class CommandTest {

private final static Logger LOGGER = LoggerFactory.getLogger(CommandTest.class);


public static void main(String[] args) throws Exception {
CommandOrder commandPhone = new CommandOrder("手机");
CommandOrder command = new CommandOrder("电视");


//阻塞方式执行
String execute = commandPhone.execute();
LOGGER.info("execute=[{}]", execute);

//异步非阻塞方式
Future<String> queue = command.queue();
String value = queue.get(200, TimeUnit.MILLISECONDS);
LOGGER.info("value=[{}]", value);


CommandUser commandUser = new CommandUser("张三");
String name = commandUser.execute();
LOGGER.info("name=[{}]", name);
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/crossoverjie/hystrix/CommandUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.crossoverjie.hystrix;

import com.netflix.hystrix.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
* Function:用户服务
*
* @author crossoverJie
* Date: 2018/7/28 16:43
* @since JDK 1.8
*/
public class CommandUser extends HystrixCommand<String> {

private final static Logger LOGGER = LoggerFactory.getLogger(CommandUser.class);

private String userName;

public CommandUser(String userName) {


super(Setter.withGroupKey(
//服务分组
HystrixCommandGroupKey.Factory.asKey("UserGroup"))
//线程分组
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("UserPool"))

//线程池配置
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withCoreSize(10)
.withKeepAliveTimeMinutes(5)
.withMaxQueueSize(10)
.withQueueSizeRejectionThreshold(10000))

//线程池隔离
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
)
;
this.userName = userName;
}


@Override
public String run() throws Exception {

LOGGER.info("userName=[{}]", userName);

TimeUnit.MILLISECONDS.sleep(100);
return "userName=" + userName;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void test1(){
node = node.createNode() ;
System.out.println(node);

//中序遍历二叉树
//层序遍历二叉树
node.levelIterator(node) ;

}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/crossoverjie/algorithm/BinaryNodeTravelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.crossoverjie.algorithm;

import org.junit.Test;

public class BinaryNodeTravelTest {
@Test
public void levelIterator() throws Exception {
BinaryNodeTravel node = new BinaryNodeTravel() ;
//创建二叉树
node = node.createNode() ;

//层序遍历二叉树
BinaryNodeTravel binaryNodeTravel = node.levelIterator(node);

while (binaryNodeTravel != null){
System.out.print(binaryNodeTravel.getData() +"--->");
binaryNodeTravel = binaryNodeTravel.next;
}
}

}

0 comments on commit 3b8c09d

Please sign in to comment.