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();
}
}