简单线程问题---请教各位下
如何在Test2中取得est1中得参数msg值并赋值给message打印结果应给为 hello
========================================================
public class Test2 {
public static void main(String[] args) {
        String message=null;
System.out.println("====="+message);
}
}
========================================================
public class Test1 implements Runnable{
private String msg=null;public void setValue(){
 Thread t=new Thread();
 t.start();
}
public void run() {
this.msg="hello";

}
========================================================
小弟对线程不是太熟悉,这题应该不难,却还摸了很久没搞定,希望路过得前辈指点下,最好能写出来。谢谢

解决方案 »

  1.   


    public class Test2 { 
    public static void main(String[] args) { 
            String message=null; 
            Test1 test1 = new Test1();
            test1.setValue();
            message = test1.getmsg;
            System.out.println("====="+message); 

    } class Test1 implements Runnable{ 

            private String msg=null; 
    String getmsg =null; //新增一友好变量接受赋值,也可以新增方法get()拿到 msg的值 public void setValue(){ 
    Thread t=new Thread(this); 
    t.start();
    try {
    t.sleep(10); //时间片问题
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    getmsg = msg;
    }  public void run() { 
    this.msg="hello"; 
    }  
      

  2.   

    public class Test1 implements Runnable{ 
    private String msg=null; public void setMsg(String msg){ 
    this.msg=msg;
     } 
    public String getMsg(){
    return msg;
    }
    public void run() { 
    this.msg="hello"; 
    }  

    public class Test2 { 
    public static void main(String[] args) { 
    Thread t=new Thread(new Test1()); 
    t.start();
            String message=t.getMsg(); //此时很有可能取到null,因为t.start()只是让线程可以运行了,具体有没有运行要取决于CPU,
    System.out.println("====="+message); 


      

  3.   

    我改了下,还是不行的啊。public class Test implements Runnable {
    public String mesg;
    public String getMesg() {
    return mesg;
    }public void setMesg(String mesg) {
    this.mesg = mesg;
    }public String initMesg(){
    Thread t=new Thread();
    t.run();
    while(getMesg()==null){
    System.out.println("=====null====");
    try {
    Thread.sleep(10);
    } catch (InterruptedException e) {e.printStackTrace();}
    }
    return this.mesg;
    }public void run() {
    this.setMesg("hello");
    }
    }
    =====================================================
    public class Test2 { public static void main(String[] args) {
    Test t1=new Test();
            System.out.println("========="+t1.initMesg());
    }
    }
    一直打印这句=====null===
      

  4.   

    public class Test2 { 
    public static void main(String[] args) { 
            String message=null; 
            Test1 test1 = new Test1();
            Thread t = new Thread(test1);
            t.start();
    try{
        t.sleep(10);
    }catch(InterruptedException e) {
                e.printStackTrace();
    }
    message = test1.getmsg();
            System.out.println(message); 

    } class Test1 implements Runnable{ 
    private String msg=null; 
    public void run() { 
    this.msg="hello"; 
    }

    public String getmsg(){
    return msg;
    }
    }  
    方法获取类私有变量,说实话,有点想不通楼主意图了。楼主上面的代码不想多说啥,看了眼睛刺激喔,呵呵。一,public只能有一个 二,public string mesg; 笔误吧,这里变量都public了,想拿到它的值还用得着费这多功夫麽,偶按private理解。 三,Thread t=new Thread(); t.run(); 改成Thread t=new Thread(this); t.start(); 四,赋值就一句,犯得着写个方法setMesg(String mesg)麽? 五,while(){}循环体中,打印语句搬到循环体外或者注释起来,毕竟这句作为测试用的。六,经过上面的改动后,程序虽然冗长但是可以编译而且正确运行。如下:
    class Test implements Runnable { 
    private String mesg; 
    public String getMesg() { 
    return mesg; 
    }  public void setMesg(String mesg) { 
    this.mesg = mesg; 
    }  public String initMesg(){ 
    Thread t=new Thread(this); 
    t.start(); 
    while(getMesg()==null){ 
    try { 
    Thread.sleep(10); 
    } catch (InterruptedException e) {e.printStackTrace();} 

    System.out.println(mesg); 
    return mesg;
    }  public void run() { 
    setMesg("hello"); 


    //===================================================== 
    public class Test2 { 
    public static void main(String[] args) { 
    Test t1=new Test(); 
            System.out.println("========="+t1.initMesg());