Skip to content

T12_00_Exchanger_Not_Work #6

@HanYehong

Description

@HanYehong

public class T12_00_Exchanger_Not_Work {
private static Exchanger exchanger = new Exchanger<>();

public static void main(String[] args) {
    char[] aI = "1234567".toCharArray();
    char[] aC = "ABCDEFG".toCharArray();

    new Thread(()->{
        for(int i=0; i<aI.length;) {
            try {
                String t1 = exchanger.exchange("T1");
                if ("T2".equals(t1)) {
                    System.out.print(aI[i]);
                    i++;
                }
                exchanger.exchange("T2");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

    new Thread(()->{
        for(int i=0; i<aC.length;) {
            try {
                String t2 = exchanger.exchange("T2");
                if ("T2".equals(t2)) {
                    System.out.print(aC[i]);
                    i++;
                    exchanger.exchange("T2");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions