Java多线程Lock对象之ReentrantLock(1)
来源:互联网 发布:ubuntu之间共享文件夹 编辑:程序博客网 时间:2024/04/29 17:31
今天本来是学习lock对象的,刚才想到多线程数量的问题,因此验证了一下,现在来记录下lock对象的学习。Lock对象是Java提供的比synchronized更为灵活,功能更为强大的锁,今天先学习一个简单的例子来说明lock锁的使用。ReentrantLock对象是lock的具体类,其作用是可以给特定的代码段加锁来支持多线程的同步功能。
共享类:
package com.lenovo.plm.dms.p14;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class Service { private Lock lock = new ReentrantLock(); public void serviceA(){ lock.lock(); for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName() + " for serviceA" + i +" at the time: "+System.currentTimeMillis()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } lock.unlock(); } public void serviceB(){ lock.lock(); for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName() + " for serviceB" + i +" at the time: "+System.currentTimeMillis()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } lock.unlock(); }}
线程类A:
package com.lenovo.plm.dms.p14;public class MyThreadA extends Thread{ private Service service; public MyThreadA(Service service){ this.service = service; } @Override public void run() { // TODO Auto-generated method stub super.run(); service.serviceA(); } }
线程类B:
package com.lenovo.plm.dms.p14;public class MyThreadB extends Thread{ private Service service; public MyThreadB(Service service){ this.service = service; } @Override public void run() { // TODO Auto-generated method stub super.run(); service.serviceB(); }}
执行类:
package com.lenovo.plm.dms.p14;public class Main { public static void main(String[] args) { Service service = new Service(); MyThreadA t1 = new MyThreadA(service); t1.start(); MyThreadB t2 = new MyThreadB(service); t2.start(); }}
执行结果如下:
Thread-0 for serviceA0 at the time: 1456141744196
Thread-0 for serviceA1 at the time: 1456141745210
Thread-0 for serviceA2 at the time: 1456141746225
Thread-0 for serviceA3 at the time: 1456141747240
Thread-0 for serviceA4 at the time: 1456141748255
Thread-1 for serviceB0 at the time: 1456141749262
Thread-1 for serviceB1 at the time: 1456141750277
Thread-1 for serviceB2 at the time: 1456141751292
Thread-1 for serviceB3 at the time: 1456141752299
Thread-1 for serviceB4 at the time: 1456141753314
可以看出这个lock代码段中的代码都是同步执行的,而且一个类中有两段被lock加锁的代码,两个线程来调用的时候是互斥的。其作用和synchronized差不多。还有更好玩的功能再下次来记录。
另外,这个Lock可以在一个类中创建两个,那表示两个锁。这两个锁是不互斥的。
package com.lenovo.plm.dms.p14;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class Service { private Lock lock = new ReentrantLock(); private Lock lock1 = new ReentrantLock(); public void serviceA(){ lock.lock(); for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName() + " for serviceA" + i +" at the time: "+System.nanoTime()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } lock.unlock(); } public void serviceB(){ lock1.lock(); for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName() + " for serviceB" + i +" at the time: "+System.nanoTime()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } lock1.unlock(); } }
再执行一下,结果如下:
Thread-0 for serviceA0 at the time: 13563172925425
Thread-1 for serviceB0 at the time: 13563173792462
Thread-1 for serviceB1 at the time: 13564183972447
Thread-0 for serviceA1 at the time: 13564184079990
Thread-0 for serviceA2 at the time: 13565202499725
Thread-1 for serviceB2 at the time: 13565203119993
Thread-0 for serviceA3 at the time: 13566217150104
Thread-1 for serviceB3 at the time: 13566217381254
Thread-0 for serviceA4 at the time: 13567230966020
Thread-1 for serviceB4 at the time: 13567231148085
Thread-0 for serviceA5 at the time: 13568245225496
Thread-1 for serviceB5 at the time: 13568245403098
Thread-1 for serviceB6 at the time: 13569259249359
Thread-0 for serviceA6 at the time: 13569260112380
Thread-1 for serviceB7 at the time: 13570275512435
Thread-0 for serviceA7 at the time: 13570275867639
Thread-1 for serviceB8 at the time: 13571290609049
Thread-0 for serviceA8 at the time: 13571290906689
Thread-0 for serviceA9 at the time: 13572303837273
Thread-1 for serviceB9 at the time: 13572304199617
Thread-0 for serviceA10 at the time: 13573319750054
Thread-1 for serviceB10 at the time: 13573320099010
Thread-0 for serviceA11 at the time: 13574334486556
Thread-1 for serviceB11 at the time: 13574334676206
Thread-0 for serviceA12 at the time: 13575349161031
Thread-1 for serviceB12 at the time: 13575349333724
Thread-0 for serviceA13 at the time: 13576364402672
Thread-1 for serviceB13 at the time: 13576364504860
Thread-0 for serviceA14 at the time: 13577379099013
Thread-1 for serviceB14 at the time: 13577379278846
Thread-0 for serviceA15 at the time: 13578394283090
Thread-1 for serviceB15 at the time: 13578394400896
Thread-0 for serviceA16 at the time: 13579408411372
Thread-1 for serviceB16 at the time: 13579408543012
Thread-0 for serviceA17 at the time: 13580424221964
Thread-1 for serviceB17 at the time: 13580424419201
Thread-1 for serviceB18 at the time: 13581438904026
Thread-0 for serviceA18 at the time: 13581442124066
Thread-1 for serviceB19 at the time: 13582454761919
Thread-0 for serviceA19 at the time: 13582454761919
Thread-1 for serviceB20 at the time: 13583469173561
Thread-0 for serviceA20 at the time: 13583469362319
Thread-1 for serviceB21 at the time: 13584483877042
Thread-0 for serviceA21 at the time: 13584483937730
Thread-1 for serviceB22 at the time: 13585499216409
Thread-0 for serviceA22 at the time: 13585499689865
Thread-1 for serviceB23 at the time: 13586514341136
Thread-0 for serviceA23 at the time: 13586514423690
在i=19的时候,两个线程是同时执行的。
- Java多线程Lock对象之ReentrantLock(1)
- Java多线程Lock对象之ReentrantLock(2)
- Java多线程之~~~Lock接口和ReentrantLock的使用
- JAVA多线程系列--Lock锁-ReentrantLock之Condition应用
- Java多线程之ReentrantLock
- Java多线程Lock对象之读写锁
- java多线程学习之ReentrantLock
- java多线程之重入锁ReentrantLock
- Java多线程Lock对象常用方法(1)
- ReentrantLock使用详解(1)之lock/unlock
- 多线程编程之Lock(ReentrantLock)的使用
- 分析ReentrantLock之lock
- Java多线程(五) ReentrantLock、Lock和Condition的用法
- JAVA多线程-Lock的使用(一)-ReentrantLock与Condition
- Java多线程编程4--Lock的使用--重入锁(ReentrantLock)、Condition
- Java 多线程(2):ReentrantLock以及lock()、lockInterruptibly()之间的区别
- java多线程学习8-显示锁Lock和ReentrantLock
- JAVA多线程系列--Lock锁-ReentrantLock,ReentrantReadWriteLock应用
- c#学习——arraylist集合
- C++11和Boost库
- NYOJ353--三维监狱-搜索应用
- 手动删除Weblogic域的方法
- java 以行为单位读取txt文件(1)
- Java多线程Lock对象之ReentrantLock(1)
- 单调队列入门
- 图解Nginx-Nginx进程模型1
- 蓝桥杯--猴子选大王-约瑟夫问题
- 1097. Deduplication on a Linked List (25)
- cp命令和cp-r的区别-----Python学习中的小贴士E01
- Pop
- 让Tux逃离虚拟世界
- 消息摘要 —— MD5算法