Exchange 사용 프레젠테이션

1817 단어

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.Exchanger;


/**
*  : Exchange 
*/
public class UseExchange {
    private static final Exchanger> exchange = new Exchanger>();


    public static void main(String[] args) {


        new Thread(new Runnable() {
            @Override
            public void run() {
                Set setA = new HashSet();// 
                try {
                    /* 
                     * set.add(.....)
                     * */
                    setA.add("A");
                    setA = exchange.exchange(setA);// set
                    /* */


                    Iterator iterator = setA.iterator();
                    while (iterator.hasNext()) {
                        System.out.println("A::"+iterator.next());
                    }
                } catch (InterruptedException e) {
                }
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                Set setB = new HashSet();// 
                try {
                    /* 
                     * set.add(.....)
                     * set.add(.....)
                     * */
                    setB.add("B");
                    setB = exchange.exchange(setB);// set
                    /* */
                    Iterator iterator = setB.iterator();
                    while (iterator.hasNext()) {
                        System.out.println("B::"+iterator.next());
                    }
                } catch (InterruptedException e) {
                }
            }
        }).start();


    }


}

좋은 웹페이지 즐겨찾기