Is there any way that there is only one main class and no inner class?
Sure. You can pass the print string to your main class. Of course, the hard part is to coordinate the streams so that they actually print out "HelloWorld"
instead of "WorldHello"
or other permutations. The threads will work in parallel, without an order guarantee. The fact that all points of stream programs - they work asynchronously. An attempt to force a specific word to be denied the purpose of using streams.
pompous This brings me a bad job in computer science. The whole point of writing in threads is that they work independently of each other. Coordination usually occurs when each thread is pulled from the work queue and then puts the results in the result queue or something like that. Every time you have a streaming program that needs to coordinate this, you are most likely not going to use streams. & L; / & bombastic GT;
But since everyone is voting, my previous answer, probably because it does not solve the problem of homework, I will add some logic to coordinate between the two threads and spit out "Hello World ...".
Two threads should be able to block something, signal each other and know when they should wait or print. Therefore, I will add boolean printHello
and lock the shared lock object, which is passed to:
public class HelloWorld implements Runnable { private static boolean printHello = true; private final String toPrint; private final boolean iPrintHello; private final Object lock; public static void main(String[] args) { final Object lock = new Object();
source share