java ReentrantLock重入锁的实现
2016-06-20 11:03
495 查看
最近正好空,研究了下JAVA并发包,具体大致如下
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test {
private ArrayList arrayList = new ArrayList();
public static Lock lock = new ReentrantLock();
public static Lock lock1 = new ReentrantLock();
public static void main(String[] args) {
final Test test = new Test();
final ArrayList arr = new ArrayList();
arr.add("a");
arr.add("b");
arr.add("c");
arr.add("d");
arr.add("e");
new Thread() {
public void run() {
test.delete(Thread.currentThread());
};
}.start();
new Thread() {
public void run() {
test.insert(Thread.currentThread(), arr);
};
}.start();
}
public void insert(Thread thread,ArrayList arr) {
// if (lock.tryLock()) {
try {
lock.lock();
System.out.println(thread.getName() + "得到了锁");
// Thread.sleep(5000);
for (int i = 0; i < 5; i++) {
if(i==3){
Thread.sleep(1000);
}
arrayList.add(arr.get(i).toString());
}
for (int j = 0; j < arrayList.size(); j++) {
System.out.println(thread.getName() + ":"
+ arrayList.get(j).toString() + "length:"
+ arrayList.size());
}
} catch (Exception e) {
// TODO: handle exception
} finally {
System.out.println(thread.getName() + "释放了锁");
lock.unlock();
}
// }
}
public void delete(Thread thread) {
//
// if (lock.tryLock()) {
try {
lock1.lock();
System.out.println(thread.getName() + "得到了锁1");
if(arrayList.size()<1){
Thread.sleep(1000);//防止arrayList为空
System.out.println(arrayList.get(0).toString());
arrayList.remove(0);
}
for (int i = 0; i < 10; i++) {
if (i == 8) {
System.out.println("ok");
}
}
} catch (Exception e) {
System.out.print("报错了");
// TODO: handle exception
} finally {
System.out.println(thread.getName() + "释放了锁1");
lock1.unlock();
}
}
// }
}
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test {
private ArrayList arrayList = new ArrayList();
public static Lock lock = new ReentrantLock();
public static Lock lock1 = new ReentrantLock();
public static void main(String[] args) {
final Test test = new Test();
final ArrayList arr = new ArrayList();
arr.add("a");
arr.add("b");
arr.add("c");
arr.add("d");
arr.add("e");
new Thread() {
public void run() {
test.delete(Thread.currentThread());
};
}.start();
new Thread() {
public void run() {
test.insert(Thread.currentThread(), arr);
};
}.start();
}
public void insert(Thread thread,ArrayList arr) {
// if (lock.tryLock()) {
try {
lock.lock();
System.out.println(thread.getName() + "得到了锁");
// Thread.sleep(5000);
for (int i = 0; i < 5; i++) {
if(i==3){
Thread.sleep(1000);
}
arrayList.add(arr.get(i).toString());
}
for (int j = 0; j < arrayList.size(); j++) {
System.out.println(thread.getName() + ":"
+ arrayList.get(j).toString() + "length:"
+ arrayList.size());
}
} catch (Exception e) {
// TODO: handle exception
} finally {
System.out.println(thread.getName() + "释放了锁");
lock.unlock();
}
// }
}
public void delete(Thread thread) {
//
// if (lock.tryLock()) {
try {
lock1.lock();
System.out.println(thread.getName() + "得到了锁1");
if(arrayList.size()<1){
Thread.sleep(1000);//防止arrayList为空
System.out.println(arrayList.get(0).toString());
arrayList.remove(0);
}
for (int i = 0; i < 10; i++) {
if (i == 8) {
System.out.println("ok");
}
}
} catch (Exception e) {
System.out.print("报错了");
// TODO: handle exception
} finally {
System.out.println(thread.getName() + "释放了锁1");
lock1.unlock();
}
}
// }
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树