/
class217.java
74 lines (60 loc) · 2.11 KB
/
class217.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.jweihao.jdemo.java.book.Thread.zhang2;
/**
* Created by weihao on 2018/4/13.
*/
public class class217 {
public static void main(String[] args) {
// 程序运行:Exception in thread "a" java.lang.NumberFormatException: For input string: "a"
// 线程a出现异常并释放锁,线程b进入方法正常打印,实现结论就是出现异常的锁被自动释放了。
try {
Service service = new Service();
ThreadA a = new ThreadA(service);
a.setName("a");
a.start();
Thread.sleep(500);
ThreadB b = new ThreadB(service);
b.setName("b");
b.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static class Service {
synchronized public void testMethod() {
if (Thread.currentThread().getName().equals("a")) {
System.out.println("ThreadName=" + Thread.currentThread().getName() + " run beginTime=" + System.currentTimeMillis());
int i = 1;
while (i == 1) {
if (("" + Math.random()).substring(0, 8).equals("0.123456")) {
System.out.println("ThreadName=" + Thread.currentThread().getName() + " run exceptionTime=" + System.currentTimeMillis());
Integer.parseInt("a");
}
}
} else {
System.out.println("Thread B run Time=" + System.currentTimeMillis());
}
}
}
public static class ThreadA extends Thread {
private Service mService;
public ThreadA(Service service) {
super();
mService = service;
}
@Override
public void run() {
mService.testMethod();
}
}
public static class ThreadB extends Thread {
private Service mService;
public ThreadB(Service service) {
super();
mService = service;
}
@Override
public void run() {
mService.testMethod();
}
}
}