0

使用 Disruptor 环形缓冲区,我每秒只能完成 600 万次操作。我想知道我哪里出错了。我的事件处理程序只是增加了一个计数器。这是单一生产者和单一消费者。有人可以告诉我我是否对语义本身有误。该程序创建一个生产者线程,该线程添加到缓冲区中。并创建一个事件处理程序来处理发布事件。每次发布事件时,事件处理程序都会增加一个 volatile 计数器。

public class MainClass{

   public static class globalVariables
   {
        static int NUMBER_OF_ITERATIONS = 33554432; // 2 power 25     
        static int NUMBER_OF_THREADS;
        static int RING_SIZE = NUMBER_OF_ITERATIONS;
        static int WRITE_CODE = 1;

        static volatile int keep_going = 1; 
   };

   public void start_execution()
   {            
        int remainder = globalVariables.NUMBER_OF_ITERATIONS % globalVariables.NUMBER_OF_THREADS;               
        int iterations_per_thread = ( globalVariables.NUMBER_OF_ITERATIONS - remainder ) / globalVariables.NUMBER_OF_THREADS ;


      /* New Shared Object */
      final sharedObject newSharedObject = new sharedObject();


    ExecutorService exec = Executors.newFixedThreadPool(1);
      Disruptor<valueEvent> disruptor = new Disruptor<valueEvent>( valueEvent.EVENT_FACTORY, globalVariables.RING_SIZE, exec );


      /* Creating event handler whenever an item is published in the queue */

           final EventHandler<valueEvent> handler = new EventHandler<valueEvent>()
           {
              public void onEvent(final valueEvent event, final long sequence, 
                          final boolean endOfBatch) throws Exception
              {         
                 newSharedObject.shared_variable++; // increment the shared variable
              }
           };


          /* Use the above handler to handler events */
          disruptor.handleEventsWith(handler);


     /* start Disruptor */
     final RingBuffer<valueEvent> ringBuffer = disruptor.start();


     final long[] runtime = new long [globalVariables.NUMBER_OF_THREADS];
     /* Code the producer thread */
     final class ProducerThread extends Thread {
        int i;

        public ProducerThread( int i )
        {
            this.i = i;
        }

        public void run()
        {
           long idle_counter = 0;
           long count;

           System.out.println("In thread "+i );

           long startTime = System.nanoTime();

           //while( globalVariables.keep_going == 1 )
           for( int counter=0; counter<globalVariables.NUMBER_OF_ITERATIONS; counter++ )
           {
              // Publishers claim events in sequence
              long sequence = ringBuffer.next();
              valueEvent event = ringBuffer.get(sequence);

              event.setValue(globalVariables.WRITE_CODE); 

              // make the event available to EventProcessors
              ringBuffer.publish(sequence);  
           }

           long stopTime = System.nanoTime();
           runtime[i] = (stopTime - startTime)/1000; 
        }
     };

     /* ------------------------------------------------------------------------------- */     
     //final class AlarmHandler extends TimerTask {     
          /*** Implements TimerTask's abstract run method.   */
    //    @Override public void run(){
    //      globalVariables.keep_going = 0;
    //    }
   //  };

     /* ------------------------------------------------------------------------------- */
     /* Creating Producer threads */
     ProducerThread[] threads = new ProducerThread[globalVariables.NUMBER_OF_THREADS];
     for (int i = 0; i < globalVariables.NUMBER_OF_THREADS; i++) {
        threads[i] = new ProducerThread( i );
        threads[i].start();
     }

     // Waiting for the threads to finish
     for (int i = 0; i < globalVariables.NUMBER_OF_THREADS; i++) {
        try
        {
         threads[i].join();
        } catch ( InterruptedException e ) { System.out.println("hi exception :)"); }
     } 

     /* shutdown */     
     disruptor.shutdown();
     exec.shutdown();

     /* Printing Statistics */
     System.out.println( "Shared Variable: " + newSharedObject.shared_variable );
     for ( int i=0; i<globalVariables.NUMBER_OF_THREADS; i++ )
     {
         System.out.println("Runtime="+ runtime[i] + "; Operations per second = " + (globalVariables.NUMBER_OF_ITERATIONS  / runtime[i] )*1000000 +"ops/sec" );         
     }     

   }

    public static void main( String args[] )
    {
        globalVariables.NUMBER_OF_THREADS = Integer.parseInt( args[0] );    

        System.out.println( "Number of Threads = "+ globalVariables.NUMBER_OF_THREADS );

        MainClass mainObj = new MainClass();
        mainObj.start_execution();
        System.exit(0);
    }
};

这是程序的输出

共享变量:33554432;运行时间=5094139 微秒;每秒操作数 = 6000000

任何帮助将非常感激。

4

1 回答 1

0

由于您在单个线程中运行事件处理程序,而不是共享该状态,因此通过让事件处理程序在非易失性字段上工作,您应该获得明显更好的性能(但仍然是正确的功能)。中断器确保您的处理程序一次只处理一个事件,因此您无需担心丢失增量。

如果系统中的另一个组件依赖于以特定顺序出现的这个值(例如:它是一个控制值),那么你应该考虑使用类似 AtomicInteger 和lazySet [1] 的东西。

[1] http://psy-lob-saw.blogspot.com.au/2012/12/atomiclazyset-is-performance-win-for.html

于 2013-12-02T01:24:21.947 回答