我在不同的博客中读过有关显示器的不同内容.所以我现在有点困惑.
据我所知,monitor是一个确保只有一个线程在关键部分执行代码的人.那么如果我们有3个同步的方法/块,那么我们将有3个监视器来确保只有一个线程在临界区?
如果以上是真的那么为什么说在Java中每个对象都有一个与之关联的监视器?它应该是每个同步块与监视器相关联.
我在很多地方看到过,冒泡排序的复杂性是O(n 2).
但是怎么会这样呢,因为内环应该总是运行ni次.
for (int i = 0; i < toSort.length -1; i++) {
for (int j = 0; j < toSort.length - 1 - i; j++) {
if(toSort[j] > toSort[j+1]){
int swap = toSort[j+1];
toSort[j + 1] = toSort[j];
toSort[j] = swap;
}
}
}
Run Code Online (Sandbox Code Playgroud) Java 8中功能接口的定义说:
功能性接口被定义为具有任何接口恰好 一个明确声明的抽象方法.(鉴定是必要的,因为接口可能具有非抽象的默认方法.)这就是为什么功能接口曾经被称为单抽象 方法(SAM)接口,这个术语有时仍然可见.
那么我们怎么会这样:
List<Double> temperature =
new ArrayList<Double>(Arrays.asList(new Double[] { 20.0, 22.0, 22.5 }));
temperature.sort((a, b) -> a > b ? -1 : 1);
Run Code Online (Sandbox Code Playgroud)
由于sort
方法List
是:
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}
Run Code Online (Sandbox Code Playgroud)
lambda表达式说:
Lambda表达式应该可以分配给功能接口
该Comparator
接口有两个抽象方法,它们是compare
和equals
用它们注释的@FunctionalInterface
.这是否违反了只有一个抽象方法的功能接口的定义?
我正在尝试使用 mvn depenedency get 从 repo 下载 tar 文件。这是命令:
mvn dependency:get -DgroupId=com.sample -DartifactId=sample-project -Dversion=1.0.1-SNAPSHOT -Dtransitive=false -Dpackaging=tar
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
[ERROR] : Premature end of Content-Length delimited message body (expected: 56473600; received: 6843
[ERROR] -> [Help 1]
Run Code Online (Sandbox Code Playgroud)
相同的命令适用于 Windows。
如果我更改命令以下载 jar,它会起作用:
mvn dependency:get -DgroupId=com.sample -DartifactId=sample-project -Dversion=1.0.1-SNAPSHOT -Dtransitive=false -Dpackaging=jar
Run Code Online (Sandbox Code Playgroud) 我确实注意到有关静态和非静态内部异常类的奇怪行为。
例如下面的代码不会编译:
public class MyClass<T> {
private class MyInnerException extends Exception { // won't compile
..
}
}
Run Code Online (Sandbox Code Playgroud)
但下面的代码将编译:
public class MyClass<T> {
private static class MyInnerException extends Exception { // will compile
..
}
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
有一点是肯定的,由于类型擦除,我们不能有泛型异常,但是上面的不是泛型异常类,而是在泛型类中。但是如果它是静态的它是允许的,但如果它是非静态的它是不允许的?
后续问题,是否因为 Generic 的类型擦除功能而不允许通用异常,就像这里提到的:https : //www.mscharhag.com/java/java-exceptions-and-generic-types 或者还有其他一些原因要吗?
我的要求是将给定时区中特定格式的给定日期与同一时区中的当前日期进行比较。此外,在比较时,我必须忽略时区。比较结果应该是:0 or 1 or -1
我尝试过的一种方法是
public void compareDate(){
TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date todayDate = dateFormatter.parse(dateFormatter.format(new Date()));
Date date = dateFormatter.parse("2021-02-28");
System.out.println(todayDate.compareTo(date));
}
Run Code Online (Sandbox Code Playgroud)
但以上对我来说看起来效率低下。
其他方法可能是获取如下所示的两个日期然后进行比较?
public static Date getDateWithoutTimeUsingCalendar() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
Run Code Online (Sandbox Code Playgroud)
有人可以建议一个更好的选择吗?
只有一件事,时区和没有时间的比较必须在那里。
我编写了一个CTE查询,我在Microsoft SQL Server 2008 R2 Management Studio中执行查询:
WITH DependencyHierarchy(processName, dependProcessName) AS
(
SELECT
processName,
dependProcessName,
1 as HierarchyLevel
FROM processDependency
UNION ALL
SELECT
e.processName,
e.dependProcessName,
eh.HierarchyLevel + 1 AS HierarchyLevel
FROM
processDependency e
INNER JOIN
DependencyHierarchy eh ON e.dependProcessName = eh.processName
)
SELECT *
FROM DependencyHierarchy
ORDER BY HierarchyLevel, processName, dependProcessName;
GO
Run Code Online (Sandbox Code Playgroud)
它抛出此错误:
解析查询时出错.[令牌行号= 1,令牌行偏移= 1,令牌错误= WITH]
该表包含以下数据:
processName dependProcessName
P1 P2
P2 P3
P3 P4
P4 P5
P6 P7
Run Code Online (Sandbox Code Playgroud) sql t-sql sql-server common-table-expression sql-server-2008
我正在查看HashMap的源代码,但是二进制运算符使很多人感到困惑。
我确实了解以下的一般目的,公平分配并将hashCode限制在存储桶限制之内。
有人可以在这里解释评论吗?立即进行操作有什么好处?
/**
* Computes key.hashCode() and spreads (XORs) higher bits of hash
* to lower. Because the table uses power-of-two masking, sets of
* hashes that vary only in bits above the current mask will
* always collide. (Among known examples are sets of Float keys
* holding consecutive whole numbers in small tables.) So we
* apply a transform that spreads the impact of higher bits
* downward. There is a tradeoff between speed, utility, and
* …
Run Code Online (Sandbox Code Playgroud)