多線程中方法的使用的代碼
/*
?* 多線程中的其它方法
?*/
public class InfoTest {
?? ?public static void main(String[] args) throws InterruptedException {
?? ??? ?System.out.println(Thread.currentThread().isAlive());?? //輸出主線程的存活狀態(tài)
?? ??? ?//設(shè)置名稱:真實(shí)角色+代理角色
?? ??? ?MyInfo info=new MyInfo("嗯哈");
?? ??? ?Thread t=new Thread(info);
?? ??? ?//設(shè)置一定要在啟動(dòng)前
?? ??? ?t.setName("娘子");
?? ??? ?t.start();
?? ??? ?Thread.sleep(1000);? //讓主線程休息1s
?? ??? ?System.out.println(t.isAlive());? //判斷t線程的存活狀態(tài)
?? ??? ?
?? ?}
?? ?
}
//真實(shí)角色
class MyInfo implements Runnable{
?? ?private String name;
?? ?
?? ?public MyInfo(String name) {
?? ??? ?
?? ??? ?this.name = name;
?? ?}
?? ?@Override
?? ?public void run() {
?? ??? ?System.out.println(Thread.currentThread().getName()+"-->"+name);
?? ??? ?
?? ?}
?? ?
}
標(biāo)簽: