java 板


LINE

请问各位,下面这个范例程式为什麽输出只会有线程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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP