JAVA多线程(三个猴子分桃)操作,为什么总是出现一个线程,求大神!
public class ThreadDemo {
int mutex = 100;
boolean isRun = true;
int i = 0;
public static void main(String[] args) {
Monkey m = new ThreadDemo().new Monkey();
new Thread(m, "1").start();
new Thread(m, "2").start();
new Thread(m, "3").start();
}
class Monkey implements Runnable {
@Override
public void run() {
getTao();
}
public void getTao() {
while (isRun) {
if (mutex % 2 == 0&&mutex!=0) {
i = mutex;
mutex = mutex / 2;
System.out.println(Thread.currentThread().getName() + "de"
+ mutex);
mutex = i - mutex;
} else if (mutex % 2 != 0) {
i = mutex;
mutex = mutex / 2 + 1;
System.out.println(Thread.currentThread().getName() + "de"
+ mutex);
mutex = i - mutex;
} else if (mutex == 0) {
isRun = false;
}
}
}
}
}
qq_28076861
2015/08/06 06:36- 多线程
- 点赞
- 收藏
- 回答