多线程和Chronicle队列

时间:2018-05-01 13:54:51

标签: chronicle-queue

所以我试图看看在多线程环境中写入Chronicle Queue的最快方法是什么,所以我有以下内容:

 public static void main(String[] args) throws  Exception{
    final String path = args[0];
    int times = Integer.parseInt(args[1]);
    int num = Integer.parseInt(args[2]);

    AtomicInteger nextid = new AtomicInteger(0);
    ThreadLocal<Integer> id = ThreadLocal.withInitial(() -> nextid.getAndIncrement());

    ChronicleTest test = new ChronicleTest();
    ChronicleWriter writer = test.new ChronicleWriter(path);

    CountDownLatch start = new CountDownLatch(1);
    CountDownLatch done = new CountDownLatch(num);
    Thread[] threads = new Thread[num];
    long[] samples = new long[times * num];
    for (int i = 0; i < num; i ++) {
        threads[i] = new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    start.await();
                    for (int i = 0; i < times; i++) {
                        int j = i + times*id.get().intValue();
                        long s = System.nanoTime();
                        writer.write(j + " DGirr5JgGVmxhvmaoO0c5MVVOUIEJxWa6nVStPnqmRl3T4hKE9tiwNjn6322uhgr2Fs4hDG8aKYvIB4O0733fx18EqGqNsshaSKoouky5ZekGK3vO87nfSUOz6uDD0olOp35QJQKPgr7tNlFgQP7BImcCyMPFCCm3yhSvOUgiVAD9W9BC3cqlKjQebOG4EkqzRIzwZjxbnIeK2YttfrvOvUJs0e9WBhXUVibi5Ks2j9ROQu2q0PJII4NYyN1a5YW2UKxyng3bRrtBVFqMSamtFzJ23EE4Y7rbQyeCVJhIKRM1LRvcGLUYZqKICWwDtOjGcbXUIlLLYiJcnVRZ4gNRvbFXvTL4XDjhD3uP5S8DjnkeAIBZcQ4VEUf30x65pTGLhWjOMV6jtiEQOWKB3nsuPMhcS1lP3wTQztViW7T8IsQlA5kvVAsnT5A7pojS1CffcYz4a2Rwqf9w6mhTPPZXgpDGArtThW3a69rwjsQxEY9aTwi0Pu0jlSAMmvOA158QFsSeJvLoJXILfocgjNEkj7iVcO0Rc6eC6b5EhJU3wv80EEHnORMXpQVuAuPyao7vJsx06TMcXf9t7Py4qxplVPhptIzrKs2qke2t5A8O4LQzq19OfEQsQGEjqHSbnfWXjfuntuFR8rV4VMyLZO1z3K7HmHtCEy14p5u0C0lj7vmtCnrOum0bnG2MwaAR7DJPIpOtwRObli5K5grv54AWnJyagpRX5e3eTEA8NAKO8oDZuLaoCvgavv9ciFrJcIDmyleVweiVrHs1fQXJoELzFpH4BmvzBuUjfZ8ORSIZsVuq4Hpls19GIA8opb1mSBt7MTifLPauo8WDWRoNi9DvjL4Z08Je6DvrhAFXasU2CMugg5EZ06OXexU17qnvxx2Vz9s9E5U50jDemySZ78KcZ6nqhQKIXUrkTktoEan2JXxI2NNSqEYifwly8ZO2MDquPe4J11rAcOqYp9y6Kb4NtFpNysM1evrLPvCx8oe");

                        long e = System.nanoTime();
                        samples[j] = e - s;
                    }
                    done.countDown();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
    for (int i = 0; i < num; i ++) {
        try {
            threads[i].start();
        } catch (Exception e){

        }
    }
    long startT = System.currentTimeMillis();
    start.countDown();
    done.await();
    long endT = System.currentTimeMillis();
    System.out.println("Time to complete [" + times + "] iteration in [" + (endT - startT) + " ms] and threads [" + num + "]");

    System.out.println("#######");
    for (int i = 0; i < times * num; i ++){
        System.out.println(samples[i]);
    }
}

private class ChronicleWriter {

    SingleChronicleQueue m_cqueue;
    ThreadLocal<ExcerptAppender> m_appender;
    ChronicleWriter(String path ) {
        m_cqueue = SingleChronicleQueueBuilder.binary(path).build();
        m_appender = new ThreadLocal<ExcerptAppender>() {
            protected ExcerptAppender initialValue() {
                return m_cqueue.acquireAppender();
            }
        };
    }

    void write(String msg){
        m_appender.get().writeText(msg);
    }
}

我运行参数:     路径2500 40

出于某种原因,这会随着核心转储而崩溃。我究竟做错了什么?我的磁盘有很多磁盘空间,所以无所谓。谢谢!

1 个答案:

答案 0 :(得分:0)

如果您的程序由于OutOfMemory错误而崩溃,那么 请注意,程序使用的磁盘空间和实际空间可能不同。 您可能需要增加jvm堆大小。

请参阅以下链接以增加jvm堆大小 What are the Xms and Xmx parameters when starting JVMs?

或者

如果您通过eclipse运行程序,请参阅以下链接 http://www.planetofbits.com/eclipse/increase-jvm-heap-size-in-eclipse/

我已经使用以下版本的chronicle-queue尝试了你的程序,它运行正常。

<dependency>
    <groupId>net.openhft</groupId>
    <artifactId>chronicle-queue</artifactId>
    <version>4.5.14</version>
</dependency>