Java Threading Issues

Discussion in 'Java' started by gibboda, Aug 30, 2006.

  1. gibboda

    gibboda Guest

    I am working on trying to use a thread to spawn a class I call to run
    in a seperate thread but it has failed. I have a class called
    ConnectDir for opening a vpn connection to a server. But what happens
    is the Cisco VPN connects but does to give screen control back. I have
    to open a seperate session of xterm or alt+F2 for another window. This
    does not work very well with automation. I have used vpncommand >
    vpn.log 2>&1 & in a java program which works if I am using simple
    methods. But I want to create individual classes to be able to read
    from text files certain information and be able to integrate with other
    classes I develop. Therefore this does not work very well either.
    Below is what I have attempted.

    The program structure I have is:

    public class myconnect
    {
    public static void main(String[] args)
    {
    ConnectDir dc = new ConnectDir();

    try
    {
    dc.open();
    }
    catch(expectj.ExpectJException error1)
    {
    System.out.println(error1)
    }

    }
    }

    What I have tried but it failed to put in seperate thread:

    public class myconnect2 extends Thread
    {
    public void run()
    {
    ConnectDir dc = new ConnectDir();

    try
    {
    dc.open();
    }
    catch(expectj.ExpectJException error1)
    {
    System.out.println(error1)
    }

    }

    public static void main(String[] args)
    {
    Thread mytest = new myconnect2();
    mytest.start();

    }
    }


    It may just be that I do not fully understand what threads are suppose
    to do. Thank you for your advice and wisdom.
    gibboda, Aug 30, 2006
    #1
    1. Advertising

  2. gibboda

    Ben Guest

    gibboda wrote:
    > I am working on trying to use a thread to spawn a class I call to run
    > in a seperate thread but it has failed. I have a class called
    > ConnectDir for opening a vpn connection to a server. But what happens
    > is the Cisco VPN connects but does to give screen control back. I have
    > to open a seperate session of xterm or alt+F2 for another window. This
    > does not work very well with automation. I have used vpncommand >
    > vpn.log 2>&1 & in a java program which works if I am using simple
    > methods. But I want to create individual classes to be able to read
    > from text files certain information and be able to integrate with other
    > classes I develop. Therefore this does not work very well either.
    > Below is what I have attempted.
    >
    > The program structure I have is:
    >
    > public class myconnect
    > {
    > public static void main(String[] args)
    > {
    > ConnectDir dc = new ConnectDir();
    >
    > try
    > {
    > dc.open();
    > }
    > catch(expectj.ExpectJException error1)
    > {
    > System.out.println(error1)
    > }
    >
    > }
    > }
    >
    > What I have tried but it failed to put in seperate thread:
    >
    > public class myconnect2 extends Thread
    > {
    > public void run()
    > {
    > ConnectDir dc = new ConnectDir();
    >
    > try
    > {
    > dc.open();
    > }
    > catch(expectj.ExpectJException error1)
    > {
    > System.out.println(error1)
    > }
    >
    > }
    >
    > public static void main(String[] args)
    > {
    > Thread mytest = new myconnect2();
    > mytest.start();
    >
    > }
    > }
    >
    >
    > It may just be that I do not fully understand what threads are suppose
    > to do. Thank you for your advice and wisdom.
    >

    You don't need to extend Thread to use threads...instead try this:

    public class myconnect2 implements Runnable
    {
    public void run() {
    ConnectDir dc = new ConnectDir();

    try
    {
    dc.open();
    }
    catch(expectj.ExpectJException error1)
    {
    System.out.println(error1)
    }

    }

    public static void main(String[] args)
    {
    myconnect2 threadTest = new myconnect2() /*or however you construct
    this class*/

    Thread mytest = new Thread(threadTest);
    mytest.start();

    }

    Good luck,
    Ben
    Ben, Aug 31, 2006
    #2
    1. Advertising

  3. gibboda

    Chris Smith Guest

    gibboda <> wrote:
    > It may just be that I do not fully understand what threads are suppose
    > to do. Thank you for your advice and wisdom.
    >


    Perhaps. You wrote:
    > What I have tried but it failed to put in seperate thread:
    >
    > public class myconnect2 extends Thread
    > {
    > public void run()
    > {

    [...]
    > }
    >
    > public static void main(String[] args)
    > {
    > Thread mytest = new myconnect2();
    > mytest.start();
    >
    > }
    > }


    That definitely does run the code in a new thread. However, the
    difference won't be observable here, because you let the main thread
    exit immediately without doing anything else. Threads all your program
    to do two things at once (or, at least, interleaved so tightly that they
    apparently happen at once); so if you want to get that benefit, your
    program will need to do two things. Add the code for that second thing
    onto the end of main, and (if your tasks run long enough) you'll be able
    to see that they both occur at about the same time.

    If all you wanted was to run your program in the background, then you
    don't need to change your code to do that. Just tell the operating
    system that you want that. For example, on UNIX (at least in common
    shells), it's done by adding an ampersand (&) to the end of the command.

    --
    Chris Smith
    Chris Smith, Aug 31, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wh
    Replies:
    4
    Views:
    450
    Kevin Spencer
    Nov 17, 2003
  2. Peter Row

    multi-threading issues, ideas to solve?

    Peter Row, Dec 12, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    465
    Peter Row
    Dec 15, 2003
  3. Replies:
    9
    Views:
    1,011
    Mark Space
    Dec 29, 2007
  4. Steven Woody
    Replies:
    0
    Views:
    394
    Steven Woody
    Jan 9, 2009
  5. Steven Woody
    Replies:
    0
    Views:
    434
    Steven Woody
    Jan 9, 2009
Loading...

Share This Page