Skip to content

Commit

Permalink
markword to synchronized 1
Browse files Browse the repository at this point in the history
  • Loading branch information
yoa1226 committed Nov 3, 2024
1 parent 72e90ae commit 06e52a6
Show file tree
Hide file tree
Showing 10 changed files with 644 additions and 0 deletions.
400 changes: 400 additions & 0 deletions _posts/2024-10-30-sync.md

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions assets/synchronized/JOLSample_01_Age.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package jol;

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;

import static java.lang.System.out;

/**
* @author Aleksey Shipilev
*/
public class JOLSample_01_Age {

public static void main(String[] args) {
A a = new A();
for (int i = 0; i < 500; i++) {
var bytes = new byte[1024 * 1024];
}
out.println(ClassLayout.parseInstance(a).toPrintable());
long markword = VM.current().getLong(a, 0);
printAgeBit(markword);
}

public static class A {
boolean f;
}

public static void printMarkWordBin(long markword) {
out.printf("markword bit: %s\n", toBinaryWithSpaces(markword));
}

public static void printMarkBit(long markword) {
out.printf("mark bit: %s\n", toBinaryWithSpaces(markword & 0b11));
}

public static void printbiasBit(long markword) {
out.printf("bias bit: %s\n", toBinaryWithSpaces(markword >> 2 & 0b1));
}

public static void printAgeBit(long markword) {
out.printf("age bit: %s\n", toBinaryWithSpaces(markword >> 3 & 0b1111));
}

public static void printHashcodeBit(long markword) {
out.printf("hash code bit: %s\n", toBinaryWithSpaces((markword >>> 8)));
}


public static String toHexWithSpaces(long number) {
String hex = Long.toHexString(number);
// 如果长度是奇数,在前面补零
if (hex.length() % 2 != 0) {
hex = "0" + hex;
}
// 每两位分隔开
return hex.replaceAll("(?<=..)(..)", " $1").toUpperCase();
}

public static String toBinaryWithSpaces(long number) {
String binary = Long.toBinaryString(number);
// 如果长度不是8的倍数,前面补零
while (binary.length() % 8 != 0) {
binary = "0" + binary;
}
// 每8位分隔开
return binary.replaceAll("(.{8})", "$1 ");
}

}
70 changes: 70 additions & 0 deletions assets/synchronized/JOLSample_01_Basic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package jol;

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;

import static java.lang.System.out;

/**
* @author Aleksey Shipilev
*/
public class JOLSample_01_Basic {

public static void main(String[] args) {
A a = new A();
out.println(ClassLayout.parseInstance(a).toPrintable());

long markword = VM.current().getLong(a,0);
printMarkWordBin(markword);
printMarkBit(markword);
printbiasBit(markword);
printAgeBit(markword);
}

public static class A {
boolean f;
}

public static void printMarkWordBin(long markword) {
out.printf("markword bit: %s\n", toBinaryWithSpaces(markword));
}

public static void printMarkBit(long markword) {
out.printf("mark bit: %s\n", toBinaryWithSpaces(markword & 0b11));
}

public static void printbiasBit(long markword) {
out.printf("bias bit: %s\n", toBinaryWithSpaces(markword>>2 & 0b1));
}

public static void printAgeBit(long markword) {
out.printf("age bit: %s\n", toBinaryWithSpaces(markword >> 3 & 0b1111));
}

public static void printHashcodeBit(long markword) {
out.printf("hash code bit: %s\n", toBinaryWithSpaces((markword >>> 8)));
}



public static String toHexWithSpaces(long number) {
String hex = Long.toHexString(number);
// 如果长度是奇数,在前面补零
if (hex.length() % 2 != 0) {
hex = "0" + hex;
}
// 每两位分隔开
return hex.replaceAll("(?<=..)(..)", " $1").toUpperCase();
}

public static String toBinaryWithSpaces(long number) {
String binary = Long.toBinaryString(number);
// 如果长度不是8的倍数,前面补零
while (binary.length() % 8 != 0) {
binary = "0" + binary;
}
// 每8位分隔开
return binary.replaceAll("(.{8})", "$1 ");
}

}
70 changes: 70 additions & 0 deletions assets/synchronized/JOLSample_01_Hashcode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package jol;

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;

import static java.lang.System.out;

/**
* @author Aleksey Shipilev
*/
public class JOLSample_01_Hashcode {

public static void main(String[] args) {
A a = new A();

int hashcode = a.hashCode();
out.println(ClassLayout.parseInstance(a).toPrintable());
out.println("a.hashCode() : " + toBinaryWithSpaces(hashcode));
long markword = VM.current().getLong(a,0);
printHashcodeBit(markword);//(markword >>> 8)
printMarkWordBin(markword);
}

public static class A {
boolean f;
}

public static void printMarkWordBin(long markword) {
out.printf("markword bit: %s\n", toBinaryWithSpaces(markword));
}

public static void printMarkBit(long markword) {
out.printf("mark bit: %s\n", toBinaryWithSpaces(markword & 0b11));
}

public static void printbiasBit(long markword) {
out.printf("bias bit: %s\n", toBinaryWithSpaces(markword>>2 & 0b1));
}

public static void printAgeBit(long markword) {
out.printf("age bit: %s\n", toBinaryWithSpaces(markword >> 3 & 0b1111));
}

public static void printHashcodeBit(long markword) {
out.printf("hash code bit: %s\n", toBinaryWithSpaces((markword >>> 8)));
}



public static String toHexWithSpaces(long number) {
String hex = Long.toHexString(number);
// 如果长度是奇数,在前面补零
if (hex.length() % 2 != 0) {
hex = "0" + hex;
}
// 每两位分隔开
return hex.replaceAll("(?<=..)(..)", " $1").toUpperCase();
}

public static String toBinaryWithSpaces(long number) {
String binary = Long.toBinaryString(number);
// 如果长度不是8的倍数,前面补零
while (binary.length() % 8 != 0) {
binary = "0" + binary;
}
// 每8位分隔开
return binary.replaceAll("(.{8})", "$1 ");
}

}
36 changes: 36 additions & 0 deletions assets/synchronized/SyncTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.ArrayList;

public class SyncTest {
private Object lock = new Object();
private Object lock1 = new Object();

public static void main(String[] args) {
new Thread(()->{
new SyncTest().testCountMonitorInMethod();
}).start();
smallObj();
}

public void testCountMonitorInMethod() {
synchronized (lock) {
synchronized (lock1) {
sleep();
}
}
}

public static void smallObj() {
ArrayList<byte[]> list = new ArrayList<byte[]>();
for (int i = 0; i < 100000; i++) {
list.add(new byte[1024*1024/2]);
}
}

public void sleep(){
try {
Thread.sleep(1000_1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Binary file added assets/synchronized/count-monitor-when-gc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/synchronized/count-monitor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/synchronized/java-version.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/synchronized/lightweight-lock-record.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/synchronized/lock-level.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 06e52a6

Please sign in to comment.