Skip to content

Instantly share code, notes, and snippets.

@guohai
Created September 1, 2011 09:43
Show Gist options
  • Save guohai/1185829 to your computer and use it in GitHub Desktop.
Save guohai/1185829 to your computer and use it in GitHub Desktop.
Java AutoIncrement Atomic Bytecode
// javap -c -l AutoIncrementDemo
Compiled from "AutoIncrementDemo.java"
public class AutoIncrementDemo extends java.lang.Object{
public AutoIncrementDemo();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public static void main(java.lang.String[]);
Code:
0: bipush 8
2: istore_1
3: iinc 1, 1
6: ldc2_w #2; //long 3l
9: lstore_2
10: lload_2
11: lconst_1
12: ladd
13: lstore_2
14: return
LineNumberTable:
line 7: 0
line 8: 3
line 9: 6
line 10: 10
line 11: 14
public static void ff0x(java.lang.String[]);
Code:
0: getstatic #4; //Field vc:I
3: iconst_1
4: iadd
5: putstatic #4; //Field vc:I
8: return
LineNumberTable:
line 14: 0
line 15: 8
public static void ff1x(java.lang.String[]);
Code:
0: getstatic #5; //Field vb:J
3: lconst_1
4: ladd
5: putstatic #5; //Field vb:J
8: return
LineNumberTable:
line 18: 0
line 19: 8
public static synchronized void foo();
Code:
0: bipush 8
2: istore_0
3: iinc 0, 1
6: return
LineNumberTable:
line 22: 0
line 23: 3
line 24: 6
public void goo();
Code:
0: aload_0
1: dup
2: astore_1
3: monitorenter
4: bipush 8
6: istore_2
7: iinc 2, 1
10: aload_1
11: monitorexit
12: goto 20
15: astore_3
16: aload_1
17: monitorexit
18: aload_3
19: athrow
20: return
Exception table:
from to target type
4 12 15 any
15 18 15 any
LineNumberTable:
line 27: 0
line 28: 4
line 29: 7
line 30: 10
line 31: 20
public void koo();
Code:
0: aload_0
1: dup
2: astore_1
3: monitorenter
4: ldc2_w #6; //long 8l
7: lstore_2
8: lload_2
9: lconst_1
10: ladd
11: lstore_2
12: aload_1
13: monitorexit
14: goto 24
17: astore 4
19: aload_1
20: monitorexit
21: aload 4
23: athrow
24: return
Exception table:
from to target type
4 14 17 any
17 21 17 any
LineNumberTable:
line 34: 0
line 35: 4
line 36: 8
line 37: 12
line 38: 24
static {};
Code:
0: iconst_1
1: putstatic #4; //Field vc:I
4: lconst_1
5: putstatic #5; //Field vb:J
8: return
LineNumberTable:
line 3: 0
line 4: 4
}
public class AutoIncrementDemo {
private static int vc = 1;
private static long vb = 1L;
public static void main(String[] args) {
int i = 8;
i++;
long j = 3L;
j++;
}
public static void ff0x(String[] args) {
vc++;
}
public static void ff1x(String[] args) {
vb++;
}
public synchronized static void foo() {
int i = 8;
i++;
}
public void goo() {
synchronized(this) {
int i = 8;
i++;
}
}
public void koo() {
synchronized(this) {
long j = 8;
j++;
}
}
}
// javap -c -l AutoIncrementDemo
Compiled from "AutoIncrementDemo.java"
public class AutoIncrementDemo extends java.lang.Object{
public AutoIncrementDemo();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public static void main(java.lang.String[]);
Code:
0: bipush 8
2: istore_1
3: iinc 1, 1
6: ldc2_w #2; //long 3l
9: lstore_2
10: lload_2
11: lconst_1
12: ladd
13: lstore_2
14: return
LineNumberTable:
line 7: 0
line 8: 3
line 9: 6
line 10: 10
line 11: 14
public static void ff0x(java.lang.String[]);
Code:
0: getstatic #4; //Field vc:I
3: iconst_1
4: iadd
5: putstatic #4; //Field vc:I
8: return
LineNumberTable:
line 14: 0
line 15: 8
public static void ff1x(java.lang.String[]);
Code:
0: getstatic #5; //Field vb:J
3: lconst_1
4: ladd
5: putstatic #5; //Field vb:J
8: return
LineNumberTable:
line 18: 0
line 19: 8
public static synchronized void foo();
Code:
0: bipush 8
2: istore_0
3: iinc 0, 1
6: return
LineNumberTable:
line 22: 0
line 23: 3
line 24: 6
public void goo();
Code:
0: aload_0
1: dup
2: astore_1
3: monitorenter
4: bipush 8
6: istore_2
7: iinc 2, 1
10: aload_1
11: monitorexit
12: goto 20
15: astore_3
16: aload_1
17: monitorexit
18: aload_3
19: athrow
20: return
Exception table:
from to target type
4 12 15 any
15 18 15 any
LineNumberTable:
line 27: 0
line 28: 4
line 29: 7
line 30: 10
line 31: 20
public void koo();
Code:
0: aload_0
1: dup
2: astore_1
3: monitorenter
4: ldc2_w #6; //long 8l
7: lstore_2
8: lload_2
9: lconst_1
10: ladd
11: lstore_2
12: aload_1
13: monitorexit
14: goto 24
17: astore 4
19: aload_1
20: monitorexit
21: aload 4
23: athrow
24: return
Exception table:
from to target type
4 14 17 any
17 21 17 any
LineNumberTable:
line 34: 0
line 35: 4
line 36: 8
line 37: 12
line 38: 24
static {};
Code:
0: iconst_1
1: putstatic #4; //Field vc:I
4: lconst_1
5: putstatic #5; //Field vb:J
8: return
LineNumberTable:
line 3: 0
line 4: 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment