我已经阅读了很多Java 8 Optional,我确实理解了这个概念,但是在我的代码中尝试自己实现它时仍然遇到困难.
虽然我已经把网络作为很好的例子,但我没有找到一个有很好解释的网站.
我有下一个方法:
public static String getFileMd5(String filePath) throws NoSuchAlgorithmException, IOException {
AutomationLogger.getLog().info("Trying getting MD5 hash from file: " + filePath);
MessageDigest md = MessageDigest.getInstance("MD5");
InputStream inputStream;
try {
inputStream = Files.newInputStream(Paths.get(filePath));
} catch (NoSuchFileException e) {
AutomationLogger.getLog().error("No such file path: " + filePath, e);
return null;
}
DigestInputStream dis = new DigestInputStream(inputStream, md);
byte[] buffer = new byte[8 * 1024];
while (dis.read(buffer) != -1);
dis.close();
inputStream.close();
byte[] output = md.digest();
BigInteger bi = new BigInteger(1, output);
String …Run Code Online (Sandbox Code Playgroud) 我被告知我应该考虑在我的代码中对Checked异常抛出Unchecked异常,而不仅仅是这样,而是用我自己的扩展RuntimeException.现在,我确实理解了两者之间的区别,但仍然不明白我为什么要这样做?
如果我有这个方法标题,抛出2种异常:
public static Optional<String> getFileMd5(String filePath) throws NoSuchAlgorithmException, IOException {}
Run Code Online (Sandbox Code Playgroud)
为什么我要用一个(不太详细的)例外替换它们?
java exception-handling checked-exceptions unchecked-exception
我正在尝试编写一个程序,在Main类中可以启动未知数量的新线程.每个线程依次调用Singleton Copier类,该类应该调用文件传输操作.
我的目标是,无论线程请求数量多少,都要将并发传输的数量限制为2次传输,所以我想用它来解决它Semaphore.我的问题是,似乎线程一个接一个地运行而不是并发运行.
这是我试图做的:
public class Copier {
private static final int POOL_SIZE = 2;
private static volatile Copier instance = null;
private static Semaphore semaphore;
private Copier() {
}
public static Copier getInstance() {
if (instance == null) {
synchronized (Copier.class) {
if (instance == null) {
instance = new Copier();
semaphore = new Semaphore(POOL_SIZE);
}
}
}
return instance;
}
public void fileTransfer(CopyThread copyThread) {
try {
semaphore.acquire();
System.out.println("Running thread..."); …Run Code Online (Sandbox Code Playgroud) 我有一个父类 -Product
public abstract class Product {}
Run Code Online (Sandbox Code Playgroud)
并扩展了3个子类:
public class Vinyl extends Product {}
public class Book extends Product {}
public class Video extends Product {}
所有子类都preview()使用其特定实现覆盖该方法.现在,我有一个新的设计需求:我需要定义一个&的组合项目,它也有一个方法(这是乙烯基和书籍的组合).在说明中它说我可以创建Interface\class字段成员或我想支持它的任何实现,但我不确定如何.vinylbookpreview()
新设计是否也应该用inheritance或者我应该改变当前的设计?
我创建了一个新的“配置表单”屏幕,其中包含多个文本输入。我的目标是使用这些输入变量并将它们合并到一个属性文件中。
我使用的是 6.1.6 版 - 因此我为此创建了“修改 ZIP 文件”操作并将其放在安装程序的安装文件下。在操作的“修改操作”属性中,我创建了新的“将属性写入文件”操作,并在其中的“属性定义源”下选择了安装程序变量选项。然后让我绑定我之前定义的变量,这是有道理的。
我的问题是运行安装程序后,属性文件不会得到更新。打开 installation.log 时,我收到下一条消息:
[ERROR] com.install4j.runtime.beans.actions.properties.WritePropertiesFileAction [ID 1540]: Properties source variable db.database is not an instance of java.util.Map
Run Code Online (Sandbox Code Playgroud)
更新:
我将变量名设置为:“${installer:db.database}”,它应该将它放在 Map 中,现在我仍然收到错误消息:
[ERROR] com.install4j.runtime.beans.actions.properties.WritePropertiesFileAction [ID 1540]: Properties source variable postgres has not been set
Run Code Online (Sandbox Code Playgroud) 我找到了这个答案:在一组数字中找到缺失数字的最快方法,当你只有一个数字丢失时这很好.
继续这个问题 - 我想知道找到所有缺失数字的最佳(和最快)方法是什么,以及对未排序数组进行排序.(例如,数组就像链接问题中描述的那样 - 数组大小为100,随机数为1-100,但其中一些缺失)
我确信基本术语MultiThreading对我来说很清楚 - 一个进程由多个线程组成,可以同时运行它们,对吧?
我遇到了这个网站,声明:
一次只能有一个线程在一个进程中运行.
线程调度程序主要使用抢占式或时间切片调度来调度线程.
那么在写作时会发生什么:
public static void main (String [] args) {
new CalcThread("CalcThread A").start();
new CalcThread("CalcThread B").start();
}
Run Code Online (Sandbox Code Playgroud)
假设线程调度程序选择thread a先运行,并且假设thread b它将在它之后立即安排.是thread b要运行后才能thread a终止?
如果是这种情况 - 为什么称它为并发行为?