小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

Java最近版本新特性使用介绍



                                               本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!

         

         在阅读《Thinking in Java》的过程中,并发这一章出现不少新特性,工作中也有遇到,就详细介绍一下

        Java5出现一个新的对象AutomicInteger,经查询它是为避免线程不安全而出现,避免传统方法里使用finally加锁的情况,在高并发环境下应用比较常见!

        用法如下:


	public static void main(String[] args) {
		AtomicInteger ai = new AtomicInteger(0);
		print("当前值:" + ai.get());// 取得当前值
		print("给当前值加2,返回更新后的值:" + ai.addAndGet(2));// 给当前值加2,返回更新后的值
		print("当前值:" + ai.get());// 取得当前值
		print(" 减1操作,返回更新后的值:" + ai.decrementAndGet());// 减1操作,返回更新后的值
		print("当前值:" + ai.get());// 取得当前值
		print(" 给当前值加2,返回当前的值:" + ai.getAndAdd(2));// 给当前值加2,返回当前的值
		print("当前值:" + ai.get());// 取得当前值
		print("减1操作, 返回操作前的值:" + ai.getAndDecrement());//减1操作, 返回操作前的值
		print("当前值:" + ai.get());// 取得当前值
		print("加1操作,返回操作前的值:" + ai.getAndIncrement());// 加1操作,返回操作前的值
		print("当前值:" + ai.get());// 取得当前值
		print(" 设置新值,返回操作前的值:" + ai.getAndSet(2));// 设置新值,返回操作前的值
		print("当前值:" + ai.get());// 取得当前值
		print("加1操作,返回更新后的值:" + ai.incrementAndGet());// 加1操作,返回更新后的值
		System.out.println(ai.compareAndSet(2, 3));
		System.out.println((ai.weakCompareAndSet(2, 3)));
		ai.lazySet(13);// 设置新值
		print("当前值:" + ai.get());// 取得当前值
	}

	static void print(String str) {
		System.out.println(str);
	}

结果如下:

当前值:0
给当前值加2,返回更新后的值:2
当前值:2
 减1操作,返回更新后的值:1
当前值:1
 给当前值加2,返回当前的值:1
当前值:3
减1操作, 返回操作前的值:3
当前值:2
加1操作,返回操作前的值:2
当前值:3
 设置新值,返回操作前的值:3
当前值:2
加1操作,返回更新后的值:3
false
false
当前值:13

Java5中一个新的特性:volatile,主要也是用来实现并发,特点:如果值改变,则告诉所有调用对象

示例:

public class LatestModify extends Thread {
	volatile boolean mRun = true;

	public void test() {
		try {
			Thread.sleep(2);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mRun = false;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		while (mRun) {
			System.out.println("我跑跑!");
		}
		System.out.println("我不跑了!");
	}

	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException {
		LatestModify modify = LatestModify.class.newInstance();
		modify.start();
		modify.test();
	}
我跑跑!
我跑跑!
...........
我跑跑!
我跑跑!
我跑跑!
我不跑了!

Java7中新特性Semaphore允许多个任务同时访问一个资源

原理:给一个资源若干“许可证”,分发给每个任务

应用场景:不详

	class Pool<T>{
		private int size;
		private List<T> items=new ArrayList<T>();
		private volatile boolean[] checkedOut;
		private Semaphore available;
		public Pool(Class<T> classObject,int size){
			this.size=size;
			checkedOut=new boolean[size];
			available=new Semaphore(size, true);
			for (int i = 0; i < size; i++) {
				try {
					items.add(classObject.newInstance());
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		public T checkOut() throws InterruptedException{
			available.acquire();
			return getItem();
		}
		public void checkIn(T x){
			if(releaseItem(x)){
				available.release();
			}
		}
		private synchronized T getItem(){
			for (int i = 0; i < size; i++) {
				if(!checkedOut[i]){
					checkedOut[i]=true;
					return items.get(i);
				}
			}
			return null;
		}
		private synchronized boolean releaseItem(T item){
			int index=items.indexOf(item);
			if(index==-1)
				return false;
			if(checkedOut[index]){
				checkedOut[index]=false;
				return true;
			}
			return false;
		}
	}

因此加上这两个关键字volatile和Semphore,加上sychronzied关键字对象加锁、对方法加锁、Lock、wait+notify、Sleep总共有7个方法,进行多线程同步即并发操作


3Q!

posted on 2013-11-15 10:14  王峰炬  阅读(129)  评论(0编辑  收藏  举报

导航