多線程

題目描述

問題描述:有4個線程和1個公共的字符數組。線程1的功能就是向數組輸出A,線程2的功能就是向字符輸出B,
線程3的功能就是向數組輸出C,線程4的功能就是向數組輸出D。要求按順序向數組賦值ABCDABCDABCD,
ABCD的個數由線程函數1的參數指定。

輸入描述:

輸入一個int整數

輸出描述:

輸出多個ABCD

輸入例子:

10

輸出例子:

ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD

算法實現

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Declaration: All Rights Reserved !!!
 */
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            for (int i = 0; i < n; i++) {
                System.out.print("ABCD");
            }
            System.out.println();
        }

        scanner.close();
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    // TODO 應用使用下面的方法進行操做
    ////////////////////////////////////////////////////////////////////////////////////////////////////


    /**
     * 打印線程
     */
    public static class WriteThread extends Thread {
        // 打印次數
        private final int times;
        // 線程名稱
        private final String name;
        // 打印順序變量
        private final AtomicInteger order;
        // 當前線程的特別標識
        private final int flag;
        // 全部的線程數
        private final int all;
        // 保存結果
        private final StringBuilder builder;

        public WriteThread(int times, String name, AtomicInteger order, int flag, int all, StringBuilder builder) {
            this.times = times;
            this.name = name;
            this.order = order;
            this.flag = flag;
            this.all = all;
            this.builder = builder;
        }

        @Override
        public void run() {
            int count = 0;
            // 一直輸出到指定次數
            while (count < times) {
                // 判斷是否輪到本身輸出
                if (order.get() == flag) {
                    // 輸出計數加一
                    count++;
                    // 輸出內容
                    builder.append(name);
                    // 指定下一個線程進行輸出
                    order.set((flag + 1) % all);
                }
            }
        }
    }

    public static void main2(String[] args) throws InterruptedException {
        Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main2.class.getClassLoader().getResourceAsStream("data.txt"));

        // 讀取並保存全部的輸入
        List<Integer> input = new LinkedList<>();
        while (scanner.hasNextLong()) {
            input.add(scanner.nextInt());
        }

        // 計算結果
        List<StringBuilder> result = new LinkedList<>();
        for (int num : input) {
            StringBuilder builder = new StringBuilder();
            result.add(builder);
            AtomicInteger order = new AtomicInteger(0);
            WriteThread a = new WriteThread(num, "A", order, 0, 4, builder);
            WriteThread b = new WriteThread(num, "B", order, 1, 4, builder);
            WriteThread c = new WriteThread(num, "C", order, 2, 4, builder);
            WriteThread d = new WriteThread(num, "D", order, 3, 4, builder);
            a.start();
            b.start();
            c.start();
            d.start();
            a.join();
            b.join();
            c.join();
            d.join();

        }

        for (StringBuilder b : result) {
            System.out.println(b);
        }

        scanner.close();
    }
}
相關文章
相關標籤/搜索