CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1

    Multithreading Queries

    Hi,
    I am learning java multithreading. I am not able to understand below queries which are in my mind.

    1. Is it possible to understand number of threads in a running program? Further, can we also understand state of a thread?

    e.g. In the below program, 4 threads are created. So, can we understand the status of each thread i.e. runnable,running,blocked, terminated etc.


    <code>
    class YourThread implements Runnable {
    String tName;
    Thread t;
    YourThread (String threadName) {
    tName = threadName;
    t = new Thread (this, tName);
    t.start();
    }
    public void run() {
    try {
    System.out.println("Your Thread run called " + tName );
    Thread.sleep(2000);
    } catch (InterruptedException e ) {
    System.out.println("Exception: Your Thread "
    + tName + " interrupted");
    }
    System.out.println("Terminating your thread: " + tName );
    }
    }

    class ThreadDemo {
    public static void main (String args []) {
    new YourThread ("10");
    new YourThread ("20");
    new YourThread ("30");
    new YourThread ("40");
    System.out.println("Terminating thread: main thread.");
    }
    }
    </code>

    Q2 How multithreading can be applied following program
    I want to execute print1 and print2 methods parallely?

    class test{
    void print1()
    {
    System.out.println("Hi.");
    System.out.println("Hisfsd.");
    System.out.println("Hifsdsdf.");
    System.out.println("fdssdfHi.");
    //such 200 lines
    }


    void print2()
    {
    System.out.println("welcome");
    System.out.println("wsdfsdfelcome");
    System.out.println("welcsdffdsome");
    System.out.println("welcofsdsdfme");
    //such 100 lines
    }
    }
    class ThreadDemo {
    public static void main (String args [])
    {
    test t1=new test();
    t1.print1();
    t1. print2();
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Multithreading Queries

    1. Is it possible to understand number of threads in a running program? Further, can we also understand state of a thread?
    By 'understand' do you mean 'determine' and do you mean from within the program or using tools such as profiler?

    Q2 How multithreading can be applied following program
    I want to execute print1 and print2 methods parallely?
    You create 2 threads with each thread calling one of the methods. Depending on the speed of your computer you may need to add more print statements (or wrap a loop around the existing ones) to see the output from the 2 methods being interleaved.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3

    Re: Multithreading Queries

    Quote Originally Posted by keang View Post
    By 'understand' do you mean 'determine' and do you mean from within the program or using tools such as profiler?

    You create 2 threads with each thread calling one of the methods. Depending on the speed of your computer you may need to add more print statements (or wrap a loop around the existing ones) to see the output from the 2 methods being interleaved.

    Thanks, Keang for prompt response.

    1. Yes, I mean "Determine" and within the program without using profiler.
    As I am beginner, request you to modify the code snippet that I have posted
    2. Yes, I will add a loop in both these methods. It will be great if you can modify the code snippet provided for second query.

    Thanks in advance.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Multithreading Queries

    As I am beginner, request you to modify the code snippet that I have posted
    No, you will learn more by doing it yourself.

    Read the java.lang.Thread and java.lang.ThreadGroup API docs.

    Every Thread belongs to a ThreadGroup and all ThreadGroups are connected in a tree with each having a single parent ThreadGroup. So you ask the current Thread for its ThreadGroup and then walk the tree of ThreadGroups until you find the root ThreadGroup, ie the ThreadGroup without a parent. You can then use the ThreadGroup's enumerate(..) method to find all the Threads.

    2. Yes, I will add a loop in both these methods. It will be great if you can modify the code snippet provided for second query.
    Again, it's better if you do it yourself.

    If you get stuck post your code, in code tags, and state exactly what your problem is.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured