Sequential streams in Java
Greetings,
I'm trying to solve the following problem: Given N threads, make them run sequentially. For example, each of them should output it in the following order:
Topic 1 Topic 2 ... Topic N
How can I do this using only waits / notifications and synchronized methods (no flags, etc.)?
PS Sorry for my poor english :)
source to share
What about:
thread1.run();
thread2.run();
thread3.run();
New threads are not needed unless parallel execution is required. A single thread can execute Threads bodies sequentially.
If this simple approach does not work (i.e. meets teachers' expectations for contrived homework), please clarify the problem in your question.
source to share
Have you tried to join ing to those?
I won't give more hints as this is homework.
source to share
Have you heard about performers?
// create ExecutorService to manage threads
ExecutorService threadExecutor = Executors.newFixedThreadPool( 3 );
threadExecutor.execute( task1 ); // start task1
threadExecutor.execute( task2 ); // start task2
threadExecutor.execute( task3 ); // start task3
In cases where task1, task2, and task3 are objects that implement the runnable interface. Executors are available since Java 5 More information: http://www.deitel.com/articles/java_tutorials/20051126/JavaMultithreading_Tutorial_Part4.html
source to share