两个线程,一个线程打印1-52,另一个打印A-Z,打印顺序为12A34B56C....5152Z.

/*
两个线程,一个线程打印1-52,另一个打印A-Z,打印顺序为12A34B56C....5152Z.
 */
public class PrintTwo {
    public static void main(String[] args) {
        Print print = new Print();
        IntThread intThread = new IntThread(print);
        LetterThread letterThread = new LetterThread(print);
        intThread.start();
        letterThread.start();
    }
}

class IntThread extends Thread {
    private Print print;

    public IntThread(Print print) {
        this.print = print;
    }

    @Override
    public void run() {
        print.printInt();
    }
}

class LetterThread extends Thread {
    private Print print;

    public LetterThread(Print print) {
        this.print = print;
    }

    @Override
    public void run() {
        print.printLetter();
    }
}

class Print {
    private boolean flag = true;

    public synchronized void printInt() {
        int i = 1;
        while (i < 53) {
            if (!flag) {
                try {
                    wait();
                    flag = true;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else {
                System.out.print(i);
                i++;
                if((i-1)%2==0){
                    flag = false;
                    notify();
                }
            }
        }
    }
    public synchronized void printLetter() {
        char i = 'A';
        while (i <= 'Z') {
            if (flag) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else {
                System.out.print(i);
                i++;
                flag = true;
                notify();
                }
            }
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用`wait()`和`notify()`方法来实现线程的同步,具体代码实现如下: ``` public class PrintNumberAndLetter { private static final Object lock = new Object(); private static int count = 1; private static char letter = 'A'; public static void main(String[] args) { Thread thread1 = new Thread(() -> { synchronized (lock) { while (count <= 52) { System.out.print(count++); System.out.print(count++); lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } }); Thread thread2 = new Thread(() -> { synchronized (lock) { while (letter <= 'Z') { System.out.print(letter++); lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } }); thread1.start(); thread2.start(); } } ``` 在`Thread1`中,通过`synchronized`关键字锁住了共享对象`lock`。在循环中,先打印两个数字,然后通过`notify()`方法通知`Thread2`线程可以执行,自己则等待。当`Thread2`线程执行完毕后,通过`wait()`方法释放锁,`Thread1`线程重新获取锁并继续执行。 在`Thread2`中,同样通过`synchronized`关键字锁住共享对象`lock`。在循环中,每次打印一个字母,然后通过`notify()`方法通知`Thread1`线程可以执行,自己则等待。当`Thread1`线程执行完毕后,通过`wait()`方法释放锁,`Thread2`线程重新获取锁并继续执行。 通过这种方式,实现了两个线程的同步,按照题目要求依次打印数字和字母。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值