Java 哈希表的线程安全

Java 哈希表的线程安全,java,hashtable,Java,Hashtable,我有一个哈希表 现在,由于哈希表是线程安全的,所以每当我向哈希表中的列表中添加内容时,它也是线程安全的吗 乙二醇 这些操作是线程安全的吗?不,绝对不是。哈希表上各个操作的线程安全性根本不会影响您放入哈希表的内容的线程安全性。因此,例如,如果将ArrayList引用放入哈希表中,这不是线程安全的,因此如果这些线程中有任何线程将修改数据,则不应在没有同步的情况下从多个线程中使用它; package legacyCLasses; import java.util.*; public

我有一个哈希表

现在,由于哈希表是线程安全的,所以每当我向哈希表中的列表中添加内容时,它也是线程安全的吗

乙二醇


这些操作是线程安全的吗?

不,绝对不是。哈希表上各个操作的线程安全性根本不会影响您放入哈希表的内容的线程安全性。因此,例如,如果将
ArrayList
引用放入哈希表中,这不是线程安全的,因此如果这些线程中有任何线程将修改数据,则不应在没有同步的情况下从多个线程中使用它;
   package legacyCLasses;
   import java.util.*;
   public class HashTable_With_Enumeration implements Runnable
   {
    Hashtable<Integer,String> htobj=new Hashtable<Integer,String>();
    static int count=1;
    public void run() 
    {
            this.htobj.put(10,"ABC");
            this.htobj.put(200,"PQR");
            this.htobj.put(1,"JKL");
            this.dis();
    }
    void dis()
    {
        try
        {
            Enumeration<String> eobj=this.htobj.elements();
            while(eobj.hasMoreElements())
            {
                System.out.print(eobj.nextElement());
                if(count%2!=0)
                {
                    System.out.print("\t\t\t\t");
                    count++;
                }
                else
                {
                    System.out.println();
                    count++;
                }
                Thread.sleep(2000);
            }           
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    public static void main(String[] args)
    {
        HashTable_With_Enumeration heobj1=new HashTable_With_Enumeration();
        HashTable_With_Enumeration heobj2=new HashTable_With_Enumeration();     
        Thread t=new Thread(heobj1,"HashTable_Thread1");
        Thread t2=new Thread(heobj2,"HashTable_Thread2");
        try
        {
            System.out.print("\n"+t.getName()+" Values \t"+t2.getName()+ "Values     
              \n");                     
            t.start();  
            Thread.sleep(500);          
            t2.start();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
      }
   } 
导入java.util.*; 带有枚举的公共类哈希表实现可运行 { Hashtable htobj=新的Hashtable(); 静态整数计数=1; 公开募捐 { 本.htobj.put(10,“ABC”); 本.htobj.put(200,“PQR”); 本.htobj.put(1,“JKL”); 这是dis(); } 无效dis() { 尝试 { 枚举eobj=this.htobj.elements(); while(eobj.hasMoreElements()) { System.out.print(eobj.nextElement()); 如果(计数%2!=0) { 系统输出打印(“\t\t\t\t”); 计数++; } 其他的 { System.out.println(); 计数++; } 《睡眠》(2000年); } } 捕获(例外e) { System.out.println(e.getMessage()); } } 公共静态void main(字符串[]args) { HashTable_With_枚举heobj1=新的HashTable_With_枚举(); HashTable_With_枚举heobj2=新的HashTable_With_枚举(); 螺纹t=新螺纹(heobj1,“哈希表_螺纹1”); 螺纹t2=新螺纹(heobj2,“哈希表_螺纹2”); 尝试 { System.out.print(“\n”+t.getName()+”值\t”+t2.getName()+”值 \n”); t、 start(); 睡眠(500); t2.start(); } 捕获(例外e) { System.out.println(e.getMessage()); } } }
这取决于存储在哈希表中的列表。请记住,
add
是对存储在哈希表中的对象调用的,因此该对象必须是线程安全的。因此,如果我使用哈希表,那么我认为我的问题已经解决。请您发表意见好吗?@akshay:是的,应该可以。您是否确实需要
哈希表而不是(比如)ConcurrentHashMap
?请解释这是如何回答问题的?
   package legacyCLasses;
   import java.util.*;
   public class HashTable_With_Enumeration implements Runnable
   {
    Hashtable<Integer,String> htobj=new Hashtable<Integer,String>();
    static int count=1;
    public void run() 
    {
            this.htobj.put(10,"ABC");
            this.htobj.put(200,"PQR");
            this.htobj.put(1,"JKL");
            this.dis();
    }
    void dis()
    {
        try
        {
            Enumeration<String> eobj=this.htobj.elements();
            while(eobj.hasMoreElements())
            {
                System.out.print(eobj.nextElement());
                if(count%2!=0)
                {
                    System.out.print("\t\t\t\t");
                    count++;
                }
                else
                {
                    System.out.println();
                    count++;
                }
                Thread.sleep(2000);
            }           
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    public static void main(String[] args)
    {
        HashTable_With_Enumeration heobj1=new HashTable_With_Enumeration();
        HashTable_With_Enumeration heobj2=new HashTable_With_Enumeration();     
        Thread t=new Thread(heobj1,"HashTable_Thread1");
        Thread t2=new Thread(heobj2,"HashTable_Thread2");
        try
        {
            System.out.print("\n"+t.getName()+" Values \t"+t2.getName()+ "Values     
              \n");                     
            t.start();  
            Thread.sleep(500);          
            t2.start();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
      }
   }