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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 再见亦是泪 中级黑马   /  2013-1-18 20:05  /  1403 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package ThreadLearn;

class Res
{
        static String Name = "李斯";
        static String Sex = "男";
        static boolean flag = false;
        static Object obj = new Object();
}
class Input implements Runnable
{
        public void run()
        {
               
                for(int x=0; x < 10; x++)
                {
                        synchronized(Res.obj)
                        {
                          if(Res.flag)       
                          {
                                  try{Res.obj.wait();}catch(Exception e){}
                          }
                      if(x%2 == 0)
                      {
                               Res.Name = "麦克。。。。";
                               Res.Sex = "M男。。。。。。。。。";
                      }
                      else
                      {
                             Res.Name = "Lily";
                             Res.Sex = "Female";
                      }
                      Res.flag = false;
                      Res.obj.notifyAll();
                        }
                }
        }
}
class Output implements Runnable
{
        public void run()
        {
                synchronized(Res.obj)
                {
                  for(int i = 0; i < 10 ; i++)
                  {
                         if(!Res.flag)
                         {
                                 try{Res.obj.wait();}catch(Exception e){}
                         }
                       System.out.println(Res.Name+"....."+Res.Sex);
                       Res.flag = true;
                       Res.obj.notifyAll();
                  }
                }
        }
}
public class ThreadLearn3
{

        public static void main(String args[])
        {
                Input input = new Input();
                Output output = new Output();
               
                Thread t1 = new Thread(input);
                Thread t2 = new Thread(output);
               
                t1.start();
                t2.start();
        }
}

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1 神马都是浮云

查看全部评分

5 个回复

倒序浏览
我要每个名字轮翻打一次,如这样写就只能一个名字打出来 不能交替 何解
回复 使用道具 举报
本帖最后由 黑马唐贤来 于 2013-1-18 20:27 编辑
  1. //同学你好,我修改你的代码后实现了你的要求

  2. class Res
  3. {
  4.         static String Name = "李斯";
  5.         static String Sex = "男";
  6.         static boolean flag = false;
  7.         static Object obj = new Object();
  8. }
  9. class Input implements Runnable
  10. {
  11.         public void run()
  12.         {
  13.                
  14.                 for(int x=0; x < 10; x++)
  15.                 {
  16.                         synchronized(Res.obj)
  17.                         {
  18.                           //if(Res.flag)
  19.                                 while(Res.flag)        //这里应该使用while(),这样可以使线程每次被唤醒后都会去检查Res.flag值
  20.                           {
  21.                                   try{Res.obj.wait();}catch(Exception e){}
  22.                           }
  23.                       if(x%2 == 0)
  24.                       {
  25.                                Res.Name = "麦克。。。。";
  26.                                Res.Sex = "M男。。。。。。。。。";
  27.                       }
  28.                       else
  29.                       {
  30.                              Res.Name = "Lily";
  31.                              Res.Sex = "Female";
  32.                       }
  33.                       Res.flag = true;                               //这里修改为true,因为必须要让这个线程执行一遍后wait
  34.                       Res.obj.notifyAll();
  35.                         }
  36.                 }
  37.         }
  38. }
  39. class Output implements Runnable
  40. {
  41.         public void run()
  42.         {
  43.                 synchronized(Res.obj)
  44.                 {
  45.                   for(int i = 0; i < 10 ; i++)
  46.                   {
  47.                          //if(!Res.flag)
  48.                           while(!Res.flag)                                                            //这里使用(!Res.flag)取反来调节线程运行
  49.                          {
  50.                                  try{Res.obj.wait();}catch(Exception e){}
  51.                          }
  52.                        System.out.println(Res.Name+"....."+Res.Sex);
  53.                        Res.flag = false;
  54.                        Res.obj.notifyAll();
  55.                   }
  56.                 }
  57.         }
  58. }
  59. public class Demo2
  60. {

  61.         public static void main(String args[])
  62.         {
  63.                 Input input = new Input();
  64.                 Output output = new Output();
  65.                
  66.                 Thread t1 = new Thread(input);
  67.                 Thread t2 = new Thread(output);
  68.                
  69.                 t1.start();
  70.                 t2.start();
  71.         }
  72. }
  73. 希望共同努力,加油
复制代码

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1 赞一个!

查看全部评分

回复 使用道具 举报
//同学你好,我修改你的代码后实现了你的要求

class Res

{

        static String Name = "李斯";

        static String Sex = "男";

        static boolean flag = false;

        static Object obj = new Object();

}

class Input implements Runnable

{

        public void run()

        {

               
                for(int x=0; x < 10; x++)

                {

                        synchronized(Res.obj)

                        {

                          //if(Res.flag)

                                while(Res.flag)        //这里应该使用while(),这样可以使线程每次被唤醒后都会去检查Res.flag值

                          {

                                  try{Res.obj.wait();}catch(Exception e){}

                          }

                      if(x%2 == 0)

                      {

                               Res.Name = "麦克。。。。";

                               Res.Sex = "M男。。。。。。。。。";

                      }

                      else

                      {

                             Res.Name = "Lily";

                             Res.Sex = "Female";

                      }

                      Res.flag = true;                               //这里修改为true,因为必须要让这个线程执行一遍后wait

                      Res.obj.notifyAll();

                        }

                }

        }

}

class Output implements Runnable

{

        public void run()

        {

                synchronized(Res.obj)

                {

                  for(int i = 0; i < 10 ; i++)

                  {

                         //if(!Res.flag)

                          while(!Res.flag)                                                            //这里使用(!Res.flag)取反来调节线程运行

                         {

                                 try{Res.obj.wait();}catch(Exception e){}

                         }

                       System.out.println(Res.Name+"....."+Res.Sex);

                       Res.flag = false;

                       Res.obj.notifyAll();

                  }

                }

        }

}

public class Demo2

{

        public static void main(String args[])

        {

                Input input = new Input();

                Output output = new Output();

               
                Thread t1 = new Thread(input);

                Thread t2 = new Thread(output);

               
                t1.start();

                t2.start();

        }

}
写while循环进行判断是因为线程等待后在此被唤醒会继续它上次的操作继续运行,这样会出现问题,
循环的在让这个线程在判断一次就不会出先错误了,
赋值的任务中没赋值完应该让标识该成true然后判断到true是该线程等待,让输出的线程来执行,输
出线程执行完后在将标识符该为false,在去唤醒等待的赋值线程,如此交替进行,就可以实现你想要
达到的目的了


评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1 山寨

查看全部评分

回复 使用道具 举报
谢谢,共同努力
回复 使用道具 举报
  1. class Res
  2. {
  3.         private        String name        = "李斯";
  4.         private        String Sex = "男";
  5.         private        boolean        flag = false;
  6.         public synchronized void set(String name , String Sex)
  7.         {       
  8.                 while(flag)
  9.                         try{this.wait();}catch(Exception e){};
  10.                 this.name = name;
  11.                 this.Sex = Sex;
  12.                 flag = true;
  13.                 this.notify();
  14.         }

  15.         public synchronized void out ()
  16.         {
  17.                 while(!flag)
  18.                         try{this.wait();}catch(Exception e){};
  19.                 System.out.println(name+"....."+Sex);
  20.                 flag = false;
  21.                 this.notify();
  22.         }
  23. }

  24. class Input implements Runnable
  25. {               
  26.         private Res r;
  27.         Input(Res r)
  28.         {
  29.                 this.r=r;
  30.         }
  31.         public void run()
  32.         {
  33.                 for(int x=0; x < 10; x++)
  34.                 {
  35.                         if(x%2 == 0)
  36.                                 r.set("麦克。。。。","M男。。。。。。。。。");
  37.                         else
  38.                                 r.set("Lily","Female");
  39.                      
  40.                 }
  41.         }
  42. }
  43. class Output implements Runnable
  44. {       
  45.         private Res r;
  46.         Output(Res r)
  47.         {
  48.                 this.r=r;
  49.         }
  50.         public void run()
  51.         {
  52.                 for(int i = 0; i < 10 ; i++)
  53.                         r.out();
  54.         }
  55. }
  56. public class Demo08
  57. {

  58.         public static void main(String args[])
  59.         {
  60.                                 Res r = new Res();
  61.                 Input input = new Input(r);
  62.                 Output output = new Output(r);
  63.                
  64.                 Thread t1 = new Thread(input);
  65.                 Thread t2 = new Thread(output);
  66.                
  67.                 t1.start();
  68.                 t2.start();
  69.         }
  70. }
复制代码

th1111.jpg (14.64 KB, 下载次数: 24)

th1111.jpg
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马