import java.util.Vector;
public class TestRunnable implements Runnable{
public String name;
static Vector v=new Vector();
static Vector v2;
public TestRunnable(String n)
{
this.name=n;
}public TestRunnable()
{
}
public void run() {
// TODO Auto-generated method stub
write(name);}
public synchronized void write(String name){
int i=0;
while(true){
System.out.println(this.name+" --");
try {
v.add(i);
i++;
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[]){
TestRunnable t2 = new TestRunnable();
v2=v;
for(int i=0;i<v2.size();i++)
System.out.println(v2.get(i));
}public void goToC(){
TestRunnable t1=new TestRunnable("t1");
Thread th1=new Thread(t1,"t1");
th1.start();
TestRunnable t2=new TestRunnable("t2");
Thread th2=new Thread(t2,"t2");
th2.start();
TestRunnable t3=new TestRunnable("t3");
Thread th3=new Thread(t3,"t3");
th3.start();
}
}
为什么v2里面获取不了几个线程的数据???怎样才能获取到
public class TestRunnable implements Runnable{
public String name;
static Vector v=new Vector();
static Vector v2;
public TestRunnable(String n)
{
this.name=n;
}public TestRunnable()
{
}
public void run() {
// TODO Auto-generated method stub
write(name);}
public synchronized void write(String name){
int i=0;
while(true){
System.out.println(this.name+" --");
try {
v.add(i);
i++;
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[]){
TestRunnable t2 = new TestRunnable();
v2=v;
for(int i=0;i<v2.size();i++)
System.out.println(v2.get(i));
}public void goToC(){
TestRunnable t1=new TestRunnable("t1");
Thread th1=new Thread(t1,"t1");
th1.start();
TestRunnable t2=new TestRunnable("t2");
Thread th2=new Thread(t2,"t2");
th2.start();
TestRunnable t3=new TestRunnable("t3");
Thread th3=new Thread(t3,"t3");
th3.start();
}
}
为什么v2里面获取不了几个线程的数据???怎样才能获取到
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货