如何获取线程的ID?

时间:2014-03-16 08:04:23

标签: java

你能帮助我理解为什么这个项目总是显示我的标识9.我的意思是输出是这样的:

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

我估计Id会有所不同。即使我停止项目并再次运行,此ID也不会更改。

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

    }
}

6 个答案:

答案 0 :(得分:4)

您的代码使用单个线程:主线程。要启动一个帖子,你需要

  • 扩展线程
  • 覆盖其run()方法
  • 实例化您的Thread子类
  • 调用其start()方法

或者你需要

  • 创建一个Runnable
  • 将其传递给Thread构造函数
  • 调用Thread的start()方法

the tutorial清楚解释了这一点。

扩展线程,向扩展类添加方法(如readFile())并调用它,不会在另一个线程中调用此方法。

附注:请尊重Java命名约定。方法和变量以小写字母开头。

答案 1 :(得分:1)

据我所知,你在这里只创建了两个主题:

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

然后你实际上总是使用FirstThread

CurrentThread = FirstThread;

因此,只有一个线程工作,因此打印相同的ID。

BTW在java中有命名约定。所有变量和方法都以小写字母开头。您的大写非常混乱,使您的代码可读性降低。

答案 2 :(得分:1)

CurrentThread变量始终初始化为FirstThread。即使您已从Thread类扩展,也不会使用start()方法启动Threads。因此,没有任何线程在主线程之外运行。

答案 3 :(得分:0)

首先,CurrentThread始终为FirstThread

另一方面,你实际上并没有将其他线程用作线程。你只是调用一个方法,好像它们是任何其他对象一样。

覆盖public void run()类中的Thrd,其中的任何代码将在单独的线程中运行。要启动每个主题,请使用FirstThread.start();SecondThread.start();

答案 4 :(得分:0)

  

你能帮助我理解为什么这个项目总是向我显示Id 9。

生成线程ID的机制是未指定 ...但是如果查看the code,您将看到它是使用从零开始的简单计数器完成的。

因此,如果再次运行相同的应用程序,并且线程创建的顺序相同,则相应的线程将具有相同的ID。

但是,由于此行为未指定,依赖它是不明智的。

因此,在您的示例中,您获得了相同的线程ID,因为您的应用程序正在以相同的顺序创建它们,从一次运行到下一次运行。

  

我估计[假设?] Id是不同的。

javadocs中没有这种假设的基础。事实上它是不正确的。

答案 5 :(得分:0)

你的代码中有一个拼写错误,你每次都分配相同的帖子。

替换:

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

通过

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

但是,也许你应该考虑使用ThreadName来识别你的线程:JavaDoc for Thread

相关问题