【JavaEE】作业回顾——多线程顺序打印

导读:本篇文章讲解 【JavaEE】作业回顾——多线程顺序打印,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1. 顺序打印(初级版)


有三个线程,线程名称分别为:a,b,c。

每个线程打印自己的名称。

需要让他们同时启动,并按 c,b,a的顺序打印


package 顺序打印初级版;

public class Demo {
    public static void main(String[] args) {
        Thread c = new Thread(()->{
            System.out.println(Thread.currentThread().getName()+" "); 
        },"c");
        Thread b = new Thread(()->{
            try {
                c.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" ");
        },"b");
        Thread a = new Thread(()->{
            try {
                b.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" ");
        },"a");
        a.start();
        b.start();
        c.start();
    }
}

【JavaEE】作业回顾——多线程顺序打印


2. 顺序打印(进阶版)


有三个线程,分别只能打印A,B和C

要求按顺序打印ABC,打印10次

输出示例:

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC


package 顺序打印进阶版;

public class Demo {
    private static volatile int COUNTER = 0;
    //计数器
    private static Object lock = new Object();
    //定义一个锁对象


    public static void main(String[] args) {
        //创建三个线程,并指定线程名,每个线程名分别用A,B,C表示
        Thread t1 = new Thread(()->{
            //循环10次
            for (int i = 0; i < 10; i++) {
                //执行的代码加锁
                synchronized (lock) {
                    //每次唤醒后都重新判断是否满足条件
                    //每条线程判断的条件不一样
                    while (COUNTER % 3 != 0) {
                        try {
                            lock.wait();
                            //不满足条件时,主动等待并释放锁
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    //满足条件,打印线程名
                    System.out.print(Thread.currentThread().getName());
                    //累加计数
                    COUNTER++;
                    //唤醒其他线程
                    lock.notifyAll();
                }
            }
        },"A");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock) {
                    while (COUNTER % 3 != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    COUNTER++;
                    lock.notifyAll();
                }
            }
        }, "B");

        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock) {
                    while (COUNTER % 3 != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    // 换行打印
                    System.out.println(Thread.currentThread().getName());
                    COUNTER++;
                    lock.notifyAll();
                }
            }
        }, "C");

        // 启动线程
        t1.start();
        t2.start();
        t3.start();

  }
}

【JavaEE】作业回顾——多线程顺序打印

 上面这个方法使用一个锁来控制线程,我们也可以使用三个所来控制各自线程


public class Demo {
    private static Object locker1 = new Object();
    private static Object locker2 = new Object();
    private static Object locker3 = new Object();
 
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker1) {
                        locker1.wait();
                    }
                    System.out.print("A");
                    synchronized (locker2) {
                        locker2.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        Thread t2 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker2) {
                        locker2.wait();
                    }
                    System.out.print("B");
                    synchronized (locker3) {
                        locker3.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        Thread t3 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker3) {
                        locker3.wait();
                    }
                    System.out.println("C");
                    synchronized (locker1) {
                        locker1.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
        t3.start();
 
        Thread.sleep(1000);
 
        synchronized (locker1) {
            locker1.notify();
        }
    }
}

【JavaEE】作业回顾——多线程顺序打印

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119547.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!