使用多线程打印字母和数字

时间:2018-10-23 12:33:08

标签: java multithreading synchronization thread-synchronization

我刚刚开始学习线程并对其进行了新的尝试。我试图一个接一个地打印字母和数字。我已经使用一个标志将它们同步,但是没有用。

公共类字母{

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();
}

}

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 ....

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

由于我无法同步这两个线程,因此有人可以指出错误吗?

3 个答案:

答案 0 :(得分:0)

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

SELECT
     thestatus, 
     COUNT(DISTINCT thestatus),
     SUM(thecost)
FROM THETABLE
GROUP BY thestatus

答案 1 :(得分:0)

好吧,您想要的是或多或少的流水线,因此线程需要知道何时允许它们工作。

因此,我将使用Queue等待一个线程上的输入,并等待在另一个线程中设置它。

AN类:

import java.util.concurrent.BlockingQueue;

class AN
{

    BlockingQueue<Boolean> input;

    BlockingQueue<Boolean> output;

    AN(BlockingQueue<Boolean> input, BlockingQueue<Boolean> output)
    {

        this.input = input;
        this.output = output;
    }

    void Alpha() throws InterruptedException
    {
        for (char i = 'a'; i <= 'z'; i++) {
            input.take();
            System.out.print(i);
            output.put(Boolean.TRUE);

        }
    }

    void numbers() throws InterruptedException
    {
        for (int i = 1; i <= 26; i++) {
            input.take();
            System.out.print(i);
            output.put(Boolean.TRUE);
        }

    }

}

Class Test(或您的主要测试对象):

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Test
{

    public static void main(String[] args) throws InterruptedException
    {

        BlockingQueue<Boolean> input = new LinkedBlockingQueue<>();
        BlockingQueue<Boolean> output = new LinkedBlockingQueue<>();

        AN an1 = new AN(output, input);
        AN an2 = new AN(input, output);
        output.add(Boolean.TRUE);

        Thread t1 = new Thread(new Runnable()
        {

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

        Thread t2 = new Thread(new Runnable()
        {

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

            }
        });

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

    }
}

输出:a1b2c3d4e5f6g7h8i9j10k11l12m13n14o15p16q17r18s19t20u21v22w23x24y25z26

答案 2 :(得分:0)

您可以通过使用BlockingQueue和Object的wait和notify方法来实现。

public class AlphaNum {
    public static void main(String[] args) throws InterruptedException {


    BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);

    AtomicBoolean flag = new AtomicBoolean(Boolean.TRUE);

    Object lock = new Object();

    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                for(int i=1;i<=26;i++){
                        synchronized (lock){
                            while (flag.get()){
                                lock.wait();
                            }
                            System.out.print(i);
                            flag.set(Boolean.TRUE);
                            lock.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    });

    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                for(char c='A';c<='Z';c++){
                    synchronized (lock){
                        while (!flag.get()){
                            lock.wait();
                        }
                        System.out.print(c);
                        flag.set(Boolean.FALSE);
                        lock.notify();
                    }
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    });


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

    }
}

上面的代码显示a1b2c3d4 ....... z26