62,614
社区成员
发帖
与我相关
我的任务
分享
public class EvenCheckerEasy implements Runnable {
private static int val = 0;
@Override
public void run() {
while (true) {
++val;
++val;
System.out.println(val); // 在我电脑上测试,此处添加打印输出似乎防止了并发的产生,为什么?
if (val % 2 != 0) {
System.out.println(val + " not event!");
return;
}
}
}
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
exec.execute(new EvenCheckerEasy());
}
exec.shutdown();
}
}
public void run() {
while (true) {
++val;
++val;
synchronized (System.out) {//注释掉这行测试
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}//注释掉这行测试
if (val % 2 != 0) {
System.out.println(val + " not event!");
return;
}
}
}
当原理很明确的时候,出现奇怪现象,你只要逐行看看你的代码,你自己就能想清楚为什么出现这个情况。[/quote]
good,解释的很清楚!
public void run() {
while (true) {
++val;
++val;
synchronized (System.out) {//注释掉这行测试
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}//注释掉这行测试
if (val % 2 != 0) {
System.out.println(val + " not event!");
return;
}
}
}
当原理很明确的时候,出现奇怪现象,你只要逐行看看你的代码,你自己就能想清楚为什么出现这个情况。private static int val = 0;
private int i = 10;
@Override
public void run() {
while (i-- > 0) {
++val;
++val;
System.out.println(Thread.currentThread().getId() + ":" + val); // 在我电脑上测试,此处添加打印输出似乎防止了并发的产生,为什么?
if (val % 2 != 0) {
System.out.println(val + " not event!");
return;
}
}
}
得到的结果是
10:10
10:68
10:70
10:72
10:74
10:76
10:78
10:8
10:80
10:84
11:116
11:118
11:120
11:122
11:124
11:26
11:28
11:30
11:34
11:8
12:152
12:156
12:168
12:170
12:174
12:178
12:182
12:186
12:190
12:90
13:114
13:126
13:128
13:130
13:132
13:162
13:32
13:36
13:38
13:40
14:22
14:50
14:52
14:54
14:56
14:58
14:60
14:62
14:64
14:66
15:100
15:102
15:104
15:106
15:108
15:110
15:112
15:46
15:96
15:98
16:136
16:138
16:140
16:142
16:144
16:146
16:148
16:150
16:158
16:92
17:154
17:172
17:180
17:188
17:192
17:194
17:196
17:198
17:200
17:82
8:134
8:160
8:164
8:166
8:176
8:184
8:52
8:86
8:90
8:94
9:12
9:14
9:16
9:18
9:20
9:24
9:42
9:46
9:48
9:8
可以看出线程间还是交叉运行的,val++的操作都执行完了,我觉得可能是每个线程都在等待IO的调用,所以获得数都是偶数,这个应该跟线程与IO调用关系有关
public void println(int x) {
synchronized (this) {
print(x);
newLine();
}
}
public void run() {
while (true) {
++val;
++val;
// System.out.println(val);
synchronized (System.out) {
}
if (val % 2 != 0) {
System.out.println(val + " not event!");
return;
}
}
}
我可以这么理解吗?还是不行啊!private static int val = 0;
private int i = 10;
@Override
public void run() {
while (i-- > 0) {
++val;
++val;
System.out.println(Thread.currentThread().getId() + ":" + val); // 在我电脑上测试,此处添加打印输出似乎防止了并发的产生,为什么?
if (val % 2 != 0) {
System.out.println(val + " not event!");
return;
}
}
}
得到的结果是
9:8
可以看出线程间还是交叉运行的,val++的操作都执行完了,我觉得可能是每个线程都在等待IO的调用,所以获得数都是偶数,这个应该跟线程与IO调用关系有关[/quote]
能说的更详细点吗?线程与IO调用有什么关系呢?private static int val = 0;
private int i = 10;
@Override
public void run() {
while (i-- > 0) {
++val;
++val;
System.out.println(Thread.currentThread().getId() + ":" + val); // 在我电脑上测试,此处添加打印输出似乎防止了并发的产生,为什么?
if (val % 2 != 0) {
System.out.println(val + " not event!");
return;
}
}
}
得到的结果是
10:10
10:68
10:70
10:72
10:74
10:76
10:78
10:8
10:80
10:84
11:116
11:118
11:120
11:122
11:124
11:26
11:28
11:30
11:34
11:8
12:152
12:156
12:168
12:170
12:174
12:178
12:182
12:186
12:190
12:90
13:114
13:126
13:128
13:130
13:132
13:162
13:32
13:36
13:38
13:40
14:22
14:50
14:52
14:54
14:56
14:58
14:60
14:62
14:64
14:66
15:100
15:102
15:104
15:106
15:108
15:110
15:112
15:46
15:96
15:98
16:136
16:138
16:140
16:142
16:144
16:146
16:148
16:150
16:158
16:92
17:154
17:172
17:180
17:188
17:192
17:194
17:196
17:198
17:200
17:82
8:134
8:160
8:164
8:166
8:176
8:184
8:52
8:86
8:90
8:94
9:12
9:14
9:16
9:18
9:20
9:24
9:42
9:46
9:48
9:8
可以看出线程间还是交叉运行的,val++的操作都执行完了,我觉得可能是每个线程都在等待IO的调用,所以获得数都是偶数,这个应该跟线程与IO调用关系有关[/quote]
我认为13楼说的是对的。