栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java可重入锁示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java可重入锁示例

package Thread2;


public class ReenTranLockDemo {
    private Object currentThread;
    private int lockTimes = 0;
    public synchronized void lock() throws InterruptedException {
        while (Thread.currentThread() == currentThread || currentThread == null || lockTimes > 0) {
            if (Thread.currentThread() == currentThread) {
                //当前线程重复加锁
                lockTimes++;
                System.out.println(1);
                System.out.println(currentThread);
                return;
            } else if (currentThread == null) {
                //当前线程首次加锁
                currentThread = Thread.currentThread();
                lockTimes++;
                System.out.println(2);
                System.out.println(currentThread);
                return;
            } else {
                //锁已被占用,当前线程进入等待
                wait();
                System.out.println(3);
                System.out.println(currentThread);
            }
        }
        System.out.println("lockTime:" + lockTimes);
        return;
    }
    public synchronized void unlock() {
        if (currentThread != null && Thread.currentThread() == currentThread) {
            if (1 == lockTimes) {
                currentThread = null;
                notify();
                System.out.println(4);
                System.out.println(currentThread);
            } else {
                System.out.println(5);
                System.out.println(currentThread);
            }
            lockTimes--;
        }
        System.out.println("lockTime:" + lockTimes);
    }


}
package Thread2;

public class RunReenTranLock {
    private static int a =0;
    public static void main(String[] args) throws InterruptedException {
        ReenTranLockDemo reenTranLockDemo = new ReenTranLockDemo();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    reenTranLockDemo.lock();
                    try {
                        reenTranLockDemo.lock();
                        for (int i = 0; i < 10000; i++) {
                            a++;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        reenTranLockDemo.unlock();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    reenTranLockDemo.unlock();
                }
            }
        });

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    reenTranLockDemo.lock();
                    try {
                        reenTranLockDemo.lock();
                        for (int i = 0; i < 10000; i++) {
                            a++;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        reenTranLockDemo.unlock();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    reenTranLockDemo.unlock();
                }
            }
        });
        thread.start();
        thread1.start();
        thread.join();
        thread1.join();
        System.out.println(a);
    }
}

测试结果

2
Thread[Thread-0,5,main]
1
Thread[Thread-0,5,main]
5
Thread[Thread-0,5,main]
lockTime:1
4
null
lockTime:0
3
null
2
Thread[Thread-1,5,main]
1
Thread[Thread-1,5,main]
5
Thread[Thread-1,5,main]
lockTime:1
4
null
lockTime:0
20000

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/389974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号