`
嵇海波
  • 浏览: 14053 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Lokcs

阅读更多
package locks;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.Lock;

public class LockTest {
	
	private static final int threadInitNumber = 10;
	
	private static final Lock lock = new java.util.concurrent.locks.ReentrantLock();
	
	private List<String> list = new ArrayList<String>();
	
	public static void main(String[] args){
		
		final LockTest lockTest = new LockTest();
		for(int i=0;i<threadInitNumber;i++){
			new Thread(new Runnable(){
				@Override
				public void run() {
					
					lockTest.insert(Thread.currentThread());
				}
				
			}).start();
		}
		
	}
	
	public void insert(Thread thread){
		lock.lock();
		//获取锁
		try{
			System.out.println(thread.getName() + "获得了锁...");
			list.add(String.valueOf(new Random().nextInt(10)));
		}finally{
			System.out.println(thread.getName() + "释放锁...");
			lock.unlock();
		}
		
		
		
	}

}

 

package locks;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.Lock;

public class TryLockTest {
	
	private static final int threadInitNumber = 10;
	
	private static final Lock lock = new java.util.concurrent.locks.ReentrantLock();
	
	private List<String> list = new ArrayList<String>();
	
	public static void main(String[] args){
		
		final TryLockTest lockTest = new TryLockTest();
		for(int i=0;i<threadInitNumber;i++){
			new Thread(new Runnable(){
				@Override
				public void run() {
					
					try {
						lockTest.insert(Thread.currentThread());
					} catch (InterruptedException e) {
						e.printStackTrace();
						System.out.println(Thread.currentThread() + "线程被中断...");
					}
				}
				
			}).start();
		}
		
	}
	
	
	
	public void insert(Thread thread) throws InterruptedException{
		
		//获取锁
		//注意,如果需要正确中断等待锁的线程,必须将获取锁放在外面,然后将InterruptedException抛出
		lock.lockInterruptibly();
		try{
			System.out.println(thread.getName() + "获得了锁...");
			while(true){
				list.add(String.valueOf(new Random().nextInt(10)));
			}
			
		}finally{
			System.out.println(thread.getName() + "释放锁...");
			lock.unlock();
		}
		
		
		
	}

}

 

package locks;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.Lock;

public class LockInterruptiblyTest {
	
	private static final int threadInitNumber = 10;
	
	private static final Lock lock = new java.util.concurrent.locks.ReentrantLock();
	
	private List<String> list = new ArrayList<String>();
	
	public static void main(String[] args){
		
		final LockInterruptiblyTest lockTest = new LockInterruptiblyTest();
		for(int i=0;i<threadInitNumber;i++){
			new Thread(new Runnable(){
				@Override
				public void run() {
					
					lockTest.insert(Thread.currentThread());
				}
				
			}).start();
		}
		
	}
	
	
	
	public void insert(Thread thread){
		
		//获取锁
		if(lock.tryLock()){
			try{
				System.out.println(thread.getName() + "获得了锁...");
				list.add(String.valueOf(new Random().nextInt(10)));
			}finally{
				System.out.println(thread.getName() + "释放锁...");
				lock.unlock();
			}
		}
		//未获取锁
		else{
			System.out.println("未获取锁");
		}
		
		
		
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics