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/set_thread_name/set_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/set_thread_name/set_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/set_thread_name/set_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/set_thread_name/set_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/set_thread_name/set_thread_name.html on line 17
スレッド名の設定
スレッド名はスレッドクラスをインスタンス化する時に自動的に生成されます。
しかし、プログラムでスレッド名を設定することもできます。
スレッドに対してスレッド名を設定するには setName() メソッドを使用します。
以下はスレッドクラスです。

public class MyThread extends Thread{
	public void run(){
		for (int i=0;i<10;i++){
			System.out.println(Thread.currentThread().getName());
		}
	}
}
以下はスレッド名設定していないスレッドと、スレッド名を setName() メソッドで設定している例です。

public class TEST {
	public static void main(String[] args){
		MyThread thread1 = new MyThread();
		MyThread thread2 = new MyThread();
		thread1.start();
		
		thread2.setName("スレッド2");
		thread2.start();
	}
}
上記、Javaアプリケーションを実行すると結果は以下のようになります。

Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
スレッド2
Thread-0
スレッド2
スレッド2
スレッド2
スレッド2
スレッド2
スレッド2
スレッド2
スレッド2
スレッド2

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/set_thread_name/set_thread_name.html on line 103

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/set_thread_name/set_thread_name.html on line 103

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/set_thread_name/set_thread_name.html on line 112

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/set_thread_name/set_thread_name.html on line 112