-
-
Save 4e6/8013b1d94abe6d930199e4976ec51ca9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.netbeans.modules.sampler; | |
import java.io.DataOutputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
/** Example reproducing the issue https://github.com/apache/netbeans/pull/4134 */ | |
public class Test { | |
static Sampler sampler = Sampler.createSampler("test"); | |
static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); | |
public static void main(String[] args) { | |
sampler.start(); | |
start(); | |
Runtime.getRuntime().addShutdownHook(new Thread(() -> { | |
System.out.println("Running shutdown hook."); | |
stopSampling(); | |
})); | |
System.console().readLine(); | |
stop(); | |
} | |
private static void start() { | |
System.out.println("Starting processing ..."); | |
executor.scheduleAtFixedRate(Test::doWork, 0, 1, TimeUnit.SECONDS); | |
System.out.println("Processing started"); | |
} | |
private static void doWork() { | |
System.out.println("doing work ..."); | |
} | |
private static void stop() { | |
System.out.println("Stopping processing ..."); | |
executor.shutdown(); | |
try { | |
executor.awaitTermination(1, TimeUnit.SECONDS); | |
} catch (InterruptedException e) { | |
System.err.println("Task interrupted: " + e.getMessage()); | |
} | |
System.out.println("Processing stopped."); | |
} | |
private static void stopSampling() { | |
try { | |
System.out.println("Stopping sampling ..."); | |
sampler.stopAndWriteTo(dos("/tmp/test.npss")); | |
System.out.println("Sampling stopped."); | |
} catch (FileNotFoundException e) { | |
System.err.println("Failed to write sampling data (" + e.getMessage() + ")."); | |
throw new RuntimeException(e); | |
} | |
} | |
private static DataOutputStream dos(String path) throws FileNotFoundException { | |
return new DataOutputStream(new FileOutputStream(path)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment