Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/users/2/moriya-h5/web/confrage/java/thread/get_thread_name/get_thread_name.html on line 2

Warning: include_once(http://www.confrage.com/bin/header.php): failed to open stream: no suitable wrapper could be found in /home/users/2/moriya-h5/web/confrage/java/thread/get_thread_name/get_thread_name.html on line 2

Warning: include_once(): Failed opening 'http://www.confrage.com/bin/header.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/thread/get_thread_name/get_thread_name.html on line 2
スレッド名の取得 | Java | 株式会社CONFRAGE
Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/thread/get_thread_name/get_thread_name.html on line 17

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/thread/get_thread_name/get_thread_name.html on line 17
スレッド名の取得
javaでは、スレッドには名前があります。
このスレッド名を取得するには以下のように getName()メソッドを使用します。
以下はスレッドクラスです。

public class MyThread extends Thread{
	public void run(){
		for (int i=0;i<10;i++){
			System.out.println("aa");
		}
	}
}
そして、このMyThreadクラスをインスタンス化して start() メソッドを呼び出します。
以下を見てください。

public class TEST {
	public static void main(String[] args){
		MyThread thread1 = new MyThread();
		MyThread thread2 = new MyThread();
		thread1.start();
		thread2.start();
		System.out.println(thread1.getName());
		System.out.println(thread2.getName());
	}
}
この実行結果は以下のようになります。

aa
aa
aa
aa
aa
aa
aa
aa
aa
aa
Thread-0
Thread-1
aa
aa
aa
aa
aa
aa
aa
aa
aa
aa

スレッド名はそれぞれ Thread-0 Thread-1 であることがわかります。
スレッド名はプログラム側で設定しない場合は、javaが自動的に設定します。

Back to top

Information

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/thread/get_thread_name/get_thread_name.html on line 108

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/thread/get_thread_name/get_thread_name.html on line 108

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/thread/get_thread_name/get_thread_name.html on line 117

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/thread/get_thread_name/get_thread_name.html on line 117