Tony
Tony

Reputation: 3638

Java threads without affecting performance

Long story short; I've written a program that contains an infinite loop, in which a function is run continuously, and must run as quickly as is possible.

However, whilst this function completes in a microsecond time scale, I need to spawn another thread that will take considerably longer to run, but it must not affect the previous thread.

Hopefully this example will help explain things:

while (updateGUI == true) { //So, forever until terminated

        final String tableContents = parser.readTable(location, header);
        if (tableContents.length() == 0) {//No table there, nothing to do
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
                    //updateTable updates a JTable 
                    updateTable(tableContents, TableModel);
                    TableColumnModel tcm = guiTable.getColumnModel();
                }
            });
        }
     ***New thread is needed here!
    }

So what I need is for the readTable function to run an infinite number of times, however I then need to start a second thread that will also run an infinite number of times, however it will take milliseconds/seconds to complete, as it has to perform some file I/O and can take a bit of time to complete.

I've played around with extending the Thread class, and using the Executors.newCacheThreadPool to try spawning a new thread. However, anything I do causes the readTable function to slow down, and results in the table not being updated correctly, as it cannot read the data fast enough.

Chances are I need to redesign the way this loop runs, or possible just start two new threads and put the infinite looping within them instead.

The reason for it being designed this way was due to the fact that once the updateTable function runs, it returns a string that is used to update a JTable, which (as far as I know), must be done on Java's Main Dispatch Thread, as that is where the GUI's table was created.

If anyone has any suggestions I'd greatly appreciate them.

Thanks

Upvotes: 3

Views: 214

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533530

You have to be very careful to avoid overloading your machine. You long running task need to be made independent of you thread which must be fast. You also need to put a cap on how many of these are running at once. I would put a cap of one to start with.

Also you screen can only update so fast, and you can only see the screen updating so fast. I would limit the number of updates per second to 20 to start with.

BTW Setting the priority only helps if your machine is overloaded. Your goal should be to ensure it is not overloaded in the first place and then the priority shouldn't matter.

Upvotes: 3

trashgod
trashgod

Reputation: 205785

As you are updating a JTable, SwingWorker will be convenient. In this case, one worker can coexist with another, as suggested here.

Upvotes: 3

Enno Shioji
Enno Shioji

Reputation: 26882

It's very hard to guess what's going on here, but you said "results in the table not being updated correctly, as it cannot read the data fast enough". If you really mean the correctness of the code is affected by the timing not being fast enough, then your code is not thread safe and you need to use proper synchronization.

Correctness must not depend on timing, as timing of thread execution is not deterministic on standard JVMs.

Also, do not fiddle with thread priorities. Unless you are a concurrency guru trying to do something very unusual, you don't need to do this and it may make things confusing and/or break.

Upvotes: 2

Antoniossss
Antoniossss

Reputation: 32517

So if you want your "infinite" looping thread to have max priority, why are you setting priority to MAX for EDT insted of you "most precious one"?

               Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
                //updateTable updates a JTable 
                updateTable(tableContents, TableModel);
                TableColumnModel tcm = guiTable.getColumnModel();

In this piece of code current thread will be and EDT, or EDT spawned one. Why not moving that line before intering whileloop?

Upvotes: 0

Related Questions