Vector, HashTable thread safe example

Below this was written Vector thread safe wording:

import java.util.Vector;
 
public class Test { 
     private static Vector<Integer> vector = new Vector<Integer>();
 
        public static void main(String[] args) {
            while (true) {
                for (int i = 0; i < 10; i++) {
                    System.out.println("添加");
                    vector.add(i);
                }
 
                Thread removeThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < vector.size(); i++) {
                            System.out.println("removeThread删除");
                            vector.remove(i);
                        }
                    }
                });
 
                Thread printThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < vector.size(); i++) {
                            System.out.println ( "Get PrintThread" ); 
 
                            System.out.println ((vector.get (I))); 
                        } 
                    } 
                }); 
 
                removeThread.start (); 
                printThread.start (); 
 
                // do not simultaneously generated too many threads, otherwise it will cause the operating system suspended animation 
               the while (Thread.activeCount ()> 20 ); 
            } 
        } 
  } }

  Although Vector get (), remove (), get () method is synchronized but I run the above program the following error:

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
    at java.util.Vector.get(Vector.java:744)
    at Test$2.run(Test.java:29)
    at java.lang.Thread.run(Thread.java:722)
Exception in thread "Thread-14857" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
    at java.util.Vector.get(Vector.java:744)
    at Test$2.run(Test.java:29)

HashTable thread safe wording:

import java.util.Hashtable;
import java.util.Map;
 
public class HashmapTest {
    
     private  static Map<Integer,Integer> hashtable= new Hashtable<Integer,Integer>();
      public static void main(String[] args) {
       while(true){
           for (int i = 0; i < 10; i++) {
                   System.out.println("添加");
                 hashtable.put(i, i);
           }
           Thread removeThread = new Thread(new Runnable() {
            @Override
            public void run() {
             Iterator it = hashtable.entrySet().iterator();
                  while (it.hasNext()) {
                  Map.Entry<integer integer=""> entry=(Entry<integer integer="">) it.next();  
                           System.out.println("delete this: "+entry.getKey()+"==="+entry.getValue());  
                            it.remove(); 
                } 
                    }
                     
           
           Thread  getThread = new Thread(new Runnable() {
            @Override
            public void run() {
                 for (int i = 0; i < hashtable.size(); i++) {
                     System.out.println("getThread获取");
                     System.out.println((hashtable.get(i)));
                 }            
            }
        });
           removeThread.start();
           getThread.start();
          while (Thread.activeCount() > 20);
         }
      }
}

  There will be a lot of null values, but good, because without that key, but does not complain

getThread get
 null 
getThread get 
null

  In a multithreaded environment, if not in the end make the method call additional synchronization measures, the use of this thread is still insecure, because if another thread just the wrong time to delete an element, resulting in i do not it is available, get method will throw an ArrayIndexOutOfBoundsException

import java.util.Vector;
 
public class Test { 
     private static Vector<Integer> vector = new Vector<Integer>();
 
        public static void main(String[] args) {
            while (true) {
                for (int i = 0; i < 10; i++) {
                    System.out.println("添加");
                    vector.add(i);
                }
 
                Thread removeThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (vector) {
                              for (int i = 0; i < vector.size(); i++) {
                                  System.out.println("removeThread删除");
                                  vector.remove(i);
                              }
                        }
                    }
                });
 
                Thread printThread = new Thread(new Runnable() {
                    @Override
                    public voidRUN () {
                         the synchronized (Vector) {
                         for ( int I = 0; I <vector.size (); I ++ ) { 
                            System.out.println ( "Get PrintThread" ); 
 
                            System.out.println ((vector.get ( i))); 
                        } 
                        } 
                    } 
                }); 
 
                removeThread.start (); 
                printThread.start (); 
 
                // do not produce too many threads at the same time, otherwise it will cause the operating system suspended animation 
               the while (Thread.activeCount ()> 20 ); 
            } 
      } 
 }

Guess you like

Origin www.cnblogs.com/jing99/p/11306496.html