两个线程,一个线程打印1-52,另一个打印字母A-Z打印顺序为12A34B...5152Z, 要求用线程间通信

1 篇文章 0 订阅
1 篇文章 0 订阅

first boke

package com.atguigu.juc.FutureTaskTest;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**

  • 用一个lock去锁不同的方法,每一condition完成以后,修改对应的条件变量,再唤醒另外需要执行的代码,
  • 注意在finally里面
  • @author yhfstart
  • @create 2018-12-28 0:43

*/
public class PrintTest {

public static void main(String[] args) {


   PrintTest1 printTest1 = new printTest1();

    new Thread((()->{
        printTest1.printA_Z();
    }),"打印A-Z线程").start();

    new Thread((()->{
        printTest1.print1_52();
    }),"打印1-52线程").start();


}

}

class PrintTest1 {

private static int num = 1;

public final Lock lock = new ReentrantLock();

public final Condition printA_ZCondition = lock.newCondition();
public final Condition print1_52Condition = lock.newCondition();

//打印数字的方法
public void print1_52() {
    lock.lock();
    try {
        for (int i = 1; i < 53; i++) {
            while (num%3==0){
                try {
                    print1_52Condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println(Thread.currentThread().getName()+"=====>"+i);

            num++;



            printA_ZCondition.signal();

        }
    } finally {

        lock.unlock();
    }


}

//打印字母的方法
public void printA_Z() {

    lock.lock();

    try {
        for (char i = 'A'; i < 'Z'; i++) {
            //打印字母的方法

            while (num%3!=0){
                try {
                    printA_ZCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println(Thread.currentThread().getName()+"=====+>"+i);
            //改条件,唤醒下一个线程
            num++;

            print1_52Condition.signal();

        }
    } finally {
        lock.unlock();
    }


}

}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用`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`线程重新获取锁并继续执行。 通过这种方式,实现了两个线程的同步,按照题目要求依次打印数字和字母

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值