fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. class AffableThread implements Runnable {
  5. public void run() { //Этот метод будет выполнен в побочном потоке
  6. System.out.println("Привет из побочного потока!"+Thread.currentThread().getName());
  7. }
  8. }
  9.  
  10. public class Main {
  11. static AffableThread mSecondThread;
  12.  
  13. public static void main(String[] args) throws java.lang.InterruptedException {
  14. mSecondThread = new AffableThread();
  15. List<Thread> threads = new ArrayList<>();
  16. int i = 0;
  17. while (i<5) {
  18. Thread msSecondThread = new Thread(mSecondThread);
  19. msSecondThread.start();
  20. threads.add(msSecondThread);
  21. i++;
  22. }
  23. for (Thread t : threads) {
  24. t.join();
  25. }
  26. if ( i== 5){
  27. System.out.println("Главный поток завершён...");
  28. }
  29. }
  30. }
  31.  
Success #stdin #stdout 0.1s 27916KB
stdin
Standard input is empty
stdout
Привет из побочного потока!Thread-0
Привет из побочного потока!Thread-1
Привет из побочного потока!Thread-2
Привет из побочного потока!Thread-3
Привет из побочного потока!Thread-4
Главный поток завершён...