Joey Roosing
Joey Roosing

Reputation: 2155

Java Multithreading unclarity

I know this is the so manieth multithreading thread. I have read trough a good amount of them. And I have read basic threading tutorials. But none gave answers to my questions, whom I have several of.

Question1:

When creating a class that implements the Runnable interface, I have to implement the Run() method.

If I create a new thread like:

Thread t1 = new Thread();

It executes the run method upon calling the start method right?

Now I want my Thread t1 to execute another method in a thread.

I am facing the following situation:

    tblGames = new JTable(new GamesTableModel(mainController.retrieveGames()));

I think this is nasty coding. I reckon it would be better to instantiate a list variable like so:

List<Game> games = mainController.retrieveGames();

What happens now is, the GUI freezes when loading the table content(using JPA) So I want to run that in another thread. I am using a GamesTableModel to handle the columns etc for the JTable, hence I pass a List in the constructor of new GamesTableModel

Where do I create a new thread, do I create it the MainController class, and if so,

(concerning method looks like this:)

    public List<Game> retrieveGames(){
    List<Game> games = gameRepository.getGames();

    return games;
}

How will I go about running the content of that method, or the entire method in a different thread, actually, it would nice be to have the whole MainController in a new thread, so that it can never freeze up the UI, or is that not advisable?

If advisable, do I need to create a thread in the UI? whom then leads to the question, how will I run

MainController mainController = new MainController();

in a new thread?

I have never properly implemented threads in a program before, hence my newb questions.

My main concern with the run method from the interface is, the content applies to all threads in the implementing class rights?

I need separate threads to perform several different methods.

The main problem right now is filling the JTable's content using a new thread to get the content.

Sorry for the long tedious read. and thanks for still being here.

I hope to receive some clarification about my problems.

Upvotes: 0

Views: 170

Answers (3)

Dhruv Gairola
Dhruv Gairola

Reputation: 9182

Question1:

When creating a class that implements the Runnable interface, I have to implement the Run() method.

If I create a new thread like:

Thread t1 = new Thread(); It executes the run method upon calling the start method right?

Yes, this is correct. The Runnable interface requires you to implement the run() method, which is automatically executed when start() is performed on the thread.

I am facing the following situation:

tblGames = new JTable(new GamesTableModel(mainController.retrieveGames()));

I think this is nasty coding. I reckon it would be better to instantiate a list variable like so:

This is not necessarily nasty coding. If your intention is to, quote: I am using a GamesTableModel to handle the columns etc for the JTable, hence I pass a List in the constructer of new GamesTableModel then this is perfectly acceptable too, the only implication being on the readability of your code.

Where do I create a new thread, do I create it the MainController class, and if so, This is hard to say without knowing what your MainController class is actually doing, but from the sound of it, i'd probably guess Yes.

How will I go about running the content of that method, or the entire method in a different thread, actually, it would nice be to have the whole MainController in a new thread, so that it can never freeze up the UI, or is that not advisable?

You can define a new class which implements Runnable and instantiate an object of that class when you want it. Next, in that class' constructor, you can invoke start on the new instance. However, in swing programming, you would use the SwingWorker thread for resource intensive tasks. If you mix this resource intensive logic in the EventDispatch thread, then you UI will freeze up for sure, and i suspect this is your problem.

If advisable, do I need to create a thread in the UI? whom then leads to the question, how will I run

MainController mainController = new MainController(); in a new thread?

You need to do this in the SwingWorker thread. Here is an example of how to do this with swing programming Source. Download and run the IconDemo project.

My main concern with the run method from the interface is, the content applies to all threads in the implementing class rights?

Threads which are instances of that class will have the same run method logic. However, it is possible to create a new thread of another class with a different run method.

In conclusion, i strongly suggest you understand the concept of SwingWorkers in swing programming. I have a feeling this concept will help you solve some of the problems you're having.

Upvotes: 2

Zds
Zds

Reputation: 4359

First of all, it is often a bad idea to run long-lasting number-crunching on constructor. So, I'd first split the crunching to different method, like this:

// Initialize
MainController mainController = new MainController();
// Do the work
mainController.doStuff();

And then you can run that part in thread:

class MyThread extends Thread {
    MainController mainController;

    MyThread(MainController mainController) {
        this.mainController = mainController;
    }

    public void run() {
        mainController.doStuff();
    }
}

Now you can call elsewhere:

MainController mainController = new MainController();
MyThread myThread = new MyThread(mainController);
myThread.start();

Upvotes: 1

Peter Knego
Peter Knego

Reputation: 80340

  1. "It executes the run method upon calling the start method right?"

    Yes.

  2. All your other other questions: You should not access Swing components outside of Even Dispatching Thread (EDT). This is why you are getting the freezes. You should use SwingWorker when accessing Swing from background threads.

Upvotes: 4

Related Questions