Java中的锁(Locks in Java)
- - 并发编程网 - ifeve.com原文链接 作者:Jakob Jenkov 译者:申章 校对:丁一. 锁像synchronized同步块一样,是一种线程同步机制,但比Java中的synchronized同步块更复杂. 因为锁(以及其它更高级的线程同步机制)是由synchronized同步块的方式实现的,所以我们还不能完全摆脱synchronized关键字( 译者注:这说的是Java 5之前的情况).
//b 大小写都可以
int a = 0b01111_00000_11111_00000_10101_01010_10;
short b = (short)0b01100_00000_11111_0;
byte c = (byte)0B0000_0001;
public static final int[] phases = {
0b00110001,
0b01100010,
0b11000100,
0b10001001,
0b00010011,
0b00100110,
0b01001100,
0b10011000
}
public static final int[] phases = {
0x31, 0x62, 0xC4, 0x89, 0x13, 0x26, 0x4C, 0x98
}
int num = 1234_5678_9;
float num2 = 222_33F;
long num3 = 123_000_111L;
//下面的不行
//数字开头和结尾
int nu = _123;
int nu = 123_;
//小数点前后
float f = 123_.12;
float f = 123._12;
//F或者L前
long l = 123_L;
float f = 123_F;
//需要出现String的地方
int num = 0_b123;
float f = 0_x123F;
public static void first() {
//项目状态
String status = "approval";
//我们之前经常根据项目状态不同来进行不同的操作
//目前已经换成enum类型
switch (status) {
case "shouli":
System.out.println("状态是受理");
break;
case "approval":
System.out.println("状态是审批");
break;
case "finish":
System.out.println("状态是结束");
break;
default:
System.out.println("状态未知");
}
}
public static void second() {
String status = "approval";
if ("shouli".equals(status)) {
System.out.println("状态是受理");
} else if ("approval".equals(status)) {
System.out.println("状态是审批");
} else if ("finish".equals(status)) {
System.out.println("状态是结束");
} else {
System.out.println("状态未知");
}
}
public static void first();
Code:
0: ldc #2 // String approval
2: astore_0
3: aload_0
4: astore_1
5: iconst_m1
6: istore_2
7: aload_1
8: invokevirtual #3 // Method java/lang/String.hashCode:()I
11: lookupswitch { // 3
-1274442605: 72
-903146056: 44
1185244739: 58
default: 83
}
44: aload_1
45: ldc #4 // String shouli
47: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
50: ifeq 83
53: iconst_0
54: istore_2
55: goto 83
58: aload_1
59: ldc #2 // String approval
61: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
64: ifeq 83
67: iconst_1
68: istore_2
69: goto 83
72: aload_1
73: ldc #6 // String finish
75: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
78: ifeq 83
81: iconst_2
82: istore_2
83: iload_2
84: tableswitch { // 0 to 2
0: 112
1: 123
2: 134
default: 145
}
112: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
115: ldc #8
117: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
120: goto 153
123: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
126: ldc #10
128: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
131: goto 153
134: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
137: ldc #11
139: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
142: goto 153
145: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
148: ldc #12
150: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
153: return
public static void second();
Code:
0: ldc #2 // String approval
2: astore_0
3: ldc #4 // String shouli
5: aload_0
6: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
9: ifeq 23
12: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
15: ldc #8
17: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
20: goto 71
23: ldc #2 // String approval
25: aload_0
26: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
29: ifeq 43
32: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
35: ldc #10
37: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
40: goto 71
43: ldc #6 // String finish
45: aload_0
46: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
49: ifeq 63
52: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
55: ldc
57: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
60: goto 71
63: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
66: ldc #12
68: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
71: return
public static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
try (Closeable obj = new Closeable() {
@Override
public void close() throws IOException {
// do something
}
}) {
// do something
}
try (AutoCloseable obj = new AutoCloseable() {
@Override
public void close() throws IOException {
// do something
}
}) {
// do something
}
public static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}
try (
java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);
java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
) {
// do something
}
public static void first(){
try {
BufferedReader reader = new BufferedReader(new FileReader(""));
Connection con = null;
Statement stmt = con.createStatement();
} catch (IOException | SQLException e) {
//捕获多个异常,e就是final类型的
e.printStackTrace();
}
}
public static void second() {
try {
BufferedReader reader = new BufferedReader(new FileReader(""));
Connection con = null;
Statement stmt = con.createStatement();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void first();
Code:
0: new #2 // class java/io/BufferedReader
3: dup
4: new #3 // class java/io/FileReader
7: dup
8: ldc #4 // String
10: invokespecial #5 // Method java/io/FileReader."<init>":(Ljava/lang/String;)V
13: invokespecial #6 // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;)V
16: astore_0
17: aconst_null
18: astore_1
19: aload_1
20: invokeinterface #7, 1 // InterfaceMethod java/sql/Connection.createStatement:()Ljava/sql/Statement;
25: astore_2
26: goto 34
29: astore_0
30: aload_0
31: invokevirtual #10 // Method java/lang/Exception.printStackTrace:()V
34: return
Exception table:
from to target type
0 26 29 Class java/io/IOException
0 26 29 Class java/sql/SQLException
public static void second();
Code:
0: new #2 // class java/io/BufferedReader
3: dup
4: new #3 // class java/io/FileReader
7: dup
8: ldc #4 // String
10: invokespecial #5 // Method java/io/FileReader."<init>":(Ljava/lang/String;)V
13: invokespecial #6 // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;)V
16: astore_0
17: aconst_null
18: astore_1
19: aload_1
20: invokeinterface #7, 1 // InterfaceMethod java/sql/Connection.createStatement:()Ljava/sql/Statement;
25: astore_2
26: goto 42
29: astore_0
30: aload_0
31: invokevirtual #11 // Method java/io/IOException.printStackTrace:()V
34: goto 42
37: astore_0
38: aload_0
39: invokevirtual #12 // Method java/sql/SQLException.printStackTrace:()V
42: return
Exception table:
from to target type
0 26 29 Class java/io/IOException
0 26 37 Class java/sql/SQLException
static class FirstException extends Exception { }
static class SecondException extends Exception { }
public void rethrowException(String exceptionName) throws Exception {
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
public static void reThrowException(String exceptionName)
throws FirstException, SecondException{
try {
if ("first".equals(exceptionName))
throw new FirstException();
else
throw new SecondException();
} catch (Exception e) {
throw e;
}
}
List<String> list = new ArrayList<String>();
List<String> list = new ArrayList<>();
List<String> list = new ArrayList<>();
list.add("A");
//这个不行
list.addAll(new ArrayList<>());
// 这个可以
List<? extends String> list2 = new ArrayList<>();
list.addAll(list2);