Threading opinions

Posted on 2004-11-26
Last Modified: 2010-03-31
   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!
Question by:petepalmer
    LVL 86

    Accepted Solution

    It's usually more flexible to use Runnable:

    class DataStorer implements Runnable {
          public void run() {
    LVL 1

    Author Comment

    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?
    LVL 86

    Expert Comment

    The main difference is that you don't need to subclass Thread itself, so you have greater OO scope
    LVL 1

    Author Comment

    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...
    LVL 35

    Expert Comment

    > 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:


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now