Java面试题(2) 两个线程,交替打印100次A和B

目录

    • 一、实现方式一
      • 实现思路:
      • 代码实现:
      • 执行结果:
    • 二、实现方式二
      • 实现思路:
      • 代码实现:
      • 执行结果:
    • 三、思考

一、实现方式一

实现思路:

Java面试题(2) 两个线程,交替打印100次A和B_第1张图片

代码实现:

ThreadTest.java

package com.demo.example;

public class ThreadTest {

    public static void main(String[] args) {
        MyThread threadA = new MyThread();
        MyThread threadB = new MyThread();

        threadA.setThread(threadB);
        threadA.setKey("A");
        threadB.setThread(threadA);
        threadB.setKey("B");

        threadA.start();
        threadB.start();
    }

}

class MyThread extends Thread {

    private final Object lock = new Object();
    private MyThread thread;
    private String key;

    public void setThread(MyThread thread) {
        this.thread = thread;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public void getLock() {
        synchronized (lock) {
            try {
                lock.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void releaseLock() {
        synchronized (lock) {
            lock.notify();
        }
    }

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            System.out.println(Thread.currentThread().getName() + ", " + key + ",count: " + (i + 1));
            thread.releaseLock();
            if (i < 49) {
                this.getLock();
            }
        }
    }
}

执行结果:

Thread-0, A,count: 1
Thread-1, B,count: 1
Thread-0, A,count: 2
Thread-1, B,count: 2
Thread-0, A,count: 3
Thread-1, B,count: 3
Thread-0, A,count: 4
Thread-1, B,count: 4
Thread-0, A,count: 5
Thread-1, B,count: 5
Thread-0, A,count: 6
Thread-1, B,count: 6
Thread-0, A,count: 7
Thread-1, B,count: 7
Thread-0, A,count: 8
Thread-1, B,count: 8
Thread-0, A,count: 9
Thread-1, B,count: 9
Thread-0, A,count: 10
Thread-1, B,count: 10
Thread-0, A,count: 11
Thread-1, B,count: 11
Thread-0, A,count: 12
Thread-1, B,count: 12
Thread-0, A,count: 13
Thread-1, B,count: 13
Thread-0, A,count: 14
Thread-1, B,count: 14
Thread-0, A,count: 15
Thread-1, B,count: 15
Thread-0, A,count: 16
Thread-1, B,count: 16
Thread-0, A,count: 17
Thread-1, B,count: 17
Thread-0, A,count: 18
Thread-1, B,count: 18
Thread-0, A,count: 19
Thread-1, B,count: 19
Thread-0, A,count: 20
Thread-1, B,count: 20
Thread-0, A,count: 21
Thread-1, B,count: 21
Thread-0, A,count: 22
Thread-1, B,count: 22
Thread-0, A,count: 23
Thread-1, B,count: 23
Thread-0, A,count: 24
Thread-1, B,count: 24
Thread-0, A,count: 25
Thread-1, B,count: 25
Thread-0, A,count: 26
Thread-1, B,count: 26
Thread-0, A,count: 27
Thread-1, B,count: 27
Thread-0, A,count: 28
Thread-1, B,count: 28
Thread-0, A,count: 29
Thread-1, B,count: 29
Thread-0, A,count: 30
Thread-1, B,count: 30
Thread-0, A,count: 31
Thread-1, B,count: 31
Thread-0, A,count: 32
Thread-1, B,count: 32
Thread-0, A,count: 33
Thread-1, B,count: 33
Thread-0, A,count: 34
Thread-1, B,count: 34
Thread-0, A,count: 35
Thread-1, B,count: 35
Thread-0, A,count: 36
Thread-1, B,count: 36
Thread-0, A,count: 37
Thread-1, B,count: 37
Thread-0, A,count: 38
Thread-1, B,count: 38
Thread-0, A,count: 39
Thread-1, B,count: 39
Thread-0, A,count: 40
Thread-1, B,count: 40
Thread-0, A,count: 41
Thread-1, B,count: 41
Thread-0, A,count: 42
Thread-1, B,count: 42
Thread-0, A,count: 43
Thread-1, B,count: 43
Thread-0, A,count: 44
Thread-1, B,count: 44
Thread-0, A,count: 45
Thread-1, B,count: 45
Thread-0, A,count: 46
Thread-1, B,count: 46
Thread-0, A,count: 47
Thread-1, B,count: 47
Thread-0, A,count: 48
Thread-1, B,count: 48
Thread-0, A,count: 49
Thread-1, B,count: 49
Thread-0, A,count: 50
Thread-1, B,count: 50

二、实现方式二

实现思路:

Java面试题(2) 两个线程,交替打印100次A和B_第2张图片

代码实现:

ThreadTest.java

package com.demo.example;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTest {

    public static void main(String[] args) {
        // 写法一:显示创建Thread
        Runnable runnable = new AbThread();
        Thread threadA = new Thread(runnable);
        Thread threadB = new Thread(runnable);
        threadA.start();
        threadB.start();
        // 写法二:使用线程池
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        executorService.execute(runnable);
//        executorService.execute(runnable);
    }

}

class AbThread implements Runnable {

    char[] content = {'A', 'B'};
    int count = 0;

    @Override
    public void run() {
        synchronized (this) {
            try {
                while(count < 100) {
                    System.out.println(Thread.currentThread().getName() + ", " + content[count % 2] + ",count: " + (++count));
                    notify();
                    if (count < 100) {
                        wait();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

执行结果:

Thread-0, A,count: 1
Thread-1, B,count: 2
Thread-0, A,count: 3
Thread-1, B,count: 4
Thread-0, A,count: 5
Thread-1, B,count: 6
Thread-0, A,count: 7
Thread-1, B,count: 8
Thread-0, A,count: 9
Thread-1, B,count: 10
Thread-0, A,count: 11
Thread-1, B,count: 12
Thread-0, A,count: 13
Thread-1, B,count: 14
Thread-0, A,count: 15
Thread-1, B,count: 16
Thread-0, A,count: 17
Thread-1, B,count: 18
Thread-0, A,count: 19
Thread-1, B,count: 20
Thread-0, A,count: 21
Thread-1, B,count: 22
Thread-0, A,count: 23
Thread-1, B,count: 24
Thread-0, A,count: 25
Thread-1, B,count: 26
Thread-0, A,count: 27
Thread-1, B,count: 28
Thread-0, A,count: 29
Thread-1, B,count: 30
Thread-0, A,count: 31
Thread-1, B,count: 32
Thread-0, A,count: 33
Thread-1, B,count: 34
Thread-0, A,count: 35
Thread-1, B,count: 36
Thread-0, A,count: 37
Thread-1, B,count: 38
Thread-0, A,count: 39
Thread-1, B,count: 40
Thread-0, A,count: 41
Thread-1, B,count: 42
Thread-0, A,count: 43
Thread-1, B,count: 44
Thread-0, A,count: 45
Thread-1, B,count: 46
Thread-0, A,count: 47
Thread-1, B,count: 48
Thread-0, A,count: 49
Thread-1, B,count: 50
Thread-0, A,count: 51
Thread-1, B,count: 52
Thread-0, A,count: 53
Thread-1, B,count: 54
Thread-0, A,count: 55
Thread-1, B,count: 56
Thread-0, A,count: 57
Thread-1, B,count: 58
Thread-0, A,count: 59
Thread-1, B,count: 60
Thread-0, A,count: 61
Thread-1, B,count: 62
Thread-0, A,count: 63
Thread-1, B,count: 64
Thread-0, A,count: 65
Thread-1, B,count: 66
Thread-0, A,count: 67
Thread-1, B,count: 68
Thread-0, A,count: 69
Thread-1, B,count: 70
Thread-0, A,count: 71
Thread-1, B,count: 72
Thread-0, A,count: 73
Thread-1, B,count: 74
Thread-0, A,count: 75
Thread-1, B,count: 76
Thread-0, A,count: 77
Thread-1, B,count: 78
Thread-0, A,count: 79
Thread-1, B,count: 80
Thread-0, A,count: 81
Thread-1, B,count: 82
Thread-0, A,count: 83
Thread-1, B,count: 84
Thread-0, A,count: 85
Thread-1, B,count: 86
Thread-0, A,count: 87
Thread-1, B,count: 88
Thread-0, A,count: 89
Thread-1, B,count: 90
Thread-0, A,count: 91
Thread-1, B,count: 92
Thread-0, A,count: 93
Thread-1, B,count: 94
Thread-0, A,count: 95
Thread-1, B,count: 96
Thread-0, A,count: 97
Thread-1, B,count: 98
Thread-0, A,count: 99
Thread-1, B,count: 100

三、思考

如果是三个线程,交替打印100次ABC,怎么实现?





参考地址:

1.两个线程交替打印100次AB,https://blog.csdn.net/qq_34707456/article/details/103027820

2.两个线程交替打印1-100的奇数和偶数,https://blog.csdn.net/bobo1356/article/details/103408137

3.线程交替输出0-100,https://blog.csdn.net/zhangxiao123qqq/article/details/112842059

你可能感兴趣的:(面试,java,面试,开发语言)