英文:
How can I reset a Thread every 10 minutes?
问题
我正在制作一个通过网络中的资源文件夹传递数据的程序。这个程序有两种“类型”,一个是运行程序,另一个是查看程序。运行程序用于保存数据,查看程序用于加载数据。我正在使用OOS和OIS来实现这一功能。运行程序每隔几秒钟将数据保存到资源文件夹,查看程序也会每隔几秒钟从文件夹加载数据。在经过大约10到80分钟的时间后(据我发现),在使用共享网络时会出现EOFException错误,但在Eclipse IDE中,程序可以平稳运行,时间没有限制。
我不知道出现这种情况的原因。我首先尝试的是将加载函数移到一个单独的线程中,但没有帮助。然后我尝试每隔10分钟重置线程,但也没有奏效。这个解决方案在理论上是否可行,还是我的逻辑中存在其他明显的错误呢?
如果需要更多信息,请随时提问。
通过重置线程加载数据的代码:
LinkedList<Thread> threads = new LinkedList<>();
Timer resetTimer = new Timer();
resetTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
threads.clear();
threads.add(new Thread(new Runnable(){
public void run(){
Timer loadDataClock = new Timer();
loadDataClock.schedule(new TimerTask() {
@Override
public void run() {
try {
loadData();
} catch (Exception e) {
advancedStackTrace(e, 1);
}
}
}, 0, 5000);
}
}));
threads.get(0).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 10 * 60 * 1000);
(该代码通过每隔10分钟添加线程到链表中,然后每隔10分钟清空链表并替换为新线程)
在DataHandler类中保存和加载数据的代码:
public static void save(Serializable data, String filename) throws IOException {
haltLoad = true;
try {
try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(Paths.get(filename + "_fir")))) {
oos.writeObject(data);
try {
Path source = Paths.get(filename + "_fir");
Path target = Paths.get(filename);
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
haltLoad = false;
}
public static Object load(String filename) throws ClassNotFoundException, IOException {
if (!haltLoad) {
try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Paths.get(filename)))) {
return ois.readObject();
} catch (Exception e) {
return null;
}
} else {
return null;
}
}
(需要注意的是,变量haltLoad在调用保存方法时阻止加载操作)
英文:
I am making a program that communicates data through a resource folder within a network. There are two "types" of this same program, a runner and a viewer. The runner saves data and the viewer loads data. I am using OOS and OIS to do this. The runner saves the data to the resource every few seconds, and the viewer loads the data from the folder every few seconds as well. After some time between 10-80 minutes (as I have found), I get an EOFException when using a shared network, but in the Eclipse IDE, it runs smoothly for an indefinite time.
I don't know why this is. The first thing I did was move the load function to a separate thread, but that didn't help. Then I tried resetting the Thread every 10 minutes, that didn't work either. Would this solution even theoretically work or is there another glaring error in my logic.
Please ask if you need any more information.
Code for loading data through resetting Thread:
LinkedList<Thread> threads = new LinkedList<>();
Timer resetTimer = new Timer();
resetTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
threads.clear();
threads.add(new Thread(new Runnable(){
public void run(){
Timer loadDataClock = new Timer();
loadDataClock.schedule(new TimerTask() {
@Override
public void run() {
try {
loadData();
} catch (Exception e) {
advancedStackTrace(e, 1);
}
}
}, 0, 5000);
}
}));
threads.get(0).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 10 * 60 * 1000);
(This works by adding Threads to a linked list every 10 minutes and clearing the LinkedList every 10 minutes and replacing it with a new Thread)
Code for loading in saving data within a DataHandler class:
public static void save(Serializable data, String filename) throws IOException {
haltLoad = true;
try {
try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(Paths.get(filename + "_fir")))) {
oos.writeObject(data);
try {
Path source = Paths.get(filename + "_fir");
Path target = Paths.get(filename);
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
haltLoad = false;
}
public static Object load(String filename) throws ClassNotFoundException, IOException {
if (!haltLoad) {
try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Paths.get(filename)))) {
return ois.readObject();
} catch (Exception e) {
return null;
}
} else {
return null;
}
}
(As a note, the variable haltLoad prevents loading while the save method is being called)
专注分享java语言的经验与见解,让所有开发者获益!
评论