Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to use threads to open System.commands  RSS feed

 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
it is my first time to think of threads

i am using the java program to carry out a system command that takes a LONG time and during it i can not use my program because it is like frozen, so i thought of using threads but i dont know how to do it
anyhelp..
if possible please send links on tutorials of System commands using threads and tutorials on using threads. thanks :roll:
thanks all
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the Sun Tutorial which I see has been updated for Java 5.

Here's an outline of what you could do ... On the main thread of your program start a new thread to run a command. In that thread, exec the command and create two more new stream reader threads to read the stdout and error out from the process. Look into join() so the command thread can know when the two stream readers are done. Later we can talk about many ways the command thread can let the main thread know the whole thing is done.

Work through the tutorial, see if you can put some of that outline into code, and check in often to let us know how it goes!
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay this is what i have


class mainFrame extends JFrame implaments Runnable {
...//my code

public void Run(){
//run the system command
}
}
is that right ..but still how will i run the command in my main ??
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read and worked through the tutorial that Stan pointed you to? If you actually look at, understand, compile and run the examples in that tutorial, you should be able to answer your own question.
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maha Aburahma:
okay this is what i have


class mainFrame extends JFrame implaments Runnable {
...//my code

public void Run(){
//run the system command
}
}
is that right ..but still how will i run the command in my main ??




runtime exec

Guy
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!