add maven as package manager

add ShowObjectHeader.java to show object header
This commit is contained in:
2025-02-19 13:21:49 +08:00
parent 103b75b80b
commit 2a2707c4e7
20 changed files with 146 additions and 19 deletions

View File

@ -0,0 +1,27 @@
package main.java.jvm;
/**
* This class is intended to demo how main.java.jvm create object and invoke constructor. Please see byte code.
* use javap -v ObjectCreate.class or javap -c ObjectCreate.class to see byte code
*/
public class ObjectCreate {
private String field;
// will invoked by invokespecial
public ObjectCreate(String field) {
this.field = field;
}
public void print() {
show();
}
private void show() {
System.out.println(field);
}
public static void main(String[] args) {
ObjectCreate objectCreate = new ObjectCreate("hello!");
objectCreate.print();
}
}

View File

@ -0,0 +1,42 @@
package jvm;
import org.openjdk.jol.info.ClassLayout;
public class ShowObjectHeader {
static final Object lock = new Object();
public void syncObject() {
synchronized (lock) {
try {
Thread.sleep(1000);
System.out.println("enter sync");
System.out.println(ClassLayout.parseInstance(lock).toPrintable());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
ShowObjectHeader showObjectHeader = new ShowObjectHeader();
System.out.println("newly created");
System.out.println(ClassLayout.parseInstance(lock).toPrintable());
Thread t1 = new Thread(showObjectHeader::syncObject);
t1.start();
t1.join();
Thread t2 = new Thread(showObjectHeader::syncObject);
Thread t3 = new Thread(showObjectHeader::syncObject);
t2.start();
t3.start();
t2.join();
t3.join();
System.out.println("after sync");
System.out.println(ClassLayout.parseInstance(lock).toPrintable());
}
}

View File

@ -0,0 +1,17 @@
package main.java.jvm.createthread;
public class CreateByAnonymousClass {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("create thread by using anonymous class");
}
}).start();
new Thread(() -> {
System.out.println("create thread by using lambda");
}).start();
}
}

View File

@ -0,0 +1,22 @@
package main.java.jvm.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();
}
}
}

View File

@ -0,0 +1,30 @@
package main.java.jvm.createthread;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class CreateByCompletableFuture {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
System.out.println("create thread by using CompletableFuture. Returning results...");
return "hello!";
});
while (true) {
if (cf.isDone()) {
System.out.println(cf.get());
break;
}
}
// try {
// Thread.sleep(0);
// System.out.println(cf.get());
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// throw new RuntimeException(e);
// }
}
}

View File

@ -0,0 +1,27 @@
package main.java.jvm.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();
}
}

View File

@ -0,0 +1,18 @@
package main.java.jvm.createthread;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
public class CreateByForkJoin {
public static void main(String[] args) {
ForkJoinPool forkJoinPool = new ForkJoinPool();
forkJoinPool.execute(() -> {
System.out.println("create thread by using forkjoin 10A...");
});
List<String> list = Arrays.asList("10B......");
list.parallelStream().forEach(System.out::println);
}
}

View File

@ -0,0 +1,18 @@
package main.java.jvm.createthread;
import java.util.concurrent.FutureTask;
public class CreateByFutureTask {
public static void main(String[] args)
{
FutureTask<String> futureTask = new FutureTask<>(() -> "Create by FutureTask");
new Thread(futureTask).start();
try {
System.out.println(futureTask.get());
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,13 @@
package main.java.jvm.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();
}
}

View File

@ -0,0 +1,13 @@
package main.java.jvm.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();
}
}

View File

@ -0,0 +1,18 @@
package main.java.jvm.createthread;
public class CreateByThreadGroup {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("threadGroup");
new Thread(threadGroup, () -> {
System.out.println("6-T1......");
},"T1").start();
new Thread(threadGroup, () -> {
System.out.println("6-T2......");
},"T2").start();
new Thread(threadGroup, () -> {
System.out.println("6-T3......");
},"T3").start();
}
}

View File

@ -0,0 +1,18 @@
package main.java.jvm.createthread;
import java.util.Timer;
import java.util.TimerTask;
public class CreateByTimer {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("create thread by using timer");
}
}, 0, 1000);
}
}

View File

@ -0,0 +1,43 @@
package main.java.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() +
'}';
}
}

View File

@ -0,0 +1,42 @@
package main.java.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);
}
}