All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.studio.agent.profiler.ProfileResultUpdater Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.studio.agent.profiler;

import java.util.Set;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicReference;

import org.snapscript.common.thread.ThreadBuilder;
import org.snapscript.studio.agent.event.ProcessEventChannel;
import org.snapscript.studio.agent.event.ProfileEvent;

public class ProfileResultUpdater implements Runnable {

   private final AtomicReference reference;
   private final ProcessEventChannel channel;
   private final TraceProfiler profiler;
   private final ThreadFactory factory;

   public ProfileResultUpdater(TraceProfiler profiler, ProcessEventChannel channel) {
      this.reference = new AtomicReference();
      this.factory = new ThreadBuilder();
      this.profiler = profiler;
      this.channel = channel;
   }
   
   public void start(String process) {
      if(reference.compareAndSet(null, process)) {
         Thread thread = factory.newThread(this);
         thread.start();
      }
   }

   @Override
   public void run() {
      long delay = 1000;
      
      while(true) {
         String process = reference.get();
         try {
            Thread.sleep(delay);
            Set results = profiler.lines(2000);
            ProfileEvent event = new ProfileEvent.Builder(process)
               .withResults(results)
               .build();
            
            channel.send(event);
         }catch(Exception e) {
            e.printStackTrace();
         }finally{
            delay = 5000;
         }
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy