Skip to content

Instantly share code, notes, and snippets.

@Ansh1234
Last active October 8, 2016 17:29
Show Gist options
  • Save Ansh1234/53d79a82bbbbd1871c6170920f45b915 to your computer and use it in GitHub Desktop.
Save Ansh1234/53d79a82bbbbd1871c6170920f45b915 to your computer and use it in GitHub Desktop.
public class StringPrinter {
private final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
public static void main(String[] args) {
new StringPrinter().printString();
}
public void printString() {
for (int i = 1; i <= 6; i++) {
threadPoolExecutor.execute(getRunnable(i));
}
}
private Runnable getRunnable(final int i) {
Runnable runnable = new Runnable() {
@Override
public void run() {
String randomString = RandomClass.getRandomString(i);
System.out.println("String returned is : + randomString);
}
};
return runnable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment