亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請教關于thread構造方法的一個小問題

請教關于thread構造方法的一個小問題

翻閱古今 2018-12-07 09:22:23
package test; public class Test0919 { public static void main(String args[]) { A a=new A("t1"); } } class A implements Runnable { Thread t=null; String tname=null; public A(String tname) { this.tname=tname; this.t=new Thread(this, tname); this.t.start(); } @Override public void run() { try { for(int i=0;i<20;i++) { System.out.println(this.t.getName()); this.t.sleep(300); } } catch (InterruptedException e) { e.printStackTrace(); } } } 構造一個線程 為什么將A類中構造方法中線程的構造方法改為thread(tname)控制臺就不打印線程名稱啦 package test; public class Test0919 { public static void main(String args[]) { A a=new A("t1"); } } class A implements Runnable { Thread t=null; String tname=null; public A(String tname) { this.tname=tname; this.t=new Thread(tname); this.t.start(); } @Override public void run() { try { for(int i=0;i<20;i++) { System.out.println(this.t.getName()); this.t.sleep(300); } } catch (InterruptedException e) { e.printStackTrace(); } } } View Code
查看完整描述

2 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

我覺得之所以不打印是因為你執行start方法的線程并不是A,因為你在構造里面是新new出來的一個Thread,它start,并不代表A里面的run方法會執行。我也沒測試過,如果樓主有正確答案不妨告知下。

查看完整回答
反對 回復 2018-12-16
?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

構造函數吧Thread 的參數穿進去,public A(String tname,Thread t)

查看完整回答
反對 回復 2018-12-16
  • 2 回答
  • 0 關注
  • 577 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號