AtomicInteger的介紹和使用

public class AtomicTest {
    static long randomTime() {
        return (long) (Math.random() * 1000);
    }
    public static void main(String[] args) {
        // 阻塞隊列,能容納100個文件
        final BlockingQueue<File> queue = new LinkedBlockingQueue<File>(100);
        // 線程池
        final ExecutorService exec = Executors.newFixedThreadPool(5);
        final File root = new File("D:\\ISO");
        // 完成標誌
        final File exitFile = new File("");
        // 原子整型,讀個數
        // AtomicInteger可以在併發情況下達到原子化更新,避免使用了synchronized,而且性能非常高。
        final AtomicInteger rc = new AtomicInteger();
        // 原子整型,寫個數
        final AtomicInteger wc = new AtomicInteger();
        // 讀線程
        Runnable read = new Runnable() {
            public void run() {
                scanFile(root);
                scanFile(exitFile);
            }
            public void scanFile(File file) {
                if (file.isDirectory()) {
                    File[] files = file.listFiles(new FileFilter() {
                        public boolean accept(File pathname) {
                            return pathname.isDirectory() || pathname.getPath().endsWith(".iso");
                        }
                    });
                    for (File one : files)
                        scanFile(one);
                } else {
                    try {
                        // 原子整型的incrementAndGet方法,以原子方式將當前值加 1,返回更新的值
                        int index = rc.incrementAndGet();
                        System.out.println("Read0: " + index + " " + file.getPath());
                        // 添加到阻塞隊列中
                        queue.put(file);
                    } catch (InterruptedException e) {
                    }
                }
            }
        };
        // submit方法提交一個 Runnable 任務用於執行,並返回一個表示該任務的 Future。
        exec.submit(read);
        // 四個寫線程
        for (int index = 0; index < 4; index++) {
            // write thread
            final int num = index;
            Runnable write = new Runnable() {
                String threadName = "Write" + num;
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(randomTime());
                            // 原子整型的incrementAndGet方法,以原子方式將當前值加 1,返回更新的值
                            int index = wc.incrementAndGet();
                            // 獲取並移除此隊列的頭部,在元素變得可用之前一直等待(如果有必要)。
                            File file = queue.take();
                            // 隊列已經無對象
                            if (file == exitFile) {
                                // 再次添加"標誌",以讓其他線程正常退出
                                queue.put(exitFile);
                                break;
                            }
                            System.out.println(threadName + ": " + index + " " + file.getPath());
                        } catch (InterruptedException e) {
                        }
                    }
                }
            };
            exec.submit(write);
        }
        exec.shutdown();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章