forked from crossoverJie/JCSprout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request crossoverJie#76 from crossoverJie/fix
- Loading branch information
Showing
7 changed files
with
304 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/com/crossoverjie/algorithm/BinaryNodeTravel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/test/java/com/crossoverjie/algorithm/BinaryNodeTravelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |