Srini
Srini

Reputation: 450

Thread Behavior:Java Beginner

I tried to write a program that uses threads, but couldn't understand the o/p. I have 2 thread: s and t. But I can't see that thread s is working.

Can anyone please explain to me the behavior? Thanks

My code:

public class Bground implements Runnable
{
    Thread t,s;

    Bground()
    {
        t = new Thread(this,"first thread");
        s = new Thread(this,"second thread");

        s.start();
        t.start();
    }

    public void run()
    {
        System.out.println("inside run" + t.getName());
        try
        {
            for (int i = 0; i < 5; i++)
            {
                System.out.println("In child thread" + i);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String argv[])
    {
        Bground b = new Bground();
        try
        {                
            for (int i = 0; i < 5; i++)
            {
                System.out.println("In main thread" + i);
                Thread.sleep(1);
            }
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}

O/P:

c:\Program Files\Java\jdk1.6.0_23\bin>java Bground

In main thread0
inside runfirst thread
inside runfirst thread
In child thread0
In child thread1
In child thread2
In main thread1
In child thread3
In child thread0
In child thread1
In child thread2
In child thread3
In child thread4
In main thread2
In child thread4
In main thread3
In main thread4

Upvotes: 0

Views: 222

Answers (2)

guness
guness

Reputation: 6656

You are printing name of the thread "t" twice. that is the problem. on the other hand the threads seemed working to me. you may want to use this code instead:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Bground implements Runnable {
int name;

Bground(int name) {
    this.name=name;
}

public void run() {
    System.out.println("inside run" + name);
    try {
        for (int i = 0; i < 5; i++) {
            System.out.println("In child thread" + i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String argv[]) {
    Bground b = new Bground(1);
    Bground b2 = new Bground(2);

    ExecutorService es = Executors.newFixedThreadPool(2);
    es.submit(b);
    es.submit(b2);
    synchronized (es) {
        es.notifyAll();
    }

    try {
        for (int i = 0; i < 5; i++) {
            System.out.println("In main thread" + i);
            Thread.sleep(1);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

What do you expect will print out with:

System.out.println("inside run " + t.getName());

You're not getting the current Thread's name here but rather you're always getting the t-Thread's name. To fix -- get the current thread and call getName() on it.

Upvotes: 2

Related Questions