Skip to content

Commit e2afc50

Browse files
committed
update comment
1 parent 8b5e859 commit e2afc50

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

03concurrency/0301/src/main/java/java0/conc0301/op/Join.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package java0.conc0301.op;
22

3+
/**
4+
* 总结:线程A结束的时候,JVM内部会发送通知 notifyAll 给该线程对象A上的等待线程,唤醒在线程对象A上等待的线程
5+
*/
36
public class Join {
47

58
public static void main(String[] args) {
69
Object oo = new Object();
710

811
MyThread thread1 = new MyThread("thread1 -- ");
9-
//oo = thread1;
12+
oo = thread1;
1013
thread1.setOo(oo);
1114
thread1.start();
1215

@@ -42,10 +45,12 @@ public MyThread(String name) {
4245

4346
@Override
4447
public void run() {
45-
synchronized (oo) { // 这里用oo或this,效果不同
48+
synchronized (this) { // 这里用oo或this,效果不同
4649
for (int i = 0; i < 100; i++) {
4750
System.out.println(name + i);
4851
}
52+
//my Test: add notifyAll to solve dead lock
53+
//oo.notifyAll();
4954
}
5055
}
5156

03concurrency/0301/src/main/java/java0/conc0301/op/WaitAndNotify.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public static void main(String[] args) {
3737
class MethodClass {
3838
// 定义生产最大量
3939
private final int MAX_COUNT = 20;
40-
40+
41+
/**
42+
* 单线程创建 product, 不需要加锁
43+
*/
4144
int productCount = 0;
4245

4346
public synchronized void product() throws InterruptedException {

03concurrency/0301/src/main/java/java0/conc0301/sync/Counter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package java0.conc0301.sync;
22

3+
/**
4+
* volatile 只能保证有序性和可见性,不能保证原子性
5+
*/
36
public class Counter {
47

58
public final static int A=10;

03concurrency/0301/src/main/java/java0/conc0301/sync/Thread1.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11

22
package java0.conc0301.sync;
33

4+
5+
/**
6+
* Thread 线程对象中的任意方法获取的 this 代表线程对象本身,并不代表线程信息(线程信息中包括操作系统线程)
7+
* Thread.currentThread() 代表操作系统对应的线程信息.
8+
* 所以 this != Thread.currentThread() ,this 代表 Task 对象信息
9+
* 如下所示:TA 线程和 TB线程在操作系统上创建了两个线程对应 A线程 和 B线程,但是它们公用一个Task 对象信息, this是 task 对象信息
10+
*/
411
public class Thread1 implements Runnable {
512

613
@Override
714
public void run() {
815
synchronized (this) {
16+
System.out.println(this);
917
for (int i = 0; i < 10; i++) {
1018
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
1119
}
@@ -14,8 +22,10 @@ public void run() {
1422

1523
public static void main(String[] args) {
1624
Thread1 t1 = new Thread1();
25+
System.out.println("the Thread1 obj t1:" + t1);
1726
Thread ta = new Thread(t1, "A");
1827
Thread tb = new Thread(t1, "B");
28+
1929
ta.start();
2030
tb.start();
2131
}

0 commit comments

Comments
 (0)