DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Thread: Threads

  1. #1
    Join Date
    Oct 2004
    Posts
    151

    Question Threads

    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

  2. #2
    Join Date
    Dec 2004
    Location
    Norway
    Posts
    14
    Like this:

    Code:
    public class test implements Runnable {
    
    public test () {
      Thread t=new Thread(this);
      t.start();  // now the java vm will call this class' run() method
    }
    public void run () {
      System.out.println("Thread started");
      test1();
      test2();
      test3();
      System.out.println("Thread exits");
    }
    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();
    }
    }
    And....

    There is no programming language on earth that allows for calling
    specific command lines (statements I presume)
    Java Wreck

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links