我必须加载文件并在其中打印数据,但是线程都是并行运行的,因此几秒钟之后只显示最后一个文件中的数据。总共有七个文件,我将它们循环加载。
这是我的代码:
public class thread extends Thread {
private static final String String = null;
static String file="Lab5File";
static String output="test2.dat";
static ArrayList<Thread> threadList = new ArrayList<Thread>();
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
threadList = new ArrayList<>();
for (int l=1;l<8;l++ )
{
thread1111 x = new thread1111(file+l+".dat");
threadList.add(x);
x.start();
}
try {
int ch = System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static class thread1111 extends Thread {
String name ;
static DataOutputStream dos ;
static FileOutputStream fos;
static ArrayList<Thread> threadList;
public thread1111(String fileName)
{
name = fileName;
try {
fos = new FileOutputStream("test2.dat");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dos = new DataOutputStream(fos);
}
public void run(){
InputStream is = null;
DataInputStream dis = null;
System.out.println("TRYING.........");
try{
// create input stream from file input stream
is = new FileInputStream(name);
// create data input stream
dis = new DataInputStream(is);
while (true) synchronized(dos) {
int Zip = dis.readInt();
String City = dis.readUTF();
String State = dis.readUTF();
double Longitude =dis.readDouble();
double Latitudes=dis.readDouble();
int Zone = dis.readInt();
int dst = dis.readInt();
dos.writeInt(Zip);
dos.writeUTF(City);
dos.writeUTF(State);
dos.writeDouble(Longitude);
dos.writeDouble(Latitudes);
dos.writeInt(Zone);
dos.writeInt(dst);
System.out.println(Zip+"\t"+City+"\t"+State+"\t"+Longitude+"\t"+Latitudes+"\t"+Zone+"\t"+dst+"\n");
}
}catch(Exception e){
// if any I/O error occurs
e.printStackTrace();
}finally{
// releases any associated system files with this stream
if(is!=null)
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(dis!=null)
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}}
答案 0 :(得分:0)
几个选项,
您可以使用Thread.join
您可以使用CyclicBarrier来同步您的主题。
Single thread executor有足够大的队列(7)以满足您的需求。
答案 1 :(得分:0)
只需使用Thread类,您需要调用join()以同步线程:
Thread1.start();
Thread1.join();
Thread2.start();
Thread2.join();
Thread3.start();
Thread3.join();
...
您可以将主循环更改为
for (int l=1;l<8;l++ )
{
thread1111 x = new thread1111(file+l+".dat");
// threadList.add(x);
x.start();
x.join()
}
然后循环将不会迭代到下一个线程,直到当前线程完成 - 当然你可以把整个循环放到它自己的线程中
答案 2 :(得分:0)
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
public class Test {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(7);
ReentrantLock lock = new ReentrantLock(true);
for (int i = 0; i < 7; i++) {
service.execute(new Runner("File" + i, lock));
}
service.shutdown();
}
}
class Runner implements Runnable {
String fileName;
ReentrantLock lock;
public Runner(String name, ReentrantLock lock) {
this.fileName = name;
this.lock = lock;
}
@Override
public void run() {
lock.lock();
System.out.println("Current Thread: "
+ Thread.currentThread().getName() + " Executing file: "
+ fileName);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lock.unlock();
}
}
将您的代码放在Runnable类的run方法中,以打印文件数据并根据您自己的需要进行更改。