// mainthread.java class mainthread { public static void main(String args[]) { Thread thread = Thread.currentThread(); System.out.println( "Main thread is named " + thread.getName()); } }実行すると,
% javac mainthread.java % java mainthread Main thread is named mainという具合にmainスレッドはmainという名前を 持っている. スレッドは生成した後から名前を付ける ことができる.
// setname.java class setname { public static void main(String args[]) { Thread thread = Thread.currentThread(); System.out.println( "Main thread's original name is " + thread.getName()); thread.setName("The Main Thread"); System.out.println( "Main thread's name is now " + thread.getName()); } }実行すると,
% javac setname.java % java setname Main thread's original name is main Main thread's name is now The Main Thread