HWOJ多线程

package snippet;
import java.util.*;
public class Snippet{
    static volatile char flag = 'A';
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        final int n = in.nextInt();
        flag = 'A';
        for (int i = 0; i < 4; i++) {
            final int finalI = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < n; j++) {
                        //有点搞不懂
                        char temp = (char) ('A' + finalI);
                        while (flag != temp); 
                        System.out.print((char) ('A' + finalI));
                        flag = (char) (temp + 1);
                        if (flag == 'E') flag = 'A';
                    }
                }
            }).start();
        }
    }
}

你可能感兴趣的:(HWOJ多线程)