After a long research , I got to know that String is immutable .String Buffer is more efficient than String if the program involves many computations. But my question is slightly different from these
I have a function to which I pass a string . The string is actually the text of an article (nearly 3000-5000 charcs) .The function is implemented in threads. I mean to say , there is multiple call of function with different String text each time ..The later stage computations in the functions are too vast . Now when I run my code for a large number of threads, I am getting an error saying : GC Overhead Limit Exceeded . .
Now that I cant reduce the computations in the later stage of functions , my question is will it really help if I change the text type from String to String buffer? Also ,I don’t do any concatenation operation on the text string .
I have posted a small snipet of my code :
public static List<Thread> thread_starter(List<Thread> threads,String filename,ArrayList<String> prop,Logger L,Logger L1,int seq_no)
{ String text="";
if(prop.get(7).matches("txt"))
text=read_contents.read_from_txt(filename,L,L1);
else if(prop.get(7).matches("xml"))
text=read_contents.read_from_xml(filename,L,L1);
else if(prop.get(7).matches("html"))
text=read_contents.read_from_html(filename,L,L1);
else
{
System.out.println("not a valid config");
L1.info("Error : config file not properly defined for i/p file type");
}
/*TODO */
//System.out.println(text);
/*TODO CHANGES TO BE DONE HERE */
if(text.length()>0)
{
Runnable task = new MyRunnable(text,filename,prop,filename,L,L1,seq_no);
Thread worker = new Thread(task);
worker.start();
// Remember the thread for later usage
threads.add(worker);
}
else
{
main_entry_class.file_mover(filename, false);
}
return threads;
}
And i'm calling the above function repeatedly using the following code :
List<Thread> threads = new ArrayList<Thread>();
thread_count=10;
int file_pointer=0;// INTEGER POINTER VARIABLE
do
{
if(file.size()<=file_pointer)
break;
else
{ String file_name=file.get(file_pointer);
threads=thread_starter(threads,file_name,prop,L,L1,seq_no);
file_pointer++;
seq_no++;
}
}while(check_status(threads,thread_count)==true);
And the check status function :
public static boolean check_status(List<Thread> threads,int thread_count)
{
int running = 0;
boolean flag=false;
do {
running = 0;
for (Thread thread : threads) {
if (thread.isAlive()) {
//ThreadMXBean thMxB = ManagementFactory.getThreadMXBean();
//System.out.println(thMxB.getCurrentThreadCpuTime());
running++;
}
}
if(Thread.activeCount()-1<thread_count)
{
flag=true;
break;
}
} while (running > 0);
return flag;
}