fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. for (int i = 0; i < 10; i++) {
  13. Wife wife = new Wife();
  14. Husband husband = new Husband();
  15. wife.start();
  16. husband.start();
  17.  
  18.  
  19. try {
  20. husband.join(); //Подождать окончания мысли четверть секунды.
  21. }
  22. }
  23.  
  24. public static class Husband extends Thread {
  25. @Override
  26. public void run() {
  27. System.out.println("Husband " + Account.deposit());
  28. }
  29. }
  30.  
  31. public static class Wife extends Thread {
  32. @Override
  33. public void run() {
  34. System.out.println("Wife " + Account.deposit());
  35. }
  36. }
  37.  
  38. public static class Account {
  39. public static int account;
  40.  
  41. public static int deposit(){
  42. account += 300;
  43. return account;
  44. }
  45. }
  46. }
Success #stdin #stdout 0.1s 321216KB
stdin
Standard input is empty
stdout
Wife 300
Husband 600
Wife 900
Husband 1200
Wife 1500
Husband 1800
Wife 2100
Husband 2400
Wife 2700
Husband 3000
Wife 3300
Husband 3600
Wife 3900
Husband 4200
Wife 4500
Husband 4800
Wife 5100
Husband 5400
Wife 5700
Husband 6000