java线程数据获问题 我建了一个类,里面开了3个线程,一个main函数,我想在3个线程里面收集数据然后在main里面使用,所以我定义了一个static 的vector vData 数组,而且是public的,main外面定义,我在main里面是这样获取Vector v=类实例.vData 但是我打印数据的时候发现v是空的请问为什么???我要怎样获取线程里面的数组 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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); } 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 里面没有数据 就像上边一样,注意线程的同步问题,有可能打印的线程在main线程之前运行,只要没有进行同步就有可能导致线程的一部访问,可以参考一下楼上的这段代码。 java解析文本数据,内容类似表格 实体bean中的Date类型属性的小问题 timer的停止问题 有没有人研究过jhotdraw? 请问请问jTextField.getText()的数据类型是什么?以及怎样转换为int? frame中图片和几个按钮(或者文本区),如何让这些全部都显示出来 java连接SQL SERVER 2000出错! 看哪位高手能解决这个问题:servlet中的socket问题。 取得文件行数错误,百分请教! 请问java画图中的resize问题? 纯感谢Java大侠帖~~~~~~~大侠顺便看看我理解的多态对不对~ 下列java对象是何意思?
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);
}
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 里面没有数据