0
votes

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

2
Just to let you know: Every time you format your code like that, God Kills a kitten.Richard J. Ross III
Consider letting each thread have a Thread.sleep(1000). The argument is in milliseconds.Thorbjørn Ravn Andersen

2 Answers

2
votes

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.

2
votes

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();
    }
}

}