作者SST2000 (BMW 428i)
看板java
标题[问题] 关於JAVA synchronized的问题?
时间Mon May 11 08:23:56 2020
请问各位,下面这个范例程式为什麽输出只会有线程0执行??
ackage Thread02;
public class main {
public static void main(String[] args) {
Mythread02 T2=new Mythread02();
Thread t1=new Thread(T2);
Thread t2=new Thread(T2);
Thread t3=new Thread(T2);
t1.start();
t2.start();
t3.start();
}
下面是宣告的class
package Thread02;
public class Mythread02 implements Runnable {
private int number=100;
public void run() {
synchronized (this) {
try {
Thread.sleep(100);
//---------------------------------------------------------------//
while (true) {
if (number > 0) {
System.out.println("This is Mythread02 " +
Thread.currentThread().getName() + "---" + number--);
} else {
break;
}
}
//---------------------------------------------------------------//]
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
----------------------------下面是输出结果-------------------------
This is Mythread02 Thread-0---100
This is Mythread02 Thread-0---99
This is Mythread02 Thread-0---98
This is Mythread02 Thread-0---97
This is Mythread02 Thread-0---96
This is Mythread02 Thread-0---95
This is Mythread02 Thread-0---94
This is Mythread02 Thread-0---93
This is Mythread02 Thread-0---92
This is Mythread02 Thread-0---91
This is Mythread02 Thread-0---90
This is Mythread02 Thread-0---89
This is Mythread02 Thread-0---88
This is Mythread02 Thread-0---87
This is Mythread02 Thread-0---86
This is Mythread02 Thread-0---85
This is Mythread02 Thread-0---84
This is Mythread02 Thread-0---83
This is Mythread02 Thread-0---82
This is Mythread02 Thread-0---81
This is Mythread02 Thread-0---80
This is Mythread02 Thread-0---79
This is Mythread02 Thread-0---78
This is Mythread02 Thread-0---77
This is Mythread02 Thread-0---76
This is Mythread02 Thread-0---75
This is Mythread02 Thread-0---74
This is Mythread02 Thread-0---73
This is Mythread02 Thread-0---72
This is Mythread02 Thread-0---71
This is Mythread02 Thread-0---70
This is Mythread02 Thread-0---69
This is Mythread02 Thread-0---68
This is Mythread02 Thread-0---67
This is Mythread02 Thread-0---66
This is Mythread02 Thread-0---65
This is Mythread02 Thread-0---64
This is Mythread02 Thread-0---63
This is Mythread02 Thread-0---62
This is Mythread02 Thread-0---61
This is Mythread02 Thread-0---60
This is Mythread02 Thread-0---59
This is Mythread02 Thread-0---58
This is Mythread02 Thread-0---57
This is Mythread02 Thread-0---56
This is Mythread02 Thread-0---55
This is Mythread02 Thread-0---54
This is Mythread02 Thread-0---53
This is Mythread02 Thread-0---52
This is Mythread02 Thread-0---51
This is Mythread02 Thread-0---50
This is Mythread02 Thread-0---49
This is Mythread02 Thread-0---48
This is Mythread02 Thread-0---47
This is Mythread02 Thread-0---46
This is Mythread02 Thread-0---45
This is Mythread02 Thread-0---44
This is Mythread02 Thread-0---43
This is Mythread02 Thread-0---42
This is Mythread02 Thread-0---41
This is Mythread02 Thread-0---40
This is Mythread02 Thread-0---39
This is Mythread02 Thread-0---38
This is Mythread02 Thread-0---37
This is Mythread02 Thread-0---36
This is Mythread02 Thread-0---35
This is Mythread02 Thread-0---34
This is Mythread02 Thread-0---33
This is Mythread02 Thread-0---32
This is Mythread02 Thread-0---31
This is Mythread02 Thread-0---30
This is Mythread02 Thread-0---29
This is Mythread02 Thread-0---28
This is Mythread02 Thread-0---27
This is Mythread02 Thread-0---26
This is Mythread02 Thread-0---25
This is Mythread02 Thread-0---24
This is Mythread02 Thread-0---23
This is Mythread02 Thread-0---22
This is Mythread02 Thread-0---21
This is Mythread02 Thread-0---20
This is Mythread02 Thread-0---19
This is Mythread02 Thread-0---18
This is Mythread02 Thread-0---17
This is Mythread02 Thread-0---16
This is Mythread02 Thread-0---15
This is Mythread02 Thread-0---14
This is Mythread02 Thread-0---13
This is Mythread02 Thread-0---12
This is Mythread02 Thread-0---11
This is Mythread02 Thread-0---10
This is Mythread02 Thread-0---9
This is Mythread02 Thread-0---8
This is Mythread02 Thread-0---7
This is Mythread02 Thread-0---6
This is Mythread02 Thread-0---5
This is Mythread02 Thread-0---4
This is Mythread02 Thread-0---3
This is Mythread02 Thread-0---2
This is Mythread02 Thread-0---1
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.250.186.57 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1589156638.A.C6F.html
1F:→ charlie0275: 在 main 主执行绪加上sleep多等几秒看看 05/11 10:02
2F:→ LZN: 因为synchronized (this)的关系 05/11 11:34
3F:→ ssccg: 在synchronized里面放sleep并不会把lock释放 05/11 14:09
4F:→ ssccg: 另外以你的写法是抢到的Thread就会一次跑完,如果你是要模 05/11 14:18
5F:→ ssccg: 拟每个数字都重抢一次,Thread中断应该放在回圈里吧 05/11 14:19
6F:→ ssccg: while (true) { 05/11 14:23
7F:→ ssccg: synchronized (this) { 05/11 14:23
8F:→ ssccg: if (...) {...} else { break; } 05/11 14:24
9F:→ ssccg: } 05/11 14:24
10F:→ ssccg: Thread.sleep(100); 05/11 14:24
11F:→ ssccg: } 05/11 14:24
12F:→ ssccg: 我猜你想做的效果是这样 05/11 14:26
13F:→ ssccg: 如果一定要synchonized在外面,要用wait/notify来控制lock 05/11 14:27