我正在使用一个不是开源的项目,我需要修改一个或多个类.
在一个类中是以下集合:
private Map<Integer, TTP> ttp = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我需要做的就是使用反射并在这里使用concurrenthashmap.我试过跟随代码,但它不起作用.
Field f = ..getClass().getDeclaredField("ttp");
f.setAccessible(true);
f.set(null, new ConcurrentHashMap<>());
Run Code Online (Sandbox Code Playgroud) 听起来有点奇怪,但是可以final在运行时添加修改器吗?
我有一个标记为的变量public static int/short.在某些时候我想阻止改变它的值,我希望将其可访问性保持为标准静态值(ClassName.field).
public class Main {
private static int a = 0;
public static void addFinalMod(Field f) {
Field modifiersField = null;
try {
modifiersField = Field.class.getDeclaredField("modifiers");
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
modifiersField.setAccessible(true);
try {
modifiersField.setInt(f, f.getModifiers() & Modifier.FINAL);
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(a);
try {
Field f = Main.class.getDeclaredField("a");
addFinalMod(f);
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
a = …Run Code Online (Sandbox Code Playgroud) 让我们假设我想迭代一组对象.
List<String> strmap = ...
//Without lambdas
strmap.stream().filter(new Predicate<String>() {
public boolean test(String string) {
return string.length == 10;
}
}.forEach(new Consumer<String>() {
public void accept (String string) {
System.out.println("string " + string + " contains exactly 10 characters");
}
}
//With lambdas
strmap.stream()
.filter(s -> s.length == 10)
.forEach(s -> System.out.println("string " + s + " contains exactly 10 characters");
Run Code Online (Sandbox Code Playgroud)
第二个例子(没有lambdas)如何工作?每次调用代码时都会创建一个新对象(Predicate和Consumer),java jit编译器可以优化lambda表达式多少?为了获得更好的性能,我应该将所有lambdas声明为变量并且始终只传递引用吗?
private Predicate<String> length10 = s -> s.length == 10;
private Consumer<String> printer = s -> { "string …Run Code Online (Sandbox Code Playgroud) 我需要从我的依赖项创建一个胖罐子,它不会包含范围内的依赖项compileOnly
dependencies {
api 'org.slf4j:slf4j-api:1.7.26' // this must be in the jar
compileOnly 'it.unimi.dsi:fastutil:8.2.1' // this must not be in the jar
jar {
from {
configurations.compileClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我构建项目时,两个依赖项都存在于最终的 jar 文件中。我该如何从 fat jar 中排除 fastutil?
我尝试使用runtimeOnly
runtimeOnly 'it.unimi.dsi:fastutil:8.2.1' // this must not be in the jar
Run Code Online (Sandbox Code Playgroud)
但这会导致 fastutil 在编译时无法解析。
我正在运行 Java 16 和 Gradle 7.0.2。
我想阅读由 nashorn 引擎生成的字节码。我发现我需要的参数-d=*folder*也是我想应用乐观类型以获得更好的性能,这是由参数启用的-ot
我通过调用方法初始化引擎:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
engine.eval(myscriptfile);
Run Code Online (Sandbox Code Playgroud)
但我还没有找到我应该把 jjs 参数放在哪里。
将同一个对象保存在多个集合中是一种很好的编程习惯吗?
假设我有一张地图,其中包含例如:500+元素,
Map<String,MyObject> map = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我的应用程序适用于多个连接的客户端,我知道每个客户端几乎总是只使用此映射中的±20个已知和不同的元素.
我的问题是,如果我想保存一些迭代,为每个客户创建一个地图是否是个好主意,这个地图将容纳这20个元素.
我正在开发一个与服务器通信的应用程序。
thread = new Thread(new ThreadStart(ClientStart));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
private void ClientStart()
{
tcpClient = new TcpClient(ip,3000);
stream = tcpClient.GetStream();
while (true) {
stream.Read(...);
PrintMessage(...);
.....
}
}
private void PrintMessage(LogWin w, string msg)
{
DateTime dt = DateTime.Now;
w.GetMessageBox().Dispatcher.BeginInvoke(DispatcherPriority.Input,new Action(()=>w.GetMessageBox()
.AppendText(String.Format("{0:d/M/yyyy HH:mm:ss}", dt) + " : " + msg)));
}
Run Code Online (Sandbox Code Playgroud)
稍后我需要将结果打印到消息框。我知道我必须使用 Dispatcher,因为我在另一个线程中工作,但是即使我使用 beginInvoke 方法,我的应用程序也会冻结。
根据谢里丹的回答进行编辑:
现在我得到:WindowsBase.dll 中发生类型为“System.InvalidOperationException”的未处理异常
附加信息:调用线程无法访问此对象,因为其他线程拥有它。
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
thread = new Thread(() => ClientStart(dispatcher));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
private void ClientStart(Dispatcher dispatcher)
{
....
Run Code Online (Sandbox Code Playgroud)
并更改了打印方法:
dispatcher.BeginInvoke(DispatcherPriority.Input, new …Run Code Online (Sandbox Code Playgroud) java ×6
c# ×1
collections ×1
dispatcher ×1
field ×1
final ×1
freeze ×1
gradle ×1
java-8 ×1
java-stream ×1
javascript ×1
lambda ×1
modifier ×1
nashorn ×1
object ×1
reflection ×1
wpf ×1