Threads

Discussion in 'Java' started by freesoft_2000, Feb 7, 2005.

  1. Hi everyone,

    I have a question about threads. Consider the below
    program

    Code:
    
    public class test
    {
    
    public void test1()
    {
    System.out.println("Test 1");
    }
    
    public void test2()
    {
    System.out.println("Test 2");
    }
    
    public void test3()
    {
    System.out.println("Test 3");
    }
    
    public static void main(String args[])
    {
    test a = new Jtest();
    a.test1();
    a.test2();
    a.test2();
    }
    }
    
    
    As you can see above i have three methods in the above class. My question
    is how do i call each of these methods in a separate thread either in the
    class main or in the class itself

    Consider the below method

    Code:
    
    public void test()
    {
    System.out.println("Test 1");   //command line 1
    System.out.println("Test 2");   //command line 2
    }
    
    
    As you can see from the above method i have two command lines in the above
    method. My question is how do i call each command line in a separate
    thread.

    Basically i need to know how to call a specific method or a specific
    command line in a separate thread excluding the main thread

    I hope someone can help me with both these questions

    Any help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West
    freesoft_2000, Feb 7, 2005
    #1
    1. Advertising

  2. "freesoft_2000" <> schrieb im Newsbeitrag
    news:...
    > Hi everyone,
    >
    > I have a question about threads. Consider the below
    > program
    >
    >
    Code:
    >
    > public class test
    > {
    >
    > public void test1()
    > {
    >    System.out.println("Test 1");
    > }
    >
    > public void test2()
    > {
    >    System.out.println("Test 2");
    > }
    >
    > public void test3()
    > {
    >   System.out.println("Test 3");
    > }
    >
    > public static void main(String args[])
    > {
    >   test a = new Jtest();[/color]
    
    I think you mean "new test()".
    [color=blue]
    >   a.test1();
    >   a.test2();
    >   a.test2();
    > }
    > }
    >
    > 
    >
    > As you can see above i have three methods in the above class. My question
    > is how do i call each of these methods in a separate thread either in the
    > class main or in the class itself
    >
    > Consider the below method
    >
    >
    Code:
    >
    > public void test()
    > {
    >    System.out.println("Test 1");   //command line 1
    >    System.out.println("Test 2");   //command line 2
    > }
    >
    > 
    >
    > As you can see from the above method i have two command lines in the above
    > method. My question is how do i call each command line in a separate
    > thread.
    >
    > Basically i need to know how to call a specific method or a specific
    > command line in a separate thread excluding the main thread


    Try this:
    ---------
    Runnable runner = new Runnable() {
    public void run() {
    // put your commands here
    }
    };
    Thread t = new Thread(runner);
    t.start(); // executes the runners run()-method in a separate thread.
    Returns immediately
    ---------

    Regards
    Sebastian
    Sebastian Scheid, Feb 7, 2005
    #2
    1. Advertising

  3. freesoft_2000

    Guest

    don't forget to join() the created thread at the end of the main
    method. Otherwise you might not see anything...
    , Feb 11, 2005
    #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. Codemonkey
    Replies:
    1
    Views:
    403
    Ying-Shen Yu[MSFT]
    Oct 30, 2003
  2. yoda
    Replies:
    2
    Views:
    418
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  3. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    383
    William Ahern
    Aug 27, 2004
  4. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,401
    Arne Vajhøj
    Apr 9, 2008
  5. Une bévue
    Replies:
    0
    Views:
    131
    Une bévue
    Jun 14, 2006
Loading...

Share This Page