A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 范明正 黑马帝   /  2011-8-6 09:49  /  1942 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

为什么执行不出结果,哪出现了问题呢?

public class ThreadMain {
        public static void main(String args[]) {
                Q q = new Q();
                new Thread(new Producer(q)).start();
                new Thread(new Cousumer(q)).start();
        }
}

class Producer implements Runnable {
        Q q;

        public Producer(Q q) {
                super();
                this.q = q;
        }

        @Override
        public void run() {
                int i = 0;
                while (true) {
                        if (i == 0) {
                                q.put("zheng", "male");
                        } else {
                                q.put("zhao", "female");
                        }
                        i = (i + 1) % 2;
                }
        }

}

class Q {
        private String name = "unkown";
        private String sex = "unkown";
        private boolean bFull = false;

        public synchronized void put(String name, String sex) {
                if (bFull) {
                        try {
                                wait();
                        } catch (Exception e) {
                                // TODO: handle exception
                        }
                        this.name = name;
                        try {
                                Thread.sleep(1);
                        } catch (Exception e) {
                                // TODO: handle exception
                        }
                        this.sex = sex;
                        bFull = true;
                        notify();
                }
        }

        public synchronized void get() {
                if (!bFull) {
                        try {
                                wait();
                        } catch (Exception e) {
                                // TODO: handle exception
                        }
                        System.out.println(name);
                        System.out.println(":" + sex);
                        bFull = false;
                        notify();
                }
        }
}

class Cousumer implements Runnable {
        Q q;

        public Cousumer(Q q) {
                super();
                this.q = q;
        }

        @Override
        public void run() {
                while (true) {
                        q.get();
                }
        }

}

6 个回复

倒序浏览
黑马网友  发表于 2011-8-7 16:15:28
沙发

线程问题

使用Thread类创建线程对象时语句应该是这样的吧!
Thread thread = new Thread();
thread.start();
回复 使用道具 举报
黑马网友  发表于 2011-8-7 16:17:49
藤椅

回复 沙发 的帖子

Q q = new Q();
new Thread(new Producer(q)).start();
这样跟你说的不是一样的吗,只是形式不太一样而已。
回复 使用道具 举报
黑马网友  发表于 2011-8-7 16:24:28
板凳
多线程操作一般情况下不需要接受用户的指令.
回复 使用道具 举报
黑马网友  发表于 2011-8-8 11:56:32
报纸
我认真读了你的程序,程序让人看起来有点晕。为什么得不到结果,大概你没有认真读你的程序吧。
你的程序运行过程是这样的:
有两个线程,一个生产者,一个消费者。
Cousumer线程执行到get方法中的wait(),会wait住,也就是睡死过去,要等到其他线程调用notify()方法才会醒过来。
Producer线程执行put方法时,由于bFull是false,所以if语句内容是不会执行的。
由于Cousumer线程没有得到其它线程调用notfiy()方法,所以后面代码肯定得不到执行,并且Producer线程bFull一直是false,所以if语句的里的notfiy方法一直得不到执行,故两个线程一直僵在那里。
回复 使用道具 举报
黑马网友  发表于 2011-8-8 12:55:39
地板
楼上说的很对,昨天时间仓促,没看清楚,今天仔细看了一下,你是让线程每sleep(1)然后打印zheng:mage和zhao:femate吧!
看看下面的代码如何,是不是输出你想要的结果:

public class ThreadMain {
    public static void main(String [] args){
            Q q = new Q();
            new Thread(new Producer(q)).start();
            new Thread(new Cousumer(q)).start();       
}
class Q {
           private String name = "unknown";
           private String sex = "unknown";
           private boolean bFull = false;
           //使用同步方法
           public synchronized void put(String name,String sex){
                   notify();// 线程重新运行
                       
                        this.name = name;
                        try {
                                Thread.sleep(1);
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        this.sex = sex;
                        bFull = true;
                        try {
                                wait();// 线程中断运行
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }          
        public synchronized void get() {
                notify();

                System.out.println(name);
                System.out.println(":" + sex);
                bFull = false;
                try {
                        wait();
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
          
        }
class Producer implements Runnable {

         Q q;
    public Producer(Q q){
            super();
            this.q = q;
    }
    //覆盖public void run()方法;
        public void run() {
                // TODO Auto-generated method stub
     
     int i=0;
     while(true){
             if(i==0){
                     q.put("zheng","mate");
             }else{
                     q.put("zhao","femate");
             }
             i = (i+1)%2;
     }
        }
}
class Cousumer implements Runnable {
    Q q;
    public Cousumer(Q q){
            super();
            this.q=q;
    }
        public void run() {
                // TODO Auto-generated method stub
                while(true){
                q.get();
                }
               
        }

}
回复 使用道具 举报
黑马网友  发表于 2011-8-8 14:05:36
7#
这问题我昨天看了半天也看不出毛病,原来是这样子啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马