Java面试总结(多线程包的使用)

CountDownLatch的用法

	 final CountDownLatch latch = new CountDownLatch(N);

		 for(int i=0;i

CyclicBarrier的用法

barrier.await():等到所有parties(线程)都调用

构造方法中的Runnable是当所有线程都执行await后才执行的。


		 int parties = 5;
		 final CyclicBarrier barrier = new CyclicBarrier(parties,new Runnable() {
			
			@Override
			public void run() {
				System.out.println("所有线程执行完毕");
			}
		});
		 
		 List list = new ArrayList();

		 for(int i=0;i

Semaphore

信号量是用来限制线程数量来访问一些(物理或者逻辑资源)

如果不释放许可,那么其他线程是无法获得许可的。

如下图,由于permits=10,那么只有10个线程能获得许可并执行,其他线程等待获得许可


		
		int permits = 10;
		
		final Semaphore semaphore = new Semaphore(permits);
		
		for(int i = 0; i < 100; i++){
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					//获得许可
					try {
						semaphore.acquire();
						//模拟执行
						System.out.println(Thread.currentThread().getName() + " 获得许可,正在执行!!!");
						Thread.sleep(5000);
						//释放许可
						semaphore.release();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			thread.start();
		}
	

Exchanger

例子一:按照官方例子稍微改动了一下,看不出效果


	
	
	public static void main(String[] args){
		Exchanger exchanger = new Exchanger();
		DataBuffer emptyBuffer = null;
		DataBuffer fullBuffer = null;
		
		new Thread(new ReadBufferThread(exchanger, fullBuffer)).start();
		new Thread(new WriteToBufferThread(exchanger, emptyBuffer)).start();
	}
	
	//写入缓存数据
	static class WriteToBufferThread implements Runnable{

		Exchanger exchanger;
		DataBuffer emptyBuffer;
		
		public WriteToBufferThread(Exchanger exchanger,DataBuffer emptyBuffer) {
			this.exchanger = exchanger;
			this.emptyBuffer = emptyBuffer;
		}
		
		@Override
		public void run() {
			while(emptyBuffer != null){
				if(bufferIsFull(emptyBuffer)){
					try {
						//缓冲充满
						//通过exchanger将已满buffer传递过去并阻塞当前线程
						//另一个线程调用exchanger.exchange()时则完成了数据交换,此时buffer为空
						emptyBuffer = exchanger.exchange(emptyBuffer);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}else{
					addToBuffer(emptyBuffer);
				}
			}
		}
		
		//添加数据到buffer
		public void addToBuffer(DataBuffer buffer){
			
		}
		
		//判断buffer充满
		public boolean bufferIsFull(DataBuffer buffer){
			return true;
		}
		
	}
	
	//读取缓冲数据
	static class ReadBufferThread implements Runnable{
		Exchanger exchanger;
		DataBuffer fullBuffer;
		
		public ReadBufferThread(Exchanger exchanger,DataBuffer fullBuffer){
			this.exchanger = exchanger;
			this.fullBuffer = fullBuffer;
		}
		
		@Override
		public void run(){
			while(fullBuffer != null){
				takeFromBuffer(fullBuffer);
				if(bufferIsEmpty(fullBuffer)){
					try {
						fullBuffer = exchanger.exchange(fullBuffer);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
		public void takeFromBuffer(DataBuffer buffer){
			//to do
		}
		
		public boolean bufferIsEmpty(DataBuffer buffer){
			//to do
			return true;
		}
		
	}

例子二:自己简单写的一个例子,可以看到数据进行了交换


	
	
	public static void main(String[] args){
		Exchanger exchanger = new Exchanger();
		new ChangeThread1(exchanger).start();
		new ChangeThread2(exchanger).start();
	}
	
	static class ChangeThread1 extends Thread{
		Exchanger exchanger;
		String myname = "ChangeThread1";
		
		public ChangeThread1(Exchanger exchanger) {
			this.exchanger = exchanger;
		}
		
		@Override
		public void run(){
			System.out.println(Thread.currentThread().getName()+" this is ChangeThread 1 ");
			try {
				//名字进行了交换
				myname = exchanger.exchange(myname);
				System.out.println(Thread.currentThread().getName()+" "+myname);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	static class ChangeThread2 extends Thread{
		Exchanger exchanger;
		String myname = "ChangeThread2";
		
		public ChangeThread2(Exchanger exchanger) {
			this.exchanger = exchanger;
		}
		
		@Override
		public void run(){
			System.out.println(Thread.currentThread().getName()+ " this is ChangeThread 2 ");
			try {
				//名字进行了交换
				myname = exchanger.exchange(myname);
				System.out.println(Thread.currentThread().getName()+" "+myname);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}
	

控制台输出:

Thread-0 this is ChangeThread 1
Thread-1 this is ChangeThread 2
Thread-1 ChangeThread1
Thread-0 ChangeThread2

转载于:https://my.oschina.net/kyuan2cn/blog/1785510

你可能感兴趣的:(Java面试总结(多线程包的使用))