线程共享资源问题

b11ght 2009-11-12 12:30:34
大家好,小弟第一次发帖,如果有什么不合适的地方,还请大家多多包涵。

public class ThreadTest {

/**
* @param args
*/
public static void main(String[] args) {

ThreadDemo td = new ThreadDemo();
Thread t = new Thread(td, "thread_test");
Thread t2 = new Thread(td, "thread_test2");
t.start();
t2.start();

}
}

class ThreadDemo implements Runnable {
private int i=0;
public void run() {
for (; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}

我对以上的代码理解是:应该输出100行,两个线程共享一个资源,直到i为100
可是输出如下:
thread_test:0
thread_test:1
thread_test:2
thread_test:3
thread_test:4
thread_test:5
thread_test:6
thread_test:7
thread_test:8
thread_test:9
thread_test:10
thread_test:11
thread_test:12
thread_test:13
thread_test:14
thread_test:15
thread_test:16
thread_test:17
thread_test:18
thread_test:19
thread_test:20
thread_test:21
thread_test:22
thread_test:23
thread_test:24
thread_test:25
thread_test:26
thread_test:27
thread_test:28
thread_test:29
thread_test:30
thread_test:31
thread_test:32
thread_test:33
thread_test:34
thread_test:35
thread_test:36
thread_test:37
thread_test:38
thread_test:39
thread_test:40
thread_test:41
thread_test:42
thread_test:43
thread_test:44
thread_test:45
thread_test:46
thread_test:47
thread_test:48
thread_test:49
thread_test:50
thread_test:51
thread_test:52
thread_test:53
thread_test:54
thread_test:55
thread_test:56
thread_test:57
thread_test:58
thread_test:59
thread_test:60
thread_test:61
thread_test:62
thread_test:63
thread_test:64
thread_test:65
thread_test:66
thread_test:67
thread_test:68
thread_test:69
thread_test:70
thread_test:71
thread_test:72
thread_test:73
thread_test:74
thread_test:75
thread_test:76
thread_test:77
thread_test:78
thread_test:79
thread_test:80
thread_test:81
thread_test:82
thread_test:83
thread_test:84
thread_test:85
thread_test:86
thread_test:87
thread_test:88
thread_test:89
thread_test:90
thread_test:91
thread_test:92
thread_test:93
thread_test:94
thread_test:95
thread_test:96
thread_test:97
thread_test:98
thread_test:99
thread_test2:32
我想请问一下大家,最后一个数据 thread_test2:32是哪里来的,如果是两个线程共享的话,顶多交替进行,可是32输出了两遍,是在是不理解。请大家帮我看看。我是线程的初学者,请大家解释的清楚点,谢谢哦。
...全文
55 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
b11ght 2009-11-12
  • 打赏
  • 举报
回复
撑不住了,睡了。。。。兄弟们也早点睡吧哈。
b11ght 2009-11-12
  • 打赏
  • 举报
回复
大哥,你回复的真效率,谢谢。我明白了,多谢。
一头头 2009-11-12
  • 打赏
  • 举报
回复
还有什么不明白的问题都放上来吧

我也线程初学者 一起可以讨论下哇
一头头 2009-11-12
  • 打赏
  • 举报
回复
32是因为没有同步 所以你的这个资源被thread1&&thread2在某一刻同时占有
一头头 2009-11-12
  • 打赏
  • 举报
回复
没有同步 仅此而已


加一个同步锁就好了




public class ThreadTest {

/**
* @param args
*/
public static void main(String[] args) {

ThreadDemo td = new ThreadDemo();
Thread t = new Thread(td, "thread_test");
Thread t2 = new Thread(td, "thread_test2");
t.start();
t2.start();

}
}

class ThreadDemo implements Runnable {
private int i = 0;

public void run() {
synchronized (this) {

for (; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧