美文网首页
JUC 1234线程

JUC 1234线程

作者: 刘尔泽 | 来源:发表于2017-10-11 14:55 被阅读47次
image.png

题目如图所示

package com.example;

import java.util.concurrent.CyclicBarrier;

/**
 * Created by Java 群 坦克
 */

public class MyClass {

    public static void main(String[] args) {
        final int N = 4;
        final CyclicBarrier barrier = new CyclicBarrier(N);
        final StringBuilder[] buffers = new StringBuilder[N];
        Thread[] threads = new Thread[N];

        for (int i = 0; i < N; i++) {
            buffers[i] = new StringBuilder();
            final int id = i;
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for (int x = id; ; x = (x + N - 1) % N) {
                            barrier.await();
                            buffers[x].append(id + 1);
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
            threads[i].start();
        }
        ////////////////////////////////////////////////////////
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (StringBuilder buffer : buffers) {
            System.out.println(buffer);
        }
        System.exit(0);
    }
}

result :


image.png

我当时 想的

刘尔泽:这个题,我想的是四个线程维护一个信号量,然后按时间片每次试图往上一个文件里写入

image.png

还有再看看的

  • 用CB就可以了
    CB主要是里面有个计数器,每进来await一次,计数器加一,到最后一个进来的线程await的时候,通过ReentryLock里面的Condition,向其他人发信号
    N个线程,N个都必须同时(特别是在循环内)开始某个操作,用CyclicBarrier
  • ReentryLock 这个好像面试问的挺多,再查查
  • JUC包 java.util.concurrent 很多东西
    http://www.cnblogs.com/skywang12345/p/3498454.html
  • ReentrantLock 、synchronized和volatile

后来 他又写了一个版本

image.png
坦克 2017/11/22 18:22:49
    static final int N = 4;
    static final CyclicBarrier barrier = new CyclicBarrier(N);
    static final StringBuilder[] mockFiles = new StringBuilder[N];
    static {
        for (int i = 0; i < mockFiles.length; mockFiles[i++] = new StringBuilder());
    }
    static boolean running = true;

    public static void main(String[] args) throws Exception {
        for (int i = 0; i < N; i++) {
            new Worker(i).start();
        }
        Thread.sleep(500);
        running = false;
        for (StringBuilder mockFile : mockFiles) {
            System.out.println(mockFile);
        }
    }

    static class Worker extends Thread {
        final int index;
        public Worker(int index) {
            this.index = index;
        }
        public void run() {
            try {
                for (int d = index + 1, pos = index; running; pos = (pos + N - 1) % N) {
                    barrier.await();
                    StringBuilder mockFile = mockFiles[pos];
                    mockFile.append(d);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

相关文章

网友评论

      本文标题:JUC 1234线程

      本文链接:https://www.haomeiwen.com/subject/ebdwyxtx.html