• 线程交互


     

    使用synchronized方式进行线程交互,用到的是同步对象的wait,notify和notifyAll方法

    Lock也提供了类似的解决办法,首先通过lock对象得到一个Condition对象,然后分别调用这个Condition对象的:await, signal,signalAll 方法

    注意: 不是Condition对象的wait,nofity,notifyAll方法,是await,signal,signalAll
    线程交互
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    package multiplethread;
      
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
      
    public class TestThread {
      
        public static String now() {
            return new SimpleDateFormat("HH:mm:ss").format(new Date());
        }
      
        public static void log(String msg) {
            System.out.printf("%s %s %s %n", now() , Thread.currentThread().getName() , msg);
        }
      
        public static void main(String[] args) {
            Lock lock = new ReentrantLock();
            Condition condition = lock.newCondition();
             
            Thread t1 = new Thread() {
                public void run() {
                    try {
                        log("线程启动");
                        log("试图占有对象:lock");
      
                        lock.lock();
      
                        log("占有对象:lock");
                        log("进行5秒的业务操作");
                        Thread.sleep(5000);
                        log("临时释放对象 lock, 并等待");
                        condition.await();
                        log("重新占有对象 lock,并进行5秒的业务操作");
                        Thread.sleep(5000);
      
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    finally {
                        log("释放对象:lock");
                        lock.unlock();
                    }
                    log("线程结束");
                }
            };
            t1.setName("t1");
            t1.start();
            try {
                //先让t1飞2秒
                Thread.sleep(2000);
            catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Thread t2 = new Thread() {
      
                public void run() {
                    try {
                        log("线程启动");
                        log("试图占有对象:lock");
      
                        lock.lock();
      
                        log("占有对象:lock");
                        log("进行5秒的业务操作");
                        Thread.sleep(5000);
                        log("唤醒等待中的线程");
                        condition.signal();
      
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    finally {
                        log("释放对象:lock");
                        lock.unlock();
                    }
                    log("线程结束");
                }
            };
            t2.setName("t2");
            t2.start();
        }
      
    }
  • 相关阅读:
    iOS重签名及问题总结
    安装class-dump
    UISearchController 很坑
    hashmap 之哈希冲突
    wait()与sleep()的区别
    docker之es+es-head+kibana+ik分词器安装
    MySQL很有用的命令
    分布式事务执行逻辑
    索引 创建原则
    合理使用存储引擎
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10193939.html
Copyright © 2020-2023  润新知