fork download
  1. import java.lang.*;
  2. import java.lang.ref.WeakReference;
  3.  
  4. class Ideone
  5. {
  6. static class IntArray
  7. {
  8. WeakReference<Object> self;
  9. int[] array1;
  10. int size;
  11.  
  12. class Setter implements Runnable
  13. {
  14. private int index;
  15. private int value;
  16. Setter(int index, int value)
  17. {
  18. this.index = index;
  19. this.value = value;
  20. }
  21. @Override
  22. public void run()
  23. {
  24. if (index < 0) index = -index;
  25. if (index >= size) index = index % size;
  26. synchronized (self)
  27. {
  28. int[] tmp = array1.clone();
  29. tmp[index] = value;
  30. array1 = tmp;
  31. }
  32. }
  33. }
  34.  
  35. IntArray(int size)
  36. {
  37. this.size = size;
  38. array1 = new int[size];
  39. self = new WeakReference<Object>(this);
  40. }
  41.  
  42. public void setValue(int index, int value)
  43. {
  44. Thread thread = new Thread(new Setter(index, value));
  45. thread.start();
  46. }
  47.  
  48. public int getValue(int index)
  49. {
  50. return array1[index];
  51. }
  52.  
  53. public int length()
  54. {
  55. return size;
  56. }
  57. }
  58.  
  59. public static void main (String[] args) throws java.lang.Exception
  60. {
  61. IntArray array = new IntArray(10);
  62.  
  63. for (int i = 0; i < 10; ++i)
  64. {
  65. array.setValue(i, i * 10);
  66. array.setValue(i, i * 20);
  67. }
  68.  
  69. for (int i = 0; i < 10; ++i)
  70. {
  71. array.setValue(i, i + i + 5);
  72. }
  73.  
  74. for (int i = 0; i < 10; ++i)
  75. {
  76. System.out.println("1) " + i + ": = " + array.getValue(i));
  77. }
  78. }
  79. }
Runtime error #stdin #stdout #stderr 0.08s 382784KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
	at java.lang.Thread.start0(Native Method)
	at java.lang.Thread.start(Thread.java:693)
	at Ideone$IntArray.setValue(Main.java:45)
	at Ideone.main(Main.java:66)