Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class user{
  5. public String name;
  6. }
  7.  
  8. class Thread1 implements Runnable
  9. {
  10. Thread mythread ;
  11. ArrayList<user> list;
  12. Thread1(ArrayList<user> l)
  13. {
  14. list = l;
  15. mythread = new Thread(this, "Thread1");
  16. System.out.println("Thread1 created");
  17. mythread.start();
  18. }
  19. public void run()
  20. {
  21. try
  22. {
  23. for (int i=0 ;list.size()<1000;i++)
  24. {
  25.  
  26. list.add(new user());
  27. System.out.println("Adding items " + i);
  28. Thread.sleep(1);
  29.  
  30. }
  31. }
  32. catch(InterruptedException e)
  33. {
  34. System.out.println("Thread1 interrupted");
  35. }
  36. System.out.println("Thread1 run is over" );
  37. }
  38. }
  39.  
  40. class Thread2 implements Runnable
  41. {
  42. Thread mythread ;
  43. ArrayList<user> list;
  44. Thread2(ArrayList<user> l)
  45. {
  46. mythread = new Thread(this, "Thread2");
  47. System.out.println("Thread2 created");
  48. list = l;
  49. mythread.start();
  50. }
  51. public void run()
  52. {
  53. try
  54. {
  55. for (int i=0 ;i<1000;i++)
  56. {
  57. System.out.println("Removing objects " + i);
  58.  
  59. list.remove(list.size() -1);
  60. Thread.sleep(1);
  61. }
  62. }
  63. catch(InterruptedException e)
  64. {
  65. System.out.println("Thread2 interrupted");
  66. }
  67. System.out.println("Thread2 run is over" );
  68. }
  69. }
  70. public class DemoThread
  71. {
  72. public static void main(String args[])
  73. {
  74. ArrayList<user> list = new ArrayList<user>();
  75. Thread1 t1 = new Thread1(list);
  76. Thread2 t2 = new Thread2(list);
  77. try
  78. {
  79. while(t1.mythread.isAlive() || t2.mythread.isAlive())
  80. {
  81. System.out.println(" Size : " + list.size());
  82. Thread.sleep(1);
  83. }
  84.  
  85. }
  86. catch(InterruptedException e)
  87. {
  88. System.out.println("Main thread interrupted");
  89. }
  90. System.out.println("Main thread run is over" );
  91. }
  92. }
  93.  
  94. After 6
  95.  
  96. import java.io.*;
  97. import java.util.*;
  98.  
  99. class user{
  100. public String name;
  101. }
  102.  
  103. class Thread1 implements Runnable
  104. {
  105. Thread mythread ;
  106. ArrayList<user> list;
  107. Thread1(ArrayList<user> l)
  108. {
  109. list = l;
  110. mythread = new Thread(this, "Thread1");
  111. System.out.println("Thread1 created");
  112. mythread.start();
  113. }
  114. public void run()
  115. {
  116. try
  117. {
  118. for (int i=0 ;list.size()<1000;i++)
  119. {
  120. while(list.size() == 1000){
  121. Thread.sleep(1);
  122. }
  123. list.add(new user());
  124. System.out.println("Adding items " + i);
  125.  
  126.  
  127. }
  128. }
  129. catch(InterruptedException e)
  130. {
  131. System.out.println("Thread1 interrupted");
  132. }
  133. System.out.println("Thread1 run is over" );
  134. }
  135. }
  136.  
  137. class Thread2 implements Runnable
  138. {
  139. Thread mythread ;
  140. ArrayList<user> list;
  141. Thread2(ArrayList<user> l)
  142. {
  143. mythread = new Thread(this, "Thread2");
  144. System.out.println("Thread2 created");
  145. list = l;
  146. mythread.start();
  147. }
  148. public void run()
  149. {
  150. try
  151. {
  152. for (int i=0 ;i<1000;i++)
  153. {
  154. System.out.println("Removing objects " + i);
  155. while (list.size() == 0){
  156. Thread.sleep(1);
  157. }
  158. list.remove(list.size() -1);
  159. }
  160. }
  161. catch(InterruptedException e)
  162. {
  163. System.out.println("Thread2 interrupted");
  164. }
  165. System.out.println("Thread2 run is over" );
  166. }
  167. }
  168. public class DemoThread
  169. {
  170. public static void main(String args[])
  171. {
  172. ArrayList<user> list = new ArrayList<user>();
  173. Thread1 t1 = new Thread1(list);
  174. Thread2 t2 = new Thread2(list);
  175. try
  176. {
  177. while(t1.mythread.isAlive() || t2.mythread.isAlive())
  178. {
  179. System.out.println(" Size : " + list.size());
  180. Thread.sleep(1);
  181. }
  182.  
  183. }
  184. catch(InterruptedException e)
  185. {
  186. System.out.println("Main thread interrupted");
  187. }
  188. System.out.println("Main thread run is over" );
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement