【发布时间】:2017-07-26 12:34:32
【问题描述】:
我创建了一个 Spring Boot 应用程序。我在那里有一种同步方法。我想创建 JUnit 测试来测试这个方法是否同步。这是类和方法:
public class FileManager {
public synchronized void saveTheFile(String theTemperature, String cityName) throws IOException {
InputStream streamIn = toInputStream(theTemperature, "UTF-8");
OutputStream streamOut = new FileOutputStream(cityName + ".txt", true);
try {
IOUtils.copy(streamIn, streamOut);
} finally {
IOUtils.closeQuietly(streamIn);
IOUtils.closeQuietly(streamOut);
}
}
}
我想我需要为此使用多线程,但我什至不知道如何启动我的@Test。请指教!
【问题讨论】:
-
看起来你不需要在这里同步任何东西,因为唯一使用的变量是不可变参数。除非
toInputStream处理共享资源? -
你试过 ConcurrentUnit github.com/jhalterman/concurrentunit 吗?
-
如果你愿意,我可以向你展示我的所有代码,但简而言之,我的应用程序会从用户那里获取请求(城市名称),并将该城市的温度保存到具有城市名称的文件中。在该应用程序每小时获取该城市的温度并将其保存到同一个文件之后,直到该应用程序没有被杀死:) 同步的想法来自于很少有用户可以同时请求同一个城市的想法。
标签: java spring multithreading junit synchronized