- public class ExchangerTest {
- public static void main(String[] args) {
-
- final Exchanger change = new Exchanger();
-
- new Thread(new Runnable(){
- public void run(){
- try {
- int data1 = 65;
- System.out.println(Thread.currentThread().getName()+":准备交换 "+data1);
- int data2 = (int)change.exchange(data1);
- System.out.println(Thread.currentThread().getName()+":交换得到 "+data2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }).start();
-
- try {
- int data1 = 88;
- System.out.println(Thread.currentThread().getName()+":准备交换 "+data1);
- int data2 = (int)change.exchange(data1);
- System.out.println(Thread.currentThread().getName()+":交换得到 "+data2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 |
|