zoukankan      html  css  js  c++  java
  • Java并发编程

    现有线程T1、T2、T3、T4和T5,如何让线程按T1-T5的顺序依次执行。

    1. 使用join()

    public class TestJoin {
    
        static class TestThread extends Thread {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "正在运行");
            }
        }
    
        public static void main(String[] args) {
            Thread t1 = new TestThread();
            Thread t2 = new TestThread();
            Thread t3 = new TestThread();
            Thread t4 = new TestThread();
            Thread t5 = new TestThread();
            try {
                //t1先启动
                t1.start();
                t1.join();
                //t2
                t2.start();
                t2.join();
                //t3
                t3.start();
                t3.join();
                //t4
                t4.start();
                t4.join();
                //t5
                t5.start();
                t5.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    Java 7 Concurrency Cookbook 中对join的相关描述:

    Waiting for the finalization of a thread

    In some situations, we will have to wait for the finalization of a thread. For example, we may have a program that will begin initializing the resources it needs before proceeding with the rest of the execution. We can run the initialization tasks as threads and wait for its finalization before continuing with the rest of the program. For this purpose, we can use the join() method of the Thread class. When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.

    当我们调用某个线程的这个方法时,这个方法会挂起调用线程,直到被调用线程结束执行,调用线程才会继续执行。

    2.使用等待/通知机制

    1.wait/notify实现

    public class TestNotify {
    
        private volatile static int value = 1;
        private static final Object lock = new Object();
    
        private static Thread getThread(int currentValue) {
            return new Thread(() -> {
                try {
                    synchronized (lock) {
                        while (value != currentValue) {
                            lock.wait();
                        }
                        System.out.println(Thread.currentThread().getName() + "正在运行");
                        value = currentValue + 1;
                        lock.notifyAll();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    
        public static void main(String[] args) {
    
            Thread t1 = getThread(1);
            Thread t2 = getThread(2);
            Thread t3 = getThread(3);
            Thread t4 = getThread(4);
            Thread t5 = getThread(5);
    
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
        }
    }

    2.await/signal实现

    public class TestCondition {
    
        volatile private static int value = 1;
    
        private static Lock lock = new ReentrantLock();
        private static Condition condition1 = lock.newCondition();
        private static Condition condition2 = lock.newCondition();
        private static Condition condition3 = lock.newCondition();
        private static Condition condition4 = lock.newCondition();
        private static Condition condition5 = lock.newCondition();
    
        private static Thread setCondition(Condition condition, Condition nextCondition, int currentValue) {
            return new Thread(() -> {
                try {
                    lock.lock();
                    while (value != currentValue) {
                        condition.await();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在运行");
                    value = currentValue + 1;
                    nextCondition.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            });
        }
    
        public static void main(String[] args) {
    
            Thread t1 = setCondition(condition1, condition2, 1);
            Thread t2 = setCondition(condition2, condition3, 2);
            Thread t3 = setCondition(condition3, condition4, 3);
            Thread t4 = setCondition(condition4, condition5, 4);
            Thread t5 = setCondition(condition5, condition1, 5);
    
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
        }
    }

     

  • 相关阅读:
    javascript--运算符
    线程池 的创建小列子,以及参数的介绍
    @SpringBootApplication注解
    SpringBoot 基础知识学习(二)——配置文件多环境配置
    springboot 配置文件读取的两种方式,以及使用到的注解解释
    用虚拟机安装了一台Linux系统,突然想克隆一台服务器,克隆后发现无法上网,如何解决?
    ---oracle 数据库的设计,PL/SQL(loop,for,if,case,while)
    xml的解析技术, 它们之间的区别?
    -----oracle优化之表分区
    --------oracle 的伪表和伪劣,简单的分页
  • 原文地址:https://www.cnblogs.com/helios-fz/p/11216925.html
Copyright © 2011-2022 走看看