add recursive path find demo
add demo for creating new thred
This commit is contained in:
parent
3c709917e1
commit
f5472740be
22
src/createthread/CreateByCallable.java
Normal file
22
src/createthread/CreateByCallable.java
Normal file
@ -0,0 +1,22 @@
|
||||
package createthread;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
public class CreateByCallable implements Callable<String> {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "create by implementing Callable";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CreateByCallable createByCallable = new CreateByCallable();
|
||||
FutureTask<String> futureTask = new FutureTask<>(createByCallable);
|
||||
new Thread(futureTask).start();
|
||||
try {
|
||||
System.out.println(futureTask.get());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
27
src/createthread/CreateByExecutorService.java
Normal file
27
src/createthread/CreateByExecutorService.java
Normal file
@ -0,0 +1,27 @@
|
||||
package createthread;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class CreateByExecutorService {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ExecutorService poolA = Executors.newFixedThreadPool(2);
|
||||
poolA.execute(() -> {
|
||||
System.out.println("poolA");
|
||||
});
|
||||
|
||||
poolA.shutdown();
|
||||
|
||||
ThreadPoolExecutor poolB = new ThreadPoolExecutor(2,3,0,
|
||||
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(3),
|
||||
Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
|
||||
|
||||
poolB.submit(() -> {
|
||||
System.out.println("poolB");
|
||||
});
|
||||
poolB.shutdown();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
13
src/createthread/CreateByRunnable.java
Normal file
13
src/createthread/CreateByRunnable.java
Normal file
@ -0,0 +1,13 @@
|
||||
package createthread;
|
||||
|
||||
public class CreateByRunnable implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("create by implementing Runnable");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CreateByRunnable createByRunnable = new CreateByRunnable();
|
||||
new Thread(createByRunnable).start();
|
||||
}
|
||||
}
|
13
src/createthread/CreateByThread.java
Normal file
13
src/createthread/CreateByThread.java
Normal file
@ -0,0 +1,13 @@
|
||||
package createthread;
|
||||
|
||||
public class CreateByThread extends Thread{
|
||||
@Override
|
||||
public void run(){
|
||||
System.out.println("create new thread by extending Thread");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CreateByThread createByThread = new CreateByThread();
|
||||
createByThread.start();
|
||||
}
|
||||
}
|
43
src/recursive/TreeNode.java
Normal file
43
src/recursive/TreeNode.java
Normal file
@ -0,0 +1,43 @@
|
||||
package recursive;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TreeNode {
|
||||
|
||||
private final int id;
|
||||
|
||||
private final int parentId;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final List<TreeNode> children;
|
||||
|
||||
public TreeNode(int id, int parentId, String name) {
|
||||
this.id = id;
|
||||
this.parentId = parentId;
|
||||
this.name = name;
|
||||
children = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addChild(TreeNode child) {
|
||||
children.add(child);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getParentId() {
|
||||
return parentId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TreeNode{" +
|
||||
"id=" + id +
|
||||
", parentId=" + parentId +
|
||||
", children=" + children.toString() +
|
||||
'}';
|
||||
}
|
||||
}
|
42
src/recursive/TreeStructure.java
Normal file
42
src/recursive/TreeStructure.java
Normal file
@ -0,0 +1,42 @@
|
||||
package recursive;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class TreeStructure {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<TreeNode> treeNodes = new ArrayList<>();
|
||||
Set<Integer> leafSet = new HashSet<>();
|
||||
treeNodes.add(new TreeNode(1,0,"root"));
|
||||
treeNodes.add(new TreeNode(2,1,"a1"));
|
||||
treeNodes.add(new TreeNode(3,1,"a2"));
|
||||
treeNodes.add(new TreeNode(4,2,"b1"));
|
||||
treeNodes.add(new TreeNode(5,4,"c1"));
|
||||
|
||||
Map<Integer, TreeNode> treeMap = new HashMap<>();
|
||||
|
||||
for (TreeNode treeNode : treeNodes) {
|
||||
leafSet.add(treeNode.getId());
|
||||
}
|
||||
|
||||
for (TreeNode treeNode : treeNodes) {
|
||||
treeMap.put(treeNode.getId(), treeNode);
|
||||
leafSet.remove(treeNode.getParentId());
|
||||
}
|
||||
|
||||
for (Integer id: leafSet) {
|
||||
int parentId = treeMap.get(id).getParentId();
|
||||
int nodeId = id;
|
||||
while (parentId != 0) {
|
||||
treeMap.get(parentId).addChild(treeMap.get(nodeId));
|
||||
nodeId = parentId;
|
||||
parentId = treeMap.get(nodeId).getParentId();
|
||||
}
|
||||
}
|
||||
|
||||
TreeNode finalNode = treeMap.values().stream()
|
||||
.filter(treeNode -> treeNode.getParentId() == 0).toList().getFirst();
|
||||
|
||||
System.out.println(finalNode);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user