Created
October 5, 2023 08:47
-
-
Save rokon12/cc1d1b3359cec8e4fdbcac6f2f1d5342 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/rxrav/a52bf9e207d0c6cc22ba7d2390da431b