-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo1.java
More file actions
77 lines (65 loc) · 1.69 KB
/
Copy pathThreadDemo1.java
File metadata and controls
77 lines (65 loc) · 1.69 KB
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
75
76
77
package secondPart;
/**
* @author yangxp
* @date 2017年8月3日 下午4:11:13
* <p>
* 调用关键字<i> synchronized </i>声明的方法一定是排队运行的。
* </p>
* <p>
* 关键字 <i>synchronized</i> 取得锁都是对象锁,而不是把一段代码或方法当做锁,哪个线程先执行synchronized关键字的方法
* 哪个线程就持有该方法所属对象的锁lock,其他线程只有等待状态,前提是多个线程访问的是同一个对象。
* </p>
*/
public class ThreadDemo1 {
public static void main(String[] args) {
// HasSelfPrivateNum num2 = new HasSelfPrivateNum();
HasSelfPrivateNum num2 = new HasSelfPrivateNum();
ThreadA aThreadA = new ThreadA(num2);
aThreadA.start();
ThreadB bThreadA = new ThreadB(num2);
bThreadA.start();
}
}
class HasSelfPrivateNum {
private int num = 0;
public void addI(String string) {
// int num = 0;
try {
if (string.equals("a")) {
num = 100;
System.out.println("a set over!");
Thread.sleep(2000);
} else {
num = 200;
System.out.println("b set over!");
}
System.out.println(string + " number = " + num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class ThreadA extends Thread {
private HasSelfPrivateNum number;
public ThreadA(HasSelfPrivateNum number) {
super();
this.number = number;
}
@Override
public void run() {
super.run();
number.addI("a");
}
}
class ThreadB extends Thread {
private HasSelfPrivateNum number;
public ThreadB(HasSelfPrivateNum number) {
super();
this.number = number;
}
@Override
public void run() {
super.run();
number.addI("b");
}
}