synchronized(list){ //获得锁
list.append();
list.count();
}//释放锁
public static void main(String[] args){
List list = new LinkedList();
Thread r = new Thread(new ReadList(list));
Thread w = new Thread(new WriteList(list));
r.start();
w.start();
}
class ReadList implements Runnable{
private List list;
public ReadList(List list){
this.list = list;
}
@Override
public void run(){
System.out.println("ReadList begin at "+System.currentTimeMillis());
synchronized (list){
try {
Thread.sleep(1000);
System.out.println("list.wait() begin at "+System.currentTimeMillis());
list.wait();
System.out.println("list.wait() end at "+System.currentTimeMillis());
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("ReadList end at "+System.currentTimeMillis());
}
}
class WriteList implements Runnable{
private List list;
public WriteList(List list){
this.list = list;
}
@Override
public void run(){
System.out.println("WriteList begin at "+System.currentTimeMillis());
synchronized (list){
System.out.println("get lock at "+System.currentTimeMillis());
list.notify();
System.out.println("list.notify() at "+System.currentTimeMillis());
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("get out of block at "+System.currentTimeMillis());
}
System.out.println("WriteList end at "+System.currentTimeMillis());
}
}
ReadList begin at 1493650526582 WriteList begin at 1493650526582 list.wait() begin at 1493650527584 get lock at 1493650527584 list.notify() at 1493650527584 get out of block at 1493650529584 WriteList end at 1493650529584 list.wait() end at 1493650529584 ReadList end at 1493650529584
synchronized(object){
}
private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); condition.await();//this.wait(); condition.signal();//this.notify(); condition.signalAll();//this.notifyAll();
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == items.length)
notFull.await();
enqueue(e);
} finally {
lock.unlock();
}
}
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}
private void enqueue(E x) {
// assert lock.getHoldCount() == 1;
// assert items[putIndex] == null;
final Object[] items = this.items;
items[putIndex] = x;
if (++putIndex == items.length)
putIndex = 0;
count++;
notEmpty.signal();
}
private E dequeue() {
// assert lock.getHoldCount() == 1;
// assert items[takeIndex] != null;
final Object[] items = this.items;
@SuppressWarnings("unchecked")
E x = (E) items[takeIndex];
items[takeIndex] = null;
if (++takeIndex == items.length)
takeIndex = 0;
count--;
if (itrs != null)
itrs.elementDequeued();
notFull.signal();
return x;
}
Lock lock = new ReentrantLock();
lock.lock();
try{
}finally{
lock.unlock();
}
class RWLockList {
//读写锁
private List list;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Lock readLock = lock.readLock();
private final Lock writeLock = lock.writeLock();
public RWLockList(List list){
this.list = list;
}
public int get(int k) {
readLock.lock();
try {
return (int)list.get(k);
}
finally {
readLock.unlock();
}
}
public void put(int value) {
writeLock.lock();
try {
list.add(value);
}
finally {
writeLock.unlock();
}
}
}
class SyncList {
private List list;
public SyncList(List list){
this.list = list;
}
public synchronized int get(int k){
return (int)list.get(k);
}
public synchronized void put(int value){
list.add(value);
}
}
List list = new LinkedList();
for (int i=0;i<10000;i++){
list.add(i);
}
RWLockList rwLockList = new RWLockList(list);
//初始化数据
Thread writer = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
rwLockList.put(i);
}
}
}
);
Thread reader1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
rwLockList.get(i);
}
}
}
);
Thread reader2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
rwLockList.get(i);
}
}
}
);
long begin = System.currentTimeMillis();
writer.start();
reader1.start();
reader2.start();
try {
writer.join();
reader1.join();
reader2.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("RWLockList take "+(System.currentTimeMillis()-begin) + "ms");
List list = new LinkedList();
for (int i=0;i<10000;i++){
list.add(i);
}
SyncList syncList = new SyncList(list);//初始化数据
Thread writerS = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
syncList.put(i);
}
}
});
Thread reader1S = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
syncList.get(i);
}
}
});
Thread reader2S = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
syncList.get(i);
}
}
});
long begin1 = System.currentTimeMillis();
writerS.start();reader1S.start();reader2S.start();
try {
writerS.join();
reader1S.join();
reader2S.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("SyncList take "+(System.currentTimeMillis()-begin1) + "ms");
RWLockList take 248ms RWLockList take 255ms RWLockList take 249ms RWLockList take 224ms SyncList take 351ms SyncList take 367ms SyncList take 315ms SyncList take 323ms
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有