Link to home
Start Free TrialLog in
Avatar of petepalmer
petepalmer

asked on

Threading opinions

Hi,
   This is really a follow on from my last question about storing data from a ResultSet. Anyway the processing involved in each set  ( there are three ) is quite intensive and therefore slow. I'd like to write a method for each object that basically runs the various tasks in their own thread. The server has four CPUs so this should really boost performance. I know there are various ways of using threads and was wondering what you guys thought was the best way in this case :)

Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of petepalmer
petepalmer

ASKER

That's what I thought looking at the tutorial - certainly good to have it confirmed :)

What are the real differences between the two flexibility wise?
The main difference is that you don't need to subclass Thread itself, so you have greater OO scope
I guess as Java only supports inheriting from one class, you'd have no choice but to use Runnable i.e when you write an Applet or something...
> That's what I thought looking at the tutorial - certainly good to have
> it confirmed :)

> What are the real differences between the two flexibility wise?

It all depends on what you want to do and in you system's design. You mainly implement Runnable if your class needs to subclass (needs to extend) some other class. But there are also differences about how a Runnable or a Thread work.

The Runnable interface only contains one method, the run() method and that's all you need to implement. Your Runnable class *is not* a thread itself but you use the Thread class to run it and therefore you have separate Thread objects all running the same Runnable instance (your Runnable object).

The Thread class on the other hand creates a new Thread object per thread that you want to use.

For a very good explanation between Runnable and Thread have a look here: http://developerlife.com/lessons/threadsintro/default.htm