Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created October 5, 2023 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokon12/cc1d1b3359cec8e4fdbcac6f2f1d5342 to your computer and use it in GitHub Desktop.
Save rokon12/cc1d1b3359cec8e4fdbcac6f2f1d5342 to your computer and use it in GitHub Desktop.
package ca.bazlur;
public class JavaRocksPrinter {
private final int times;
public JavaRocksPrinter(int times) {
this.times = times;
}
public void printJava(Runnable action) {
for (int i = 0; i < times; i++) {
action.run();
}
}
public void printRocks(Runnable action) {
for (int i = 0; i < times; i++) {
action.run();
}
}
public static void main(String[] args) throws InterruptedException {
var javaRocks = new JavaRocks(10);
var javaPrinter = Thread.ofPlatform()
.unstarted(() -> javaRocks.printJava(() -> System.out.print("Java ")));
var rocksPrinter = Thread.ofPlatform()
.unstarted(() -> javaRocks.printRocks(() -> System.out.print("Rocks!\n")));
javaPrinter.start();
rocksPrinter.start();
javaPrinter.join();
rocksPrinter.join();
}
}
//Output:
//Java Rocks!
//Java Rocks!
//Java Rocks!
//Java Rocks!
//Java Rocks!
//Java Rocks!
//Java Rocks!
//Java Rocks!
//Java Rocks!
//Java Rocks!
@rxrav
Copy link

rxrav commented Oct 5, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment