Java编程练习之线程

Java线程线程

1.创建三个线程:一个线程打印 100个A,一个线程打印 100 个 B ,一个线程打印 100个C 输出效果:ABC ABC ABC…交替

public class PrintABC {
    private static int count = 1;
    private static final Object lock = new Object();

    // 打印 A 的线程
    public void printA() {
        for (int i = 0; i < 100; i++) {
            synchronized (lock) {
                while (count != 1) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        return;
                    }
                }
                System.out.print("A");
                count = (count % 3) + 1; // 更新为下一个应该打印的字符
                lock.notifyAll(); // 唤醒其他线程
            }
        }
    }

    // 打印 B 的线程
    public void printB() {
        for (int i = 0; i < 100; i++) {
            synchronized (lock) {
                while (count != 2) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        return;
                    }
                }
                System.out.print("B");
                count = (count % 3) + 1; // 更新为下一个应该打印的字符
                lock.notifyAll(); // 唤醒其他线程
            }
        }
    }

    // 打印 C 的线程
    public void printC() {
        for (int i = 0; i < 100; i++) {
            synchronized (lock) {
                while (count != 3) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        return;
                    }
                }
                System.out.print("C");
                count = (count % 3) + 1; // 更新为下一个应该打印的字符
                lock.notifyAll(); // 唤醒其他线程
            }
        }
    }
}
public class PrintABCTest {
    public static void main(String[] args) {
        PrintABC p = new PrintABC();

        Thread threadA = new Thread(p::printA);
        Thread threadB = new Thread(p::printB);
        Thread threadC = new Thread(p::printC);

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

2.需求:编写两个线程,一个 线程打印1-52,另一个线程打印字母A-Z打印顺序是12A34B…,即按照整数和字母的顺序从小到大打印,并且每打印两个整数后,打印一个字母,交替循环打印

打印数字

public class Number implements Runnable{
    private final Object lock; //同步锁对象
    private int number;

    public Number(Object lock, int startNumber) {
        this.lock = lock;
        this.number = number;
    }

    @Override
    public void run() {
        synchronized (lock) {
            while (number <= 52) { // 循环打印数字直到 52
                System.out.print(number++);
                System.out.print(number++);
                try {
                    lock.notify(); // 唤醒等待的字母线程
                    if (number <= 52) {
                        lock.wait(); // 等待字母线程打印完毕
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 确保字母线程能够结束
            lock.notify();
        }
    }
}

打印字母

public class Letter implements Runnable{
    private final Object lock; //同步锁对象
    private char letter;

    public Letter(Object lock, char startLetter) {
        this.lock = lock;
        this.letter = letter;
    }

    @Override
    public void run() {
        synchronized (lock) {
            while (letter <= 'Z') { // 循环打印字母直到 'Z'
                System.out.print(letter++);
                try {
                    lock.notify(); // 唤醒等待的数字线程
                    if (letter <= 'Z') {
                        lock.wait(); // 等待数字线程打印完毕
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 确保数字线程能够结束
            lock.notify();
        }
    }
}

测试代码

public class Test {
        public static void main(String[] args)  throws Exception{
            // 创建一个共享的同步锁对象
            Object lock = new Object();

            // 创建数字线程,传入同步锁对象和起始数字
            Thread numberThread = new Thread(new Number(lock,1));
            // 创建字母线程,传入同步锁对象和起始字母
            Thread letterThread = new Thread(new Letter(lock,'A'));

            // 启动数字线程和字母线程,开始执行
            numberThread.start();
            letterThread.start();
        }
}

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值