Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: adding get mothods to a class extending thread

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default adding get mothods to a class extending thread

    here is what i am trying to do...

    i have a class to run system commands which extending the Thread class.
    inside this class, a method called getOutput() that should return a string with the result.

    the commands run successfully with no problems. all i want to do is to get the output stream in a string...


    Thread t1 = new Command();
    t1.getOutput(); // --> Not working of course because it is not a method of the thread class

    Q: is that possible to do in another way... giving that the class extends Thread

    Thanks


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: adding get mothods to a class extending thread

    Hello,

    How are you executing these system commands?

    How are you actually feeding your results into the output variable?

    // Json

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: adding get mothods to a class extending thread

    Hello Json, sorry i was away for the last couple of days...

    anyways, i have the following class, used to get the ls (unix command) of a file

    public class TestGetDate {
    String result;
    String file;
    Process p;
    BufferedReader stdInput;
    String s;
    String awk;
    String[] command;
    public TestGetDate(String file) throws IOException{
    super();
    this.file = file;

    p = Runtime.getRuntime().exec("ls -l " + file);
    stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    result = "";
    while (!procDone(p)) {
    while((s=stdInput.readLine()) !=null){


    result = result+s;
    }
    setOutput(result);

    }
    System.out.println(result);
    stdInput.close();

    }

    private boolean procDone(Process p) {
    try {
    int v = p.exitValue();
    return true;
    }
    catch(IllegalThreadStateException e) {
    return false;
    }
    }
    public void setOutput(String result){
    this.result = result;
    }
    public String getOutput(){
    return result;
    }

    }

    i want this class to run as a thread but when i do, i cannot run the getOutput() method.

    any ideas?

    Thanks anyway

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: adding get mothods to a class extending thread

    I think it's because of how Java handles multi-threading... They have a very strict lock mechanism that prevents almost any chance of dual access, deadlocking, or an such other strange things associate with multi-threading. You can read Sun Java's Tutorial on Concurrency, but it's fairly hard to follow unless you have a fairly good knowledge of computer science and/or java.

  5. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: adding get mothods to a class extending thread

    Make you class extend Thread. then instead of calling it using Thread t1 = new Command();

    You would do something like this, if command is the class you want to run as a thread, such as your TestGetDate class.

    Command c1 = new Command();
    c1.start();

    If I understood your problem correctly, then you can call c1.getOutput();

    Regards,
    Chri

  6. The Following User Says Thank You to Freaky Chris For This Useful Post:

    aliaa2a (August 3rd, 2009)

  7. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: adding get mothods to a class extending thread

    Thanks Freaky Chris, it works like a charm.


  8. #7
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: adding get mothods to a class extending thread

    Excellent Glad I could help

    Chris
    Last edited by Freaky Chris; August 3rd, 2009 at 09:10 AM.

Similar Threads

  1. Java program to do Matrix operation
    By saladfingers73 in forum Collections and Generics
    Replies: 5
    Last Post: March 7th, 2012, 09:17 AM
  2. UDP server sends thread application
    By Koren3 in forum Threads
    Replies: 2
    Last Post: April 20th, 2009, 11:46 AM
  3. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM
  4. adding rows to coloumns. Netbeans
    By haygaurav in forum Java IDEs
    Replies: 1
    Last Post: April 1st, 2009, 03:16 PM
  5. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM