Advertisement
apl-mhd

Spring 2017AnotherTechnique

Jan 12th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package spring2017;
  2.  
  3. import one172.ThreadErrorList5;
  4.  
  5. import java.util.ArrayList;
  6.  
  7. public class Problem2 implements  Runnable {
  8.  
  9.     static ArrayList<Integer> listNum = new ArrayList<>();
  10.  
  11.  
  12.     Thread t;
  13.     Problem2(String name){
  14.  
  15.          t = new Thread(this,name);
  16.  
  17.     }
  18.  
  19.     synchronized   void removeData(){
  20.  
  21.         //for (int i=listNum.size()-1; i>=listNum.size()-5; i--){
  22.  
  23.         int size =  listNum.size();
  24.         for (int i=size-1; i>=size-5; i--){
  25.             //System.out.println(i);
  26.  
  27.             listNum.remove(i);
  28.             //System.out.print(listNum.remove(i));
  29.         }
  30.     }
  31.  
  32.     @Override
  33.     public void run() {
  34.  
  35.        // System.out.println("bangladesh");
  36.         removeData();
  37.  
  38.             System.out.println(listNum.size()+" "+ t.getName());
  39.  
  40.     }
  41.  
  42.  
  43.     public static void main(String[] args) {
  44.  
  45.         listNum.add(0);
  46.         listNum.add(1);
  47.         listNum.add(2);
  48.         listNum.add(3);
  49.         listNum.add(4);
  50.  
  51.         listNum.add(5);
  52.         listNum.add(6);
  53.         listNum.add(7);
  54.         listNum.add(8);
  55.         listNum.add(9);
  56.         listNum.add(10);
  57.         listNum.add(11);
  58.         listNum.add(12);
  59.         listNum.add(13);
  60.         listNum.add(14);
  61.  
  62.  
  63.         Problem2 c1 = new Problem2("First Thread");
  64.         Problem2 c2 = new Problem2("Second Thread");
  65.        
  66.         c1.t.start();
  67.         c2.t.start();
  68.  
  69.         try {
  70.             c1.t.join();
  71.             c2.t.join();
  72.         }
  73.         catch (InterruptedException e){
  74.  
  75.             System.out.println(e);
  76.         }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. /*
  84.  
  85.         Thread t1 = new  Thread(new Problem2());
  86.         Thread t2 = new  Thread(new Problem2());
  87.         Thread t3 = new  Thread(new Problem2());
  88.  
  89.         t1.start();
  90.  
  91.         t2.start();
  92.         t3.start();
  93.  
  94.  
  95.  
  96.         try {
  97.             t1.join();
  98.             t2.join();
  99.            t3.join();
  100.         }
  101.         catch (InterruptedException e){
  102.  
  103.             System.out.println(e);
  104.         }
  105.  
  106. */
  107.  
  108.         System.out.println(listNum.size());
  109.  
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement