Topic: 一个线程代码的分析,大家给看看。 |
Print this page |
1.一个线程代码的分析,大家给看看。 | Copy to clipboard |
Posted by: 小雨 Posted on: 2004-02-29 12:52 class A extends Thread { String[] sa; public A(String[] sa) {this.sa = sa;} public void run() { synchronized (sa) { System.out.print(sa[0] + sa[1] + sa[2]); }}} public class B { private static String[] sa = new String[]{"X","Y","Z"}; public static void main (String[] args) { synchronized (sa) { Thread t1 = new A(sa); t1.start(); sa[0] = "A"; sa[1] = "B"; sa[2] = "C"; }}} 大家看看我的这段程序代码后,帮忙看看我的分析对不对。 Thread t1 = new A(sa);是将 private static String[] sa作参数传递给A的构造函数。 当ti启动线程的时候,发现sa已经加锁,于是得等到sa运行结束也就是: sa[0] = "A"; sa[1] = "B"; sa[2] = "C";这段代码运行后,解锁后。在运行t1.start() 也就是run方法得同步部分,这样就输出“ABC”了。 希望高手们给小弟看看,拜托,拜托。线程真的很难理解雅! |
2.Re:一个线程代码的分析,大家给看看。 [Re: 小雨] | Copy to clipboard |
Posted by: mochow Posted on: 2004-03-01 10:09 建议还是先找些相关的线程书看看,理解线程的一些基本概念再说。 |
3.Re:一个线程代码的分析,大家给看看。 [Re: 小雨] | Copy to clipboard |
Posted by: 独孤牧 Posted on: 2004-03-01 14:36 又是一个碰到问题各个论坛问遍的家伙,嘿嘿 |
4.Re:一个线程代码的分析,大家给看看。 [Re: 小雨] | Copy to clipboard |
Posted by: 小雨 Posted on: 2004-03-01 19:39 无语....... |
5.Re:一个线程代码的分析,大家给看看。 [Re: 小雨] | Copy to clipboard |
Posted by: 独孤牧 Posted on: 2004-03-11 01:04 http://www.chinajavaworld.net/forum/topic.cgi?forum=21&topic=15996 兄弟,帮你ding~~~ |
6.Re:一个线程代码的分析,大家给看看。 [Re: 小雨] | Copy to clipboard |
Posted by: dearmeiw Posted on: 2004-03-13 03:42 怎么又看到了你! |
7.Re:一个线程代码的分析,大家给看看。 [Re: 小雨] | Copy to clipboard |
Posted by: wsjcert Posted on: 2004-06-01 03:05 you can have a try of this modification: ....... t1.start(); try { t1.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } sa[0] = "A"; sa[1] = "B"; sa[2] = "C"; ........ Guess you already found the answer. |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |