You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultipleThreadsTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. *
  23. */
  24. package org.dcevm.test.body;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import static org.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  28. import static org.dcevm.test.util.HotSwapTestHelper.__version__;
  29. import static org.junit.Assert.assertFalse;
  30. import static org.junit.Assert.assertTrue;
  31. /**
  32. * Class for testing redefining methods of classes that extend the Thread class. In the test setup the run method
  33. * calls the doit method in a loop until this methods returns false.
  34. *
  35. * @author Thomas Wuerthinger
  36. */
  37. public class MultipleThreadsTest {
  38. public static final int COUNT = 10;
  39. // Version 0
  40. public static class A extends Thread {
  41. private int value;
  42. private int value2;
  43. private boolean flag = false;
  44. @Override
  45. public void run() {
  46. while (doit()) {
  47. flag = false;
  48. }
  49. }
  50. public boolean doit() {
  51. if (flag) {
  52. throw new RuntimeException("Must not reach here");
  53. }
  54. flag = true;
  55. try {
  56. Thread.sleep(1);
  57. } catch (InterruptedException e) {
  58. }
  59. value++;
  60. return true;
  61. }
  62. public int getValue() {
  63. return value;
  64. }
  65. public int getValue2() {
  66. return value2;
  67. }
  68. }
  69. // Version 1
  70. public static class A___1 extends Thread {
  71. private int value;
  72. private int value2;
  73. private boolean flag = false;
  74. @Override
  75. public void run() {
  76. while (doit()) {
  77. flag = false;
  78. }
  79. }
  80. public boolean doit() {
  81. if (flag) {
  82. throw new RuntimeException("Must not reach here");
  83. }
  84. flag = true;
  85. try {
  86. Thread.sleep(1);
  87. } catch (InterruptedException e) {
  88. }
  89. value2++;
  90. return true;
  91. }
  92. public int getValue() {
  93. return value;
  94. }
  95. public int getValue2() {
  96. return value2;
  97. }
  98. }
  99. // Version 2
  100. public static class A___2 extends Thread {
  101. private int value;
  102. private int value2;
  103. private boolean flag = false;
  104. @Override
  105. public void run() {
  106. while (doit()) {
  107. flag = false;
  108. }
  109. }
  110. public boolean doit() {
  111. return false;
  112. }
  113. public int getValue() {
  114. return value;
  115. }
  116. public int getValue2() {
  117. return value2;
  118. }
  119. }
  120. @Before
  121. public void setUp() throws Exception {
  122. __toVersion__(0);
  123. }
  124. @Test
  125. public void testOneThread() {
  126. test(1);
  127. }
  128. @Test
  129. public void testThreads() {
  130. test(COUNT);
  131. }
  132. private void test(int count) {
  133. assert __version__() == 0;
  134. A[] arr = new A[count];
  135. for (int i = 0; i < count; i++) {
  136. arr[i] = new A();
  137. arr[i].start();
  138. }
  139. try {
  140. Thread.sleep(500);
  141. } catch (InterruptedException e) {
  142. }
  143. for (int i = 0; i < count; i++) {
  144. //assertTrue(arr[i].getValue() > 0);
  145. }
  146. __toVersion__(1);
  147. try {
  148. Thread.sleep(500);
  149. } catch (InterruptedException e) {
  150. }
  151. for (int i = 0; i < count; i++) {
  152. assertTrue(arr[i].getValue2() > 0);
  153. }
  154. __toVersion__(2);
  155. try {
  156. Thread.sleep(500);
  157. } catch (InterruptedException e) {
  158. }
  159. for (int i = 0; i < count; i++) {
  160. assertFalse(arr[i].isAlive());
  161. }
  162. __toVersion__(0);
  163. }
  164. }