GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Java execution in background Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java execution in background" Watch "Java execution in background" New topic
Author

Java execution in background

Happy S Singh
Ranch Hand

Joined: Nov 14, 2011
Posts: 52
i have developed an app which executes sql jobs. When I click on execute button the my application goes into running state and it halts untill the query is executed.

I want my app should not halt and the user should be able to enter other query and those query execution should run in background.

My question is how to run the execution of queries in background? means when the execute button is clicked ,the remaining execution should run behind the screen.

My app is developed using struts1.3 framework.I have written the main functionality in execute() of Action Class.

I have created thread but It does not run in background.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Happy S Singh wrote:iI have created thread but It does not run in background.

You'll have to explain what you mean by "background".

If you mean creating a Thread that runs independently, then...well, that's what they do...so if yours isn't, you're doing something wrong.

If you mean running a Thread with a different priority, I'd have two suggestions:
1. Don't do it.
2. If you do do it, don't rely on it.

Hint: show us your code (only the relevant bits please); and when you do, UseCodeTags (←click).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Happy S Singh
Ranch Hand

Joined: Nov 14, 2011
Posts: 52
I mean when the thread starts,I should be able to work with my application but It halts until the thread task is completed.
Following is my thread


And on the click of button following snippet is invoked which in turn starts the thread



Now I am planning to use RMI which in turn will start the thread.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Happy S Singh wrote:I mean when the thread starts,I should be able to work with my application but It halts until the thread task is completed.

Well I'm afraid I've never used ExecutorService, but your acQueryExecutor (and BTW, it should be AcQueryExecutor) looks pretty much as I would expect.

Have you tried testing it by just doing:
acQueryExecutor a = new acQueryExecutor(id,usrname,pswd,driver,url);
new Thread(a).start();

?
I would expect a program to carry on quite happily after that.

You could also test it with a Runnable that simply waits for 1 minute and then prints out something to the console, or opens a dialog box.
If you can continue doing other things while it's running, then your problem obviously lies elsewhere (eg, possibly in the way you're using ExecutorService).

Now I am planning to use RMI which in turn will start the thread.

Fine, but don't start loading on other layers until you've got this one working.

Winston
Happy S Singh
Ranch Hand

Joined: Nov 14, 2011
Posts: 52
Using this


The task are running in background without bothering the front end or other tasks.
@ Winston Gutkowski thank you
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Happy S Singh wrote:@ Winston Gutkowski thank you

You're welcome.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java execution in background