通过N个线程顺序循环打印从0至100

时间:2025-04-18 20:23:57

网上看到了一个题目:

  输入N个线程,然后顺序打印数字从0到100,输出结果类似如下:

thread0 value is0
thread1 value is1
thread2 value is2
thread3 value is3
thread0 value is4

 

思路是解决线程的序号,定义一个atomic的原子integer,初始值为0,然后判断序号是否等于当前integer的值,或者integer整除N是否等于当前序号,如果等于,则打印,并将原子integer 自增,然后唤醒其他的线程,否则的话就等待。

只测试了部分值,发现没有问题。

代码如下:

import ;
import ;
import ;

//通过N个线程顺序循环打印从0至100
public class ThreadDemo implements Runnable {

    private static int totalNumber = 100;

    private static AtomicInteger value = new AtomicInteger(0);

    private static int inputNum = 0;

    private static CountDownLatch countDownLatch;

    public static void main(String[] args) {

        Scanner in = new Scanner();
        int threadNum = ();
        inputNum = threadNum;
        countDownLatch = new CountDownLatch(threadNum);
        for (int i = 0; i < threadNum; i++) {
            new Thread(new ThreadDemo(), "thread" + i).start();
            ();
        }
    }


    @Override
    public void run() {
        try {
            ();
            //("begin value is " + ());
            while (() < totalNumber) {
                synchronized (value) {
                    String name = ().getName();
                    int taskThreadNum = ((6, 7));
                    if (() == taskThreadNum || () % inputNum == taskThreadNum) {
                        (name + " value is" + value);
                        ();
                        ();
                    } else {
                        ();
                    }
                }
            }
        } catch (InterruptedException e) {
            ();
        }

    }
}

 

华为Java高级面试题:用两个线程,一个输出字母,一个输出数字,交替输出1A2B3C4D...26Z

代码如下 :

import ;

/**
 * @author: yancun
 * @create: 2020/4/12
 * @description:
 */
public class ThreadDemo {

    private AtomicBoolean flag = new AtomicBoolean(true);

    public static void main(String[] args) throws Exception{
        ThreadDemo demo = new ThreadDemo();

        new Thread( ThreadOne(),"线程1").start();
        new Thread( ThreadTwo(),"线程2").start();
        (200);
        ("结束");

    }

    class ThreadOne implements Runnable {

        public void run() {
           int i = 1;
            while( i < 27) {
                synchronized (flag) {

                    if (()) {
                        (i);
                        i++;
                        (false);
                        ();
                    } else {
                        try {
                            ();
                        } catch (InterruptedException e) {
                            ();
                        }
                    }

                }
            }
        }
    }

    class ThreadTwo implements Runnable {

        private String[] charArray = new String[]{"A","B","C","D"
        ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T"
                ,"U","V","W","X","Y","Z"};

        public void run() {
            int i = 0;
            while( i < 26) {
                synchronized (flag) {

                    if (!()) {
                        (charArray[i]);
                        if (i == 25) {
                            ();
                        }
                        i++;
                        (true);
                        ();
                    } else {
                        try {
                            ();
                        } catch (InterruptedException e) {
                            ();
                        }
                    }

                }
            }

        }
    }

}

使用lock 实现

其实跟上面是一样 就是将synchronized 替换为lock


import ;
import ;
import ;
import ;

/**
 * @author: yancun
 * @create: 2020/4/13 21:42
 * @description:
 */
public class ThreadLockDemo {

    private AtomicBoolean flag = new AtomicBoolean(true);

    private Lock lock = new ReentrantLock();

    private Condition condition = ();

    public static void main(String[] args) throws Exception {
        ThreadLockDemo demo = new ThreadLockDemo();

        new Thread( ThreadOne(), "线程1").start();
        new Thread( ThreadTwo(), "线程2").start();
        (200);
        ("结束");

    }

    class ThreadOne implements Runnable {

        public void run() {
            int i = 1;
            while (i < 27) {
                ();
                if (()) {
                    (i);
                    i++;
                    (false);
                    ();
                    ();
                } else {
                    try {
                        ();
                    } catch (InterruptedException e) {
                        ();
                    } finally {
                        ();
                    }
                }
            }
        }


    }

    class ThreadTwo implements Runnable {

        private String[] charArray = new String[]{"A", "B", "C", "D"
                , "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"
                , "U", "V", "W", "X", "Y", "Z"};

        public void run() {
            int i = 0;
            while (i < 26) {
                ();
                if (!()) {
                    (charArray[i]);
                    if (i == 25) {
                        ();
                    }
                    i++;
                    (true);
                    ();
                    ();
                } else {
                    try {
                        ();
                    } catch (InterruptedException e) {
                        ();
                    } finally {
                        ();
                    }
                }
            }
        }
    }

}