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_current_thread_name/get_current_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_current_thread_name/get_current_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_current_thread_name/get_current_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_current_thread_name/get_current_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_current_thread_name/get_current_thread_name.html on line 17
現在実行中のスレッド名の取得
javaでは、スレッドには名前があります。
このスレッド名を取得するには以下のように getName()メソッドを使用します。
これに対し、現在実行中のスレッド名を取得するには Thread.currentThread().getName() を使用します。
以下、例です。

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

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

Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
スレッド名はそれぞれ Thread-0 Thread-1 であることがわかります。
また、別々のスレッドでスレッド名の出力を行っていることがわかります。

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_current_thread_name/get_current_thread_name.html on line 104

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_current_thread_name/get_current_thread_name.html on line 104

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_current_thread_name/get_current_thread_name.html on line 113

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_current_thread_name/get_current_thread_name.html on line 113