Trts
Trts

Reputation: 1069

How to get id of a thread?

Why does this project always show me Id 9? I mean, the output is like this:

FirstThread.txt
Id 9
<Some string read from the file>
...
...
SecondThread.txt
Id 9
<Some string read from the file>

I would estimate Id's to be different. This Id doesn't change even if I stop the project and run it again.

package parallelprogramming;

import java.lang.Thread;
import java.io.*;

public class Thrd extends Thread {

    public boolean readFile(String File)/* throws FileNotFoundException */ {
        try {
            FileReader fr = new FileReader(File);
            BufferedReader br = new BufferedReader(fr);
            String s;
            try {
                while ((s = br.readLine()) != null) {
                    System.out.println(File);
                    System.out.println("Id " + this.getId());
                    System.out.println(s);
                }
                fr.close();
                return true;
            } catch (IOException IOE) {
                System.out.println("IOException caught!");
                return false;
            }
        } catch (FileNotFoundException FNFD) {
            System.out.println("File not found!");
            return false;
        }

    }
}



package parallelprogramming;

import java.awt.event.*;

import java.io.FileNotFoundException;

public class ParallelProgramming {

    public static void main(String[] args) {

        Thrd FirstThread = new Thrd();
        Thrd SecondThread = new Thrd();

        Thrd CurrentThread = null;
        String File = null;
        for (int i = 0; i < 1000000; i++) {
            if (i % 2 == 0) {
                File = "FirstThread.txt";
                CurrentThread = FirstThread;
            } else {
                File = "SecondThread.txt";
                CurrentThread = FirstThread;
            }
            while (!CurrentThread.isInterrupted()) {

                if (CurrentThread.readFile(File)) {

                    break;
                };
            }
        }

    }
}

Upvotes: 1

Views: 139

Answers (6)

Stephen C
Stephen C

Reputation: 718788

Why does this project always show me Id 9?

The mechanism by which thread ids are generated is not specified ... but if you look at the code you will see that it is done using a simple counter that starts from zero.

So if you run the same application again, and the order of thread creation is the same, corresponding threads will have the same ids.

However, since this behaviour is unspecified, it would be unwise to rely on it.

So, in your example, you are getting the same thread ids because your application is creating them in the same order, from one run to the next.

I would estimate [assume?] Id's to be different.

There is no basis for this assumption in the javadocs. And in fact it is incorrect.

Upvotes: 0

Lasitha Petthawadu
Lasitha Petthawadu

Reputation: 763

The CurrentThread variable is always initialized as FirstThread. Also even though you have extended from the Thread class the Threads are not started using the start() method. Therefore there isn't any threads running other than the main thread.

Upvotes: 1

Roberto
Roberto

Reputation: 9080

you have a typo in your code, your are assigning the same thread every time.

Replace:

if (i % 2 == 0) {
            File = "FirstThread.txt";
            CurrentThread = FirstThread;
        } else {
            File = "SecondThread.txt";
            CurrentThread = FirstThread;
        }

By

if (i % 2 == 0) {
            File = "FirstThread.txt";
            CurrentThread = FirstThread;
        } else {
            File = "SecondThread.txt";
            CurrentThread = SecondThread;
        }

However, maybe you should consider to use ThreadName to identify your threads: JavaDoc for Thread

Upvotes: 0

AlexR
AlexR

Reputation: 115328

As far as I can see you create only 2 threads here:

    Thrd FirstThread = new Thrd();
    Thrd SecondThread = new Thrd();

Then you actually always use FirstThread:

CurrentThread = FirstThread;

So, only one thread is working and therefore the same ID is printed.

BTW there are naming conventions in java. All variables and methods start with small letter. Your capitalization is very confusing and makes your code less readable.

Upvotes: 1

For one thing, CurrentThread is always FirstThread.

For another thing, you're not actually using those other threads as threads. You're just calling a method, as if they were any other objects.

Override public void run() in your Thrd class, and any code inside that will run in a separate thread. To start each thread, use FirstThread.start(); and SecondThread.start();.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

Your code uses a single thread: the main thread. To start a thread, you need to

  • extends Thread
  • override its run() method
  • instantiate your Thread subclass
  • call its start() method

Or you need to

  • create a Runnable
  • pass it to a Thread constructor
  • call the start() method of the Thread

This is clearly explained in the tutorial.

Extending Thread, adding a method (like readFile()) to the extending class, and calling it, will not invoke this method in another thread.

Side note: please respect the Java naming conventions. Methods and variables start with a lowercase letter.

Upvotes: 4

Related Questions