• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Code Optimizatoin

 
Greenhorn
Posts: 15
MyEclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to optimize this code..
Can anyone please help..


Please suggest me..
 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Optimize for what?

(I doubt anyone will go through 360 lines of code and make suggestions, by the way, but you never know.)
 
Arun vel
Greenhorn
Posts: 15
MyEclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By this code I am doing the same function for two types of records..For that I have repeated the same logic twice in two classes..Other than the numner of parameters passed,there is no other difference between those two classes..
I want to know how use the same funcetionality but with different parameters..
Thus the code will be optimized..
If you see the code correctly,you will find that the same logic is repeated twice in two classes..
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun kumar.ck wrote:By this code I am doing the same function for two types of records..For that I have repeated the same logic twice in two classes..Other than the numner of parameters passed,there is no other difference between those two classes..
I want to know how use the same funcetionality but with different parameters..
Thus the code will be optimized..
If you see the code correctly,you will find that the same logic is repeated twice in two classes..



So what specific problem are you having doing that?

Again, that's a lot of code to ask anyone to read. I'm certainly not going to read it. Perhaps if you could condense it down to just enough code to show the relevant bits that are giving you trouble...
 
Arun vel
Greenhorn
Posts: 15
MyEclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay..Now I wil explain what I need to know..
I have created two classes(Thread class) to read two types of records(Textfiles-Both have different number of values)..
Both classes does same process but with two type of files(Textfiles-Both have different number of values)..Both class looks same as one..Both class uses same insertion method to insert the textfile data into the database..
Now I want to know,Instead of doing this can we create only one class(Thread class) to do both the functions..
I cant condense it because there is nothing rather than the same class is repeated twice..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun kumar.ck wrote:I have created two classes(Thread class) to read two types of records(Textfiles-Both have different number of values)..
Both classes does same process but with two type of files(Textfiles-Both have different number of values)..Both class looks same as one..Both class uses same insertion method to insert the textfile data into the database..



Okay, so both classes are the same. So get rid of one class and just use the remaining one for both tasks.

Also, you shouldn't extend Thread. You should implement Runnable instead.

I cant condense it because there is nothing rather than the same class is repeated twice..



I guarantee you it's possible to condense it.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun kumar.ck wrote:Now I want to know,Instead of doing this can we create only one class(Thread class) to do both the functions.


Yes. The question is: do you want to?

Personally, I think I'd be creating classes (Student? Exercise?) for the objects that you want to read in, and put the code for actually reading them in there.

I cant condense it because there is nothing rather than the same class is repeated twice..


As Jeff said...

Winston
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Also, you shouldn't extend Thread. You should implement Runnable instead.


Why is that better? The only advantage I see with the "implements" approach is that you can implement multiple interface, but extend from only 1 class. Are there any other benefits over the "implements" approach?
 
Tim Moores
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See https://coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable for some discussion. Using Callable or Future may be even better, depending on what you're trying to achieve.
 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:See https://coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable for some discussion. Using Callable or Future may be even better, depending on what you're trying to achieve.


Thanks; that makes sense. To be honest I've never used Executor and Callable before but it's definitely something to look into next opportunity.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Koen Aerts wrote:

Jeff Verdegan wrote:Also, you shouldn't extend Thread. You should implement Runnable instead.


Why is that better?



Because it more properly reflects your design. We're not creating a specialized type of Thread; we're creating a task to be run in its own thread.

Granted, it is convenient for anonymous inner classes to be able to skip the Runnable layer, and I'm guilty of doing it, but I still consider it bad design that Thread even implements Runnable in the first place.
 
Arun vel
Greenhorn
Posts: 15
MyEclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for the suggestions..I have done that..
The problem with me was I know the way to do,but dont know how o do..
Now my application is working fine..
Also I am not that much explainatory..So only I am not able to explain myself..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic