Java线程之Exchanger

Exchanger可以在两个线程之间交换数据,只能是2个线程,他不支持更多的线程之间互换数据。

当线程A调用Exchange对象的exchange()方法后,他会陷入阻塞状态,直到线程B也调用了exchange()方法,然后以线程安全的方式交换数据,之后线程A和B继续运行

public class ThreadLocalTest {

	public static void main(String[] args) {
		Exchanger> exchanger = new Exchanger<>();
		new Consumer(exchanger).start();
		new Producer(exchanger).start();
	}

}

class Producer extends Thread {
	List list = new ArrayList<>();
	Exchanger> exchanger = null;
	public Producer(Exchanger> exchanger) {
		super();
		this.exchanger = exchanger;
	}
	@Override
	public void run() {
		Random rand = new Random();
		for(int i=0; i<10; i++) {
			list.clear();
			list.add(rand.nextInt(10000));
			list.add(rand.nextInt(10000));
			list.add(rand.nextInt(10000));
			list.add(rand.nextInt(10000));
			list.add(rand.nextInt(10000));
			try {
				list = exchanger.exchange(list);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

class Consumer extends Thread {
	List list = new ArrayList<>();
	Exchanger> exchanger = null;
	public Consumer(Exchanger> exchanger) {
		super();
		this.exchanger = exchanger;
	}
	@Override
	public void run() {
		for(int i=0; i<10; i++) {
			try {
				list = exchanger.exchange(list);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.print(list.get(0)+", ");
			System.out.print(list.get(1)+", ");
			System.out.print(list.get(2)+", ");
			System.out.print(list.get(3)+", ");
			System.out.println(list.get(4)+", ");
		}
	}
}


你可能感兴趣的:(JAVA技术)