码迷,mamicode.com
首页 > 编程语言 > 详细

多线程同步

时间:2015-12-21 18:02:42      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

package cn.dyg;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

public class ThreadLock {

	
	public static void main(String[] args) {
		final Business business = new BusinessWriteReadLock();
		
		for (int i = 0; i < 3; i++) {
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					while(true) {
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						System.out.println(business.get("zjy"));
					}
					
				}
			}).start();
		}
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				business.put("zjy", "zhaojunyang");
			}
		}).start();
	}
	
}



interface Business {
	
	public void put(String key, String value);
	
	public String get(String key);
}

class BusinessNoSynchronized implements Business {

	private Map<String, String> map = new HashMap<String, String>();
	
	@Override
	public void put(String key, String value) {
		System.out.println(Thread.currentThread().getName() + "write shared data...");
		map.put(key, value);
	}

	@Override
	public String get(String key) {
		System.out.println(Thread.currentThread().getName() + "read shared data...");
		return map.get(key);
	}
	
}


class BusinessSynchroinzed implements Business {
	
	private Map<String, String> map = new HashMap<String, String>();
	
	@Override
	public synchronized String get(String key) {
		String value = map.get(key);
		while (value == null) {
			try {
				wait();
				value = map.get(key);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return map.get(key);
	}
	
	@Override
	public synchronized void put(String key, String value) {
		map.put(key, value);
		notify();
	}
	
}


class BusinessLock implements Business {
	
	private Map<String, String> map = new HashMap<String, String>();
	
	private ReentrantLock lock = new ReentrantLock(); 
	
	private Condition condition = lock.newCondition();
	
	
	public void put(String key, String value) {
		lock.lock();
		try {
			System.out.println(Thread.currentThread().getName() + "write shared data...");
			map.put(key, value);
			condition.signal();
		} finally {
			lock.unlock();
		}
		
	}
	
	public String get(String key) {
		lock.lock();
		try {
			System.out.println(Thread.currentThread().getName() + "read shared data...");
			while (map.get(key) == null) {
				try {
					condition.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return map.get(key);
		} finally {
			lock.unlock();
		}
	}
}
//这个是遗留问题,怎么实现阻塞并释放读写锁,这个一直比较困惑,希望路过的大神给予指导??
class BusinessWriteReadLock implements Business {

	private Map<String, String> map = new HashMap<String, String>();
	
	private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
	
	private WriteLock writeLock = lock.writeLock();
	
	private ReadLock readLock = lock.readLock();
	
	@Override
	public void put(String key, String value) {
		writeLock.lock();
		try {
			map.put(key, value);
		} finally {
			writeLock.unlock();
		}
		
	}

	@Override
	public String get(String key) {
		readLock.lock();
		try {
			while (map.get(key) == null) {
				Thread.yield();		//并不释放当前锁
			}
			return map.get(key);
		} finally {
			readLock.unlock();
		}
	}
	
}

 

多线程同步

标签:

原文地址:http://www.cnblogs.com/zhaojunyang/p/5063802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!