public class RiddleUser
{
public static void main ( String args[] )
{
Riddle riddle1 = new Riddle ("What is black and white and red all over?", "An embarrassed zebra.")
Riddle riddle2 = new Riddle ("What is black and white and read all over?", "A newspaper.")
System.out.println ("Here are two riddles:")
System.out.println (riddle1.getQuestion ())
System.out.println (riddle2.getQuestion ())
System.out.println ("The answer to the first question is:")
System.out.println (riddle1.getAnswer ())
System.out.println ("The answer to the second question is:")
System.out.println (riddle2.getAnswer ())
}
}
class Riddle
{
String question
String answer
public Riddle ( String q, String a )
{
question = q
answer = a
}
public String getQuestion ()
{
return question
}
public String getAnswer ()
{
return answer
}
}
可以在new 线程的时候指定线程名称,如果后期还想修改,可以调用Thread的setName方法:
public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName())
try {
Thread.sleep(1000)
// 主线程中进行了修改
System.out.println(Thread.currentThread().getName())
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}, "这里设置线程名称")
thread.start()
// 500毫秒后修改线程名称
Thread.sleep(500)
thread.setName("新的线程名称!")
}
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)