user3339716
user3339716

Reputation: 1

Why does a concurrent hash map work properly when accessed by two thread, one using the clear() and other using the putifAbsent() methods?

I am implementing an application using concurrent hash maps. It is required that one thread adds data into the CHM, while there is another thread that copies the values currently in the CHM and erases it using the clear() method. When I run it, after the clear() method is executed, the CHM always remains empty, though the other thread continues adding data to CHM. Could someone tell me why it is so and help me find the solution.

This is the method that adds data to the CHM. This method is called from within a thread.

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
public static ConcurrentMap<String, String> updateJobList = new ConcurrentHashMap<String, String>(8, 0.9f, 6);   
public void setUpdateQuery(String ticker, String query)
        throws RemoteException {  
    dataBaseText = "streamming";  
   int n = 0;  
    try {  
         updateJobList.putIfAbsent(ticker, query);  
        }  
    catch(Exception e)
    {e.printStackTrace();}   
     ........................
 }

Another thread calls the track_allocation method every minute.

public void track_allocation()
{
        class Track_Thread implements Runnable {
            String[] track;
            Track_Thread(String[] s)
            {
                track = s;
            }
            public void run()
            {

            }
            public void run(String[] s)
            {
                  MonitoringForm.txtInforamtion.append(Thread.currentThread()+"has     started runnning");
                String query = "";
                track = getMaxBenefit(track);
                track = quickSort(track, 0, track.length-1);
                for(int x=0;x<track.length;x++)
                {
                    query = track[x].split(",")[0];
                    try
                    {
                    DatabaseConnection.insertQuery(query);
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
        joblist = updateJobList.values();
         MonitoringForm.txtInforamtion.append("\nSize of the joblist is:"+joblist.size());
        int n = joblist.size()/6;
        String[][] jobs = new String[6][n+6];
          MonitoringForm.txtInforamtion.append("number of threads:"+n);
        int i = 0;
        if(n>0)
        {
            MonitoringForm.txtInforamtion.append("\nSize of the joblist is:"+joblist.size());
           synchronized(this)
           {
            updateJobList.clear();
           }
            Thread[] threads = new Thread[6];
           Iterator it = joblist.iterator();
           int k = 0;
            for(int j=0;j<6; j++)
            {    
                for(k = 0; k<n; k++)                    
                { 
                    jobs[j][k] = it.next().toString();                   

                    MonitoringForm.txtInforamtion.append("\n\ninserted into queue:\n"+jobs[j][k]+"\n");

                }
                if(it.hasNext() && j == 5)
                {
                    while(it.hasNext())
                    {
                        jobs[j][++k] = it.next().toString();
                    }
                }
                threads[j] = new Thread(new Track_Thread(jobs[j]));
                threads[j].start();

            }
        }
}

Upvotes: 0

Views: 154

Answers (1)

Stephen C
Stephen C

Reputation: 719189

I can see a glaring mistake. This is the implementation of your Track_Thread classes run method.

    public void run()
        {

        }

So, when you do this:

   threads[j] = new Thread(new Track_Thread(jobs[j]));
   threads[j].start();

..... the thread starts, and then immediately ends, having done absolutely nothing. Your run(String[]) method is never called!


In addition, your approach of iterating the map and then clearing it while other threads are simultaneously adding is likely to lead to entries occasionally being removed from the map without the iteration actually seeing them.


While I have your attention, you have a lot of style errors in your code:

  • The indentation is a mess.
  • You have named your class incorrectly: it is NOT a thread, and that identifier ignores the Java identifier rule.
  • Your use of white-space in statements is inconsistent.

These things make your code hard to read ... and to be frank, they put me off trying to really understand it.

Upvotes: 3

Related Questions