Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Creating several instances of a program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating several instances of a program" Watch "Creating several instances of a program" New topic

Creating several instances of a program

Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I am creating a program that connects to a database, manipulates this data and then outputs some results. This program will allow different methods to be selected by the user to help with the manipulation of the database. How do I do it so that several instances of the program can be run at the same time ie, where a user connects to the database and then chooses several different methods that he/she wishes to use- each outputting different results. Please help!
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Often times, simply starting and running multiple instances a program does not lead to concurrent data access/use problems. It may be that your application would require consideration for specific synchronization issues, but without more specific (code) details, I can't say much more.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
One way is just to start multiple instances of Java. For example, in a Windows command prompt you could type:

The "start" command runs the rest of the command in a new process, so you'd see two new windows pop up and run the program.
A more sophisticated approach would run two threads in the same program. You might code MethodOne and MethodTwo into two classes that extend Thread and:

If you're not yet into threads, Google on "Sun tutorial threads". They have a nice gentle introduction. Lemme know if that helps or just raises more questions.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
This doesn't sound like a "beginner" question to me. Perhaps this thread should be moved to the intermediate or advanced message boards. Also, you may find some helpful information in the JDBC forum, if you are using that technology.
With that said, if you are just reading data from the database, you probably don't have to deal with concurrency issues (if that is even what you are asking about). Of course, if there are other programs that access the database and modify the data that you are reading, or if your program changes the data itself, you will need to understand the problems that can arise. I suggest you find a good text on databases. Again, the JDBC forum will probably be a better place to ask about such issues.

Java API Documentation
The Java Tutorial
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
The problem I think you are running up against has more to do with UI design than databases or running several programs. You seem to be indicating that you want a single user to be able to run this application and run several requests to the DB simultaneously. Assuming we're talking about the same user, I'm guessing the DB requests are pretty long running--otherwise, it wouldn't be a big deal to simply have the user wait for the reply before making the next query.
Ok so what you want to do is make sure you follow the client/server method for having your UI layer communicate with the back-end. You basically need to write an object that waits for a user request to come in from the user interface, and when it does, it creates a new thread. In the context of this new thread, you get what you need from the DB and notify a listener with the result.
So, you have your user interface layer, which contains all of the Swing components the user interacts with. When the user hits Go at some point, the code triggered by this button event: (1) packages up all the data the back end needs to satisfy the request, (2) creates an event listener that the back-end can pass the result to, and (3) passes the request data and the listener to a back-end object that services the request.
This back-end object: (1) uses the request data, (2) uses JDBC to get information from the DB, (3) packages up the response, and (4) passes it to the supplied listener.
The listener simply calls front-end code that displays the results.
The thing to keep in mind here is that every time the front end makes a call to the back end with a request, the back end spins off a new thread to satisfy the request so this call returns immediately. This leaves the user free to initiate another request. This brings up an interesting problem...when the back-end replies by sending its response to the listener, what should the listener do? Just suddenly take over the UI and display the result? What if the user was in the middle of making another request?
Much better, instead, to simply take the response and store it in some kind of response mailbox for the user. That same thread could update the UI to indicate to the user that there is a new response in the mailbox. That way, the user can continue doing what they're doing, but they'll see a little star pop up next to the widget that navigates over to the mailbox (for example), so they know something is sitting there waiting for them.
Make sure you understand concurrency and deadlock issues before you attempt this, because these are not the sort of things you want to be left debugging after the fact. But other than those two issues, you should be able to dive right in and start designing.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Perhaps a Sheriff can move this thread to a more appropriate forum. This seems beyond the usuall scope of this beginner's board.
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
I have to admit, I've pretty much given up on what constitutes a "beginner" versus "intermediate" versus "advanced" question. I more or less figured these groups would mirror the level of expertise addressed by the Java Programmer Cert (beginner), the Developer/Web Component/Business Component certs (intermediate), and the Enterprise Architect cert (advanced).
Threading and concurrency is a basic part of the Java's built in as a core piece and everyone who's going to do more than command line tools with Java needs to know at least the basics. No? Maybe I'm way off--can anyone point me to some more definitive answer about what a beginner question is?
I agree. Here's the link:
subject: Creating several instances of a program
It's not a secret anymore!