thread{
String s=null;
run{
 s="ppoiot";
}gets(){
return s
}}调用的gets()的时候,s为null,怎么样可以保证调用s之前run执行完啊

解决方案 »

  1.   

    每次生成线程的时候就启动线程,run方法就自动被执行了啊
      

  2.   

    又的时候,
    thread t =new thread()
    t.start()
    t.gets()//这个时候返回null
    我希望run执行完毕
      

  3.   

    利用生产者与消费者同步解决。
    public synchronized String run(){}
    public synchronized String get(){}
      

  4.   

    你应该把s变量放到另一个类里面,然后把s 的getter和setter设为 synchronized,线程类是用来做事务管理的,这样就不存在这个矛盾了.
      

  5.   

    join synchronized 一起用,试哈子
      

  6.   

    Thread的join()方法可用于让当前线程阻塞,以等待特定线程的消亡.当调用join方法当前线程将等待其连接的线程不再处于活动状态
      

  7.   

    同意laughsmile(海边的星空) 。
      

  8.   

    同意laughsmile(海边的星空) 。
      

  9.   

    btb368()的这种方法应该也可以实现吧。
    利用生产者与消费者同步解决。
    public synchronized String run(){}
    public synchronized String get(){}
      

  10.   

    生产者与消费者同步是经典算法,当然可以解决,但很繁琐,而且消耗系统资源。
    我觉得java语言已经提供现成得东西了,利用join就行了。
      

  11.   

    楼主的逻辑真怪,既要放别的线程里面,又要等它执行完,这跟单线程有什么区别啊?在gets()里面再调一次run()好了。
      

  12.   

    Thread类中有 isAlive()方法
    public final boolean isAlive()
    Tests if this thread is alive. A thread is alive if it has been started and has not yet died. Returns:
    true if this thread is alive; false otherwise.
      

  13.   

    用isAlive方法判断线程是否终结
      

  14.   

    严重支持laughsmile(海边的星空) ( )
      

  15.   

    一个笨点的方法是设置一个状态参数
    以下代码仅供参考,少了很多东西.
    public int status = NOT_RUN;
    public void run(){
    status =RUNNING;
    ......
    status =RUN_OVER;
    }
    public String getStr(){
    while(status != RUN_OVER){
    Thread.sleep(50);
    }
    }