package chn1; //类1
import java.util.Random;
public class ReadRandom extends Thread{
private Random ran = new Random();
private double []random; //定义数组变量
public void run(){
random = new double[1000];
for(int i = 0;i < random.length;i++){ //初始化变量
random[i] = ran.nextDouble();
// System.out.println(random[i]);
}
}
public String getRandom(){ //get()方法确保另一个类能够使用该变量
run();
String str = "";
for (int i = 0; i < random.length; i++) {
str += random[i] + "\n";
}
return str;
}
}
package chn1; //类2
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class WriteRandom extends Thread{
ReadRandom rr = new ReadRandom(); //类1创建实例
File file;
public void run(){
String string = rr.getRandom(); //类2通过get()方法得到类1的数组变量
System.out.println(string);
file = new File("E:\\","Example.txt");
try{
if(!file.exists()){
file.createNewFile();
}
FileWriter fWriter = new FileWriter(file);
fWriter.write(string);
fWriter.close();
}
catch (IOException e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
ReadRandom rr = new ReadRandom();
WriteRandom wr = new WriteRandom();
rr.start();
wr.start();
}
}
温馨提示:内容为网友见解,仅供参考