java 并发编程Lock

1、ReetrantLock基本使用方法


package com.k6k4.java.examples.lock;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by k6k4
 */
public class ReetrantLockExample {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    static class MyThread extends Thread {
        Lock lock;

        public MyThread(Lock lock) {
            this.lock = lock;
        }

        public void run() {
            try {
                lock.lock();
                System.out.println("[Thread]" + Thread.currentThread().getName() + "[Time]" + sdf.format(new Date()));
                Thread.sleep(3000);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Thread threadA = new MyThread(lock);
        Thread threadB = new MyThread(lock);
        threadA.setName("A");
        threadB.setName("B");
        threadA.start();
        threadB.start();

    }

}

输出结果:

[Thread]A[Time]2017-01-22 12:54:24
[Thread]B[Time]2017-01-22 12:54:27

2、Condition实现等待通知


package com.k6k4.java.examples.lock;

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;

/**
 * Created by k6k4
 */
public class ReetrantLockExample {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    static class MyThread extends Thread {
        Lock lock;

        Condition condition;

        String type;

        public MyThread(Lock lock, Condition condition, String type) {
            this.lock = lock;
            this.condition = condition;
            this.type = type;
        }

        public void run() {
            if ("wait".equals(type)) {
                waitMethod();
            } else {
                signalMethod();
            }
        }

        private void waitMethod() {
            try {
                lock.lock();
                System.out.println("[BeforeWait]" + Thread.currentThread().getName() + "[Time]" + sdf.format(new Date
                        ()));
                condition.await();
                System.out.println("[AfterWait]" + Thread.currentThread().getName() + "[Time]" + sdf.format(new Date
                        ()));
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        private void signalMethod() {
            try {
                lock.lock();
                System.out.println("[BeforeSignal][Thread]" + Thread.currentThread().getName() + "[Time]" + sdf.format
                        (new Date()));
                condition.signal();
                System.out.println("[AfterSignal][Thread]" + Thread.currentThread().getName() + "[Time]" + sdf.format
                        (new
                                Date()));
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        Thread threadA = new MyThread(lock,condition,"wait");
        Thread threadB = new MyThread(lock,condition,"signal");
        threadA.setName("A");
        threadB.setName("B");
        threadA.start();
        Thread.sleep(1000);
        threadB.start();

    }

}


输出结果:


[BeforeWait]A[Time]2017-01-22 12:51:08
[BeforeSignal][Thread]B[Time]2017-01-22 12:51:09
[AfterSignal][Thread]B[Time]2017-01-22 12:51:09
[AfterWait]A[Time]2017-01-22 12:51:09


3、

个人资料
时海
等级:8
文章:272篇
访问:16.0w
排名: 2
上一篇: 图数据库Titan学习笔记
下一篇:秒杀系统架构优化思路
标签: lock、condition、thread、mythread、sdf、面试题
隐藏