我试图描述一个简单的算法:"插入排序".代码获取要排序的元素数,然后随机创建那些排序开始之后的元素.
我正在使用Visual VM进行性能分析.但是,当我开始分析时,我在控制台上看到以下警告消息:
objc[62944]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin/java (0x10004f4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1078054e0). One of the two will be used. Which one is undefined.
Profiler Agent: Waiting for connection on port 5140 (Protocol version: 15)
Profiler Agent: Established connection with the tool
Profiler Agent: Local accelerated session
Profiler Agent Warning: JVMTI classLoadHook: class name is null.
Profiler Agent Warning: JVMTI classLoadHook: class name is null.
Profiler engine warning: class
sun.reflect.GeneratedSerializationConstructorAccessor29 that should be instrumented is not loaded by …
Run Code Online (Sandbox Code Playgroud) 我试图理解多线程如何在Java中工作.我明白之间的差别Volatile
和Synchronization
.
Volatile
是关于可见性,并不保证同步.当我们使用多线程环境时,每个线程在它们正在处理的变量的本地缓存上创建自己的副本.更新此值时,更新首先在本地缓存副本中发生,而不是在实变量中发生.因此,其他线程与其他线程正在更改的值无关.这就是volatile
图片.易失性字段立即写入主存储器,并从主存储器读取.
片段Thinking In Java
-
同步还会导致刷新到主内存,因此如果某个字段完全被同步的方法或块保护,则不必使其变为易失性.
如果类只有一个可变字段,那么通常只使用volatile而不是synchronized来安全.同样,您的第一选择应该是使用synchronized关键字 - 这是最安全的方法,并且尝试做任何其他事情都是有风险的.
但我的问题是,如果在同步块中,正在修改非易失性共享变量,其他线程是否会看到更新的数据?(由于有问题的变量是非易失性的,其他线程应该从缓存而不是主要主内存中读取陈旧数据)
如果对上述问题的答案是NO
,那么我可以得出结论,每次我使用同步时,我应该确保必须标记共享变量volatile
吗?
如果答案是YES
,那么这是否意味着我总是可以使用synchronization
而不是标记共享变量volatile
?
ps:在提出这个问题之前,我已经在StackOverflow和其他网站上阅读了很多答案,但我找不到我的问题的答案.
可以说我们有两个hazelcast实例:
HazelcastInstance firstInstance = Hazelcast.newHazelcastInstance(new Config());
HazelcastInstance secondInstance = Hazelcast.newHazelcastInstance(new Config());
// Add entries to firstInstance
// Add entries to secondInstance
Run Code Online (Sandbox Code Playgroud)
现在我想删除一切firstInstance
,然后add everything from
secondInstance to firstInstance
.
有没有办法实现这个目标?
我正在使用jdk 1.9.我想提出一个简单的ReST服务.使用maven原型创建项目:jersey-quickstart-webapp
.然后我继续修改了pom.xml和web.xml.我的maven配置如下所示:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>restWebService</groupId>
<artifactId>restWebService</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>restWebService</name>
<build>
<finalName>restWebService</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<inherited>true</inherited>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-bundle -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-core -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId> …
Run Code Online (Sandbox Code Playgroud) 我正在使用Map<String, Optional<List<String>>>
.我得到一个明显NullPointerException
的结果是因为结果是null
关键.
有没有办法处理null情况?
public Map<MyEnum, Optional<List<String>>> process(Map<MyEnum, Optional<List<String>>> map) {
Map<MyEnum, Optional<List<String>>> resultMap = new HashMap<>();
// Getting NullPointerException here, since map.get(MyEnum.ANIMAL) is NULL
resultMap.put(MyEnum.ANIMAL, doSomething(map.get(MyEnum.ANIMAL).get()));
// do something more here
}
private Optional<List<String>> doSomething(List<String> list) {
// process and return a list of String
return Optional.of(resultList);
}
Run Code Online (Sandbox Code Playgroud)
我试图if-else
通过使用Optional 避免检查null.
如何测量或找到 Zipf 分布?例如,我有一个英语单词语料库。我如何找到 Zipf 分布?我需要找到 Zipf 分布,然后绘制它的图形。但我陷入了第一步,即找到 Zipf 分布。
编辑:从每个单词的频率计数来看,很明显它遵守 Zipf 定律。但我的目标是绘制一个 zipf 分布图。我不知道如何计算分布图的数据
我正在尝试学习Java 8的功能,尤其是它的函数式编程方面.所以我试图解决一个问题:在数组中查找领导者 - 领导者是一个元素,它比数组中右边的所有元素都大.
例如:
输入数组:{98,23,54,12,20,7,27}
产出:领导者 - 27 54 98
现在,我已经使用通常的迭代方法解决了这个问题,如下所示.
private static void findLeaders(int[] array) {
int currentLeader = array[array.length - 1];
System.out.println(currentLeader);
for(int i = array.length - 1; i >= 0; i--) {
if(array[i] > currentLeader) {
System.out.println(array[i]);
currentLeader = array[i];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用Java 8解决它,但除了编写这段代码之外我做不了多少,这又有编译错误:
Function<Integer, Integer> checkLeader = i -> i > currentLeader ? i : currentLeader;
Run Code Online (Sandbox Code Playgroud)
错误:在封闭范围内定义的局部变量currentLeader必须是最终的或有效的最终
现在,我如何使用Java 8的功能解决相同的问题.
我为这个看似简单且几乎是一个愚蠢的问题道歉,但我花了很多时间来修复它而没有太大的成功.
我创建了一个非常简单的maven项目,然后在这个src/resources
文件夹中创建了一个简单的文本文件.
pom.xml很简单.这个App
类看起来像这样:
public class App {
public static void main(String[] args) throws IOException {
Files.lines(Paths.get("test.txt")).forEach(System.out::println);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的例外:
Exception in thread "main" java.nio.file.NoSuchFileException: test.txt
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.nio.file.Files.newInputStream(Files.java:152)
at java.nio.file.Files.newBufferedReader(Files.java:2784)
at java.nio.file.Files.lines(Files.java:3744)
at java.nio.file.Files.lines(Files.java:3785)
at com.rahul.App.main(App.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)
这似乎是一个非常愚蠢的问题,但我无法理解它.任何帮助都是真诚的感谢.
我正在尝试通过Java执行以下步骤的步骤:
1)使用给定的URL连接到共享点站点。
2)获取该页面上列出的文件列表
3)使用修改日期过滤文件
4)使用创建日期和修改日期执行更多检查
5)最后将文件保存到Unix框中。
截至目前,我已经能够访问特定文件并进行读取。但是,在读取文件之前,我需要掌握文件的元数据。是否有API或以Java方式完成所有这些操作的方法。
谢谢
我在这里学习MongoDB.我坚持使用MongoDB Java驱动程序执行非常基本的操作.我试图从文档中的数组中删除一个元素.该文件如下:
db.test.find().pretty()
{
"_id" : ObjectId("581245dd51030d389f5cf701"),
"name" : "Rahul",
"scores" : [
{
"SDM" : 97
},
{
"SE" : 96
},
{
"DM" : 80
}
]
}
Run Code Online (Sandbox Code Playgroud)
我试图删除数组中的第二个元素即{"SE" : 96}
.我明白我可以用这个来实现coll.update(match, new BasicDBObject("$pull", update));
.但是我想利用这个com.mongodb.client.model.Updates.pull(final String fieldName, final TItem value)
方法.
这是我在发布问题之前尝试过的:
1)当我尝试这个时,我没想到会发生任何事情,而且确实没有对文件进行任何修改.
` Bson filter = Filters.eq("name", "Rahul");
Bson delete = Updates.pull("SE", 96);
collection.updateOne(filter, delete);`
Run Code Online (Sandbox Code Playgroud)
2)以下是抛出异常.
` Bson filter = Filters.eq("name", "Rahul");
Bson delete = Updates.pull("scores.SE", 96);
collection.updateOne(filter, delete);`
Run Code Online (Sandbox Code Playgroud)
例外:
Exception in thread …
Run Code Online (Sandbox Code Playgroud)