qq_28076861 2015-08-06 06:36 采纳率: 0%
浏览 1898

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;

            }
        }
    }
}

}

  • 写回答

2条回答 默认 最新

  • Tiger_Zhao 2015-08-06 06:51
    关注

    你m只有一个实例!
    通常new Thread(new ...)都是每个线程单独创建一个实例的。

    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员