fork download
  1. package org.sample;
  2.  
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5.  
  6. class A {
  7.  
  8. Object o = new Object();
  9.  
  10. public void hello(String holderName) {
  11. synchronized (o) {
  12. for (int i = 0; i < 10; i++) {
  13. System.out.println("hello " + holderName);
  14. try {
  15. Thread.sleep(1000);
  16. } catch (InterruptedException e) {
  17. throw new RuntimeException(e);
  18. }
  19. }
  20. }
  21. }
  22.  
  23. }
  24.  
  25. class B implements Runnable {
  26. private A a;
  27. private String name;
  28.  
  29. public A getA() {
  30. return a;
  31. }
  32.  
  33.  
  34. public B(A a, String name) {
  35. this.a = a;
  36. this.name = name;
  37. }
  38.  
  39. @Override
  40. public void run() {
  41. a.hello(name);
  42. }
  43. }
  44.  
  45.  
  46. public class Main {
  47. public static void main(String[] args) throws InterruptedException {
  48. A a = new A();
  49. B b1 = new B(a, "b1");
  50. B b2 = new B(a, "b2");
  51.  
  52. ExecutorService executorService = Executors.newCachedThreadPool();
  53. executorService.submit(b1);
  54. Thread.sleep(1000);
  55. a.o = new Object();
  56. executorService.submit(b2);
  57. }
  58. }
Runtime error #stdin #stdout #stderr 0.12s 320768KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: Could not find or load main class Main