当前位置: 代码迷 >> J2SE >> Runnable 回答后快速给分!该怎么处理
  详细解决方案

Runnable 回答后快速给分!该怎么处理

热度:238   发布时间:2016-04-24 15:20:43.0
Runnable 回答后快速给分!
public   class   A   implements   Runnable   {
Vector   vec   =   new   Vector();

private   Thread   netthread   =   new   Thread(this);

public   void   run()   {
System.out.println( "vec.size ");
vec   =   new   Vector();
vec.addElement( "1 ");
}

public   Vector   communicate()   {
//   先执行这个
netthread.start();
//   再执行这个
System.out.println( "vec.size== "   +   vec.size());
return   vec;
}

public   static   void   main(String[]   args)   {
A   a   =   new   A();
a.communicate();
}
}

result:
vec.size==0
vec.size

如何让程序,
先netthread.start();
后System.out.println( "vec.size== "   +   vec.size());

------解决方案--------------------
import java.util.*;

public class A implements Runnable {
Vector vec = new Vector();

private Thread netthread = new Thread(this);

public void run() {
System.out.println( "vec.size ");
vec = new Vector();
vec.addElement( "1 ");
}

public Vector communicate() {
// 先执行这个
netthread.setPriority(10);
netthread.start();
// 再执行这个
System.out.println( "vec.size== " + vec.size());
return vec;
}

public static void main(String[] args) {
A a = new A();
a.communicate();
}
}

result:
vec.size
vec.size==1

------解决方案--------------------
*/
public class A implements Runnable {
Vector vec = new Vector();

private Thread netthread = new Thread( this );

public void run() {
System.out.println( "vec.size " );
vec = new Vector();
vec.addElement( "1 " );
}

public Vector communicate() {
// 先执行这个
netthread.start();
try{
Thread.sleep(10);
} catch(Exception ex) {
ex.printStackTrace();
}
// 再执行这个
System.out.println( "vec.size== " + vec.size() );
return vec;
}

public static void main( String[] args ) {
A a = new A();
a.communicate();
}
}
------解决方案--------------------
import java.util.*;

public class ATest implements Runnable {
Vector vec = new Vector();

private Thread netthread = new Thread(this);

public void run() {
System.out.println( "vec.size ");
vec = new Vector();
vec.addElement( "1 ");
}

public Vector communicate() {
// ?????
netthread.setPriority(10);
netthread.start();
// ?????
try {
netthread.join();
}
catch (Exception e){
e.printStackTrace();
}
System.out.println( "vec.size== " + vec.size());
return vec;
}

public static void main(String[] args) {
ATest a = new ATest();
a.communicate();
}
}

  相关解决方案