Advertisement
apl-mhd

RemoveFRomArrayListThreadsequentially

Jan 10th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. package one172;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5.  
  6. public class RemoveFromArrayListThread2 implements  Runnable{
  7.  
  8.     static ArrayList<Integer> arrayList = new ArrayList<>();
  9.     synchronized void removeList(ArrayList<Integer> arr){
  10.  
  11.         int count=0;
  12.  
  13.         Iterator<Integer> it = arr.iterator();
  14.         while (it.hasNext()){
  15.  
  16.             it.next();
  17.             it.remove();
  18.  
  19.             count++;
  20.             if (count==5)
  21.                 break;
  22.         }
  23.  
  24.  
  25.     }
  26.  
  27.     @Override
  28.     public void run() {
  29.  
  30.         //remove(arrayList);
  31.  
  32.  
  33.         removeList(arrayList);
  34.  
  35.         /*for(int i=0; i<5; i++){
  36.  
  37.             System.out.println(arrayList.get(i));
  38.  
  39.         }*/
  40.     }
  41.  
  42.     public static void main(String[] args) {
  43.  
  44.         arrayList.add(1);
  45.         arrayList.add(2);
  46.         arrayList.add(3);
  47.         arrayList.add(4);
  48.         arrayList.add(5);
  49.         arrayList.add(6);
  50.  
  51.         arrayList.add(7);
  52.         arrayList.add(8);
  53.         arrayList.add(9);
  54.         arrayList.add(10);
  55.  
  56.  
  57.         Thread t1 = new Thread(new RemoveFromArrayListThread2());
  58.         Thread t2 = new Thread(new RemoveFromArrayListThread2());
  59.         Thread t3 = new Thread(new RemoveFromArrayListThread2());
  60.  
  61.         t1.start();
  62.         t2.start();
  63.        // t3.start();
  64.  
  65.         try {
  66.             t1.join();
  67.             t2.join();
  68.            //t3.join();
  69.         }
  70.         catch (InterruptedException e){
  71.             e.printStackTrace();
  72.         }
  73.  
  74.  
  75.         System.out.println(arrayList.size());
  76.  
  77.     }
  78.  
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement