使用多线程打印字母和数字 [英] Printing Alphabets and Numbers using multi-threading

查看:34
本文介绍了使用多线程打印字母和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习线程并且对它很陌生.我正在尝试一个接一个地打印字母和数字.我使用标志将它们同步但没有用.

I have just started learning threads and pretty new to it.I'm trying to print alphabets and numbers one after the other.I have synchronized them using a flag but of no use.

public class Alphabets {

    public static void main(String[] args) {

              AN an= new AN(false);

              Thread t1=new Thread(new Runnable() {

                @Override
                public void run() {

                    try {

                        an.Alpha();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });


              Thread t2= new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        an.numbers();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            });


              t1.start();
              t2.start();
    }

}

...

 class AN
    {

        boolean flag;

        AN(boolean flag)
        {
            this.flag=flag;
        }
        synchronized void Alpha() throws InterruptedException
        {
            if(flag==false)
            {
            for(char i='A'; i<='Z';i++)
            {
                System.out.println(+i);
                notifyAll();
                flag=true;
            }
            }
            else
            {
                wait();
            }

        }

        synchronized void numbers() throws InterruptedException
        {
            if(flag==true)
            {
            for(int i=1;i<=26;i++)
            {
                System.out.println(+i);
                notifyAll();
                flag=false;
            }
            }
            else
            {
                wait();
            }
        }


    }

我想要的输出是:a1b2c3d4....

My desired output is : a1b2c3d4....

我的控制台输出是:abcd...1234...

My console output is : abcd...1234...

谁能指出错误,因为我无法同步这两个线程.

Can anybody point out the mistake since I'm unable to synchronize these two threads.

推荐答案

更改类 AN 以检查 while 循环中的标志.

Change class AN to check the flag in while loop.

public class AN {
  boolean flag;

  AN(boolean flag) {
    this.flag = flag;
  }

  synchronized void Alpha() throws InterruptedException {

    for(char i = 'A'; i <= 'Z'; i++) {
      while(flag == true) {
        wait();
      }
      System.out.println(i);
      notifyAll();
      flag = true;
    }
  }

  synchronized void numbers() throws InterruptedException {

    for(int i = 1; i <= 26; i++) {
      while(flag == false) {
        wait();
      }
      System.out.println(i);
      notifyAll();
      flag = false;
    }
  }

这篇关于使用多线程打印字母和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆