aspose file tools*
The moose likes Threads and Synchronization and the fly likes Threads with MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads with MVC" Watch "Threads with MVC" New topic
Author

Threads with MVC

Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Hello everybody I'm developing an application on Desktop and Database, decided to use MVC because I love this model and also think it applies better in my application ....
In my application I have to insert a large volume of records in the database and therefore decide to do this in a separate thread, my question is how to integrate Threads in my MVC model, I make my controller class implement Runnable? or should I create a separate class to implement the Thread?
Thanks to anyone who can answer me!
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
At a high level, it looks like you may need many threads. At least one each for M-V-C may be a good way
to start thinking about the problem. Your questions about how to create, launch and manage the threads
may be a bit premature, however, as you may want to think through your design a bit more first. What
kind of UI do you plan to have? Is this to be web-based (now or in the future) or a local .exe? What does
the data model look like? Is your database storage local or remote?

But maybe your have already answered these questions. So in answer to your specific question, it is
generally recommended that you build your own "thread-level" by implementing Runnable. This will
give you design flexibility in the class and also provide better encapsulation.

Jim ... ...


BEE MBA PMP SCJP-6
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Hello Jim thanks for the help, my application is Desktop and the bank data is local, my big question is how to integrate Threads and MVC, thank you for your help.
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

I think it all depends on your personal style and how you application flows. I have an application that implements threads in two different ways, one in the controller and the other in the model. It was done this way because that's what made the most sense.
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Hello everybody, I have a doubt, the following code is a good programming practice?



The above is a good programming practice?
was the most appropriate way I found to solve my doubt who was responsible for creating this topic
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
In my application I have to insert a large volume of records in the database and therefore decide to do this in a separate thread


Looks like you are making a pre-mature decision to use threads, and also to use the MVC pattern.

Also, thread creation should be contained within the Model application, not the Controlller.

The purpose of the Controller is to simply mediate between a View and the Model, that is all.
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Frank Bennett wrote:
In my application I have to insert a large volume of records in the database and therefore decide to do this in a separate thread


Looks like you are making a pre-mature decision to use threads, and also to use the MVC pattern.

Also, thread creation should be contained within the Model application, not the Controlller.

The purpose of the Controller is to simply mediate between a View and the Model, that is all.


But the code above (just an example) would be the logic of my application and the application logic is in the controller, the above is a good programming practice?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Vinicius : As Frank and I have said, what you are proposing and asking
is unclear and discussing code details seems premature. But to answer
your specific question, controlling repeated execution of a process by
looping, as you do in longTask(), is not considered good practice. Threads
should communicate and coordinate activities using shared objects and the
synchronization mechanisms provided by the JVM.

Jim ... ...
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Hello Jim, really was premature to discuss the code, but as the code above is not a good practice, how I could implement Threads in the specific case of my system to handle large volume of data?
Because in my system I'll have to insert a large amount of data in the database and I would like to use threads to prevent interface freezes in time the system is performing operations on the database.
Could you pass me a tutorial example showing how to work with database and threads?
Thanks for your help!
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Jim Hoglund wrote: Threads should communicate and coordinate activities using shared objects and the
synchronization mechanisms provided by the JVM.
...


I don't necessarily agree on this. Threads are also useful to run processes in parallel, specially I/O intensive or database calls.

It makes good sense if,
//here I have my job that I want to run in another thread
public void longTask {
//complicated loop
for(int i = 0; i < 1000000000; i++) {
//somewhere process
//Some database operation
Thread.sleep(100);
}
}

On the other hand I'm not a fan of using threads every time you feel you need additional performance.
For e.g a bulk update to the database is way better than multi-threaded single updates.

Coming to your questions, I'm not a desktop app developer. I agree with Richard - on the desktop side the MVC can be a little fuzzed and could depend on you style.

Coming to your final question, I see nothing particularly wrong with the way you have designed your multi threaded container. Design question are difficult to answer without really knowing the whole picture. A lot of time it depends on what you need to accomplish!


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Mercs Hello Sam thanks for the help, really questions about the code development is very complicated has many different opnion hint that I'm using is to develop programs more intuitive, my big concern is escape of good programming practices.
Thanks for your help!
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Sam : Yes, running parallel processes is a good use of coordinated treads.
We can't assume, however, that better performance will always be a result.
Since there is often only one CPU, sharing it across many threads increases
overhead. So the speed of completing work may actually decrease.

Sam's suggestion "For e.g a bulk update to the database is way better than
multi-threaded single updates..." can make a lot of sense. Is the data available
in bulk, or does it dribble in? If so, can a number of updates be accumulated
before hitting the database?

A better algorithm usually wins over a faster CPU. (I wonder who first said that.)

Jim ... ...
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
In my application I have to load data from a dbf file and insert into a MySQL database, using threads will be necessary to avoid the interface freezes.
The use of SwingWorker is a good solution?

PS: Sorry for my vocabulary because I'm Brazilian and have difficulties in English language.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Your English is just fine. Have we used terms that should be explained
better? And can you describe the "interface freezes" a bit more?

Jim ... ...
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Jim Thanks for the compliment!
When I refer to "interface freeze" I am referring to my form GUI
For example, in the case of my system I would like my form data entry inform the user the progress of the registration information in the database, for this I thought of using a JProgressBar.
But to do that I have to create a form so that when the process runs, will not occur in the GUI locking my form because I have to insert a large amount of data
My idea at first was to create a parallel process (another thread) and in it I would perform the actions in the database and would upgrading my GUI form in case my progress bar!
This is my problem and I have several questions on how to fix it!
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
How about launching the update thread, and having the GUI
thread watch it's progress, updating the progress bar? Does
the GUI need to be doing other things too while the updates
are being processed? Or are there other processes that need
to be running at the same time?

Jim ... ...
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Basically in my gui I want you to be a JProgressBar indicating the actual process of entering information in the database.
To avoid problems in the performance of my GUI I think a thread that runs parallel process in the database and update my progress bar and it worked properly.
But my question is how to integrate it to the MVC model, the code below I demonstrate a reduced form as I developed, I will not put all the code because it is too long but if you prefer I lay.

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Vinicius : I will be off-line until next Tuesday, but I will look at
your outline this weekend. See you then.

Jim ... ...
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Alright Jim!
Thank you for your help!
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Hello everybody, I looked at some tutorials on SwingWorker, does in my case it can be applied to replace the threads?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Hi Vinicius - I'm back in town. Looking at your p-code from a MVC perspective, there seems
to be some overlap of the three functions. As Frank Bennett suggested, the Controller could
mediate between the View (possibly made of Swing components) and the Model or database.
One way to force this partitioning would be to let the Controller class run in the main thread,
have it launch a second thread for the View and third thread for the data Model. Appropriate
messages could then be sent between the threads. If you can slice what you have into these
three buckets, we could go from there.

Jim ... ...
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Hello Jim, thank you for help, but I am not able to understand , is there any tutorial that can help me!
Or you could give me some small code example?
Thanks
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
After much discussion, I applied the SwingWorker in my application and the results were satisfactory, thank you Jim for your help!
thomas soares
Greenhorn

Joined: Oct 22, 2012
Posts: 1
Hello guys

I'm having the same troubles with Threads and MVC.
However, my application is web and I have to insert a lot of informartion on the DB and I'm using MVC to develop my system.
My specific question is: Which layer should I use Threads or should I create a new class only for the Threads ?

Sorry for the poor english, I'm a brazilian.

Thks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads with MVC
 
Similar Threads
JTable Question
SWING, MVC, Thread-Controller
MVC and my design
JDBC in Action class...???
Separating View-Controller in MVC