简体   繁体   中英

Pipelining in Java

I have 6 functions:

  • fetch operation
  • decode
  • fetch operands
  • execute
  • writeback
  • updatepc

Each giving input to another. I want to execute them at the same time ie, pipelining.

How to do that?

Pipeline Pattern

Pipeline Pattern is helpful in dividing the problem into smaller reusable code components. This is a simple yet powerful structural pattern to organize a complex logic into smaller reusable components, which can be added/removed/modified independently.

Create a class for each pipeline component that implements Runnable . Give each component a ConcurrentLinkedQueue to hold the data to be processed; each component will poll this queue in an infinite loop (in its run() method), processing the data as it pulls it off. Each preceding component will add its output to the next component's queue. Now assign each runnable to a thread, start the threads, and start feeding data to the first component's queue.

If a component finds that its queue is empty then you may want to put it to sleep for half a second or so.

You may also want to add a cancel() method to each component that will break out of the infinite loop in its run() method.

public class Decode implements Runnable {
   private boolean cancel = false;
   private ConcurrentLinkedQueue<Data> queue = new ConcurrentLinkedQueue<>();
   private FetchOperands nextComponent;

   public void run() {
       while(!cancel) {
           Data data = queue.poll();
           if(data != null) {
               ...
               nextComponent.enqueue(data);
           } else (Thread.sleep(500);
       }
   }

   public void enqueue(Data data) {
       queue.offer(data);
   }

   public void cancel() {
       cancel = true;
   }

   public void setFetchOperands(FetchOperands nextComponent) {
       this.nextComponent = nextComponent;
   }
}

public class Main implements Runnable {
    public void run() {
        Decode decode = new Decode();
        FetchOperands fetchOperands = new FetchOperands();
        decode.setFetchOperands(fetchOperands);
        Thread t1 = new Thread(decode);
        Thread t2 = new Thread(fetchOperands);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}

要改进@ Zim-Zam O'Pootertoot的答案 ,而不是让你的组件在没有任何东西可以处理的情况下稍微休眠一下,你可以让它wait() ,然后每当你传递一些东西进行处理时调用notify()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM