相关疑难解决方法(0)

等待JavaFX Application Thread完成处理事件?

这里有很多问题,询问如何暂停后台线程的JavaFX应用程序线程,但我想要相反!

我正在尝试测试一系列关键输入完全处理所需的时间.我正在使用JavaFX 的Automaton测试库,而editor.type(key)生成一个由应用程序处理的按键事件.这是许多尝试之一:

long start = System.nanoTime();

editor.type(AGUtils.LEFT_ARROW);
editor.type(AGUtils.LEFT_ARROW);
editor.type(AGUtils.RIGHT_ARROW);
editor.type(AGUtils.RIGHT_ARROW);

FutureTask<Callable> t = new FutureTask<>(...);
Platform.runLater(t);

while (!t.isDone()) { } // wait for the FutureTask to be called

long end = System.nanoTime();
Run Code Online (Sandbox Code Playgroud)

但是,FX应用程序线程似乎在处理其余的按键事件之前处理FutureTask.

TLDR:我想精确测量JavaFX应用程序线程何时完成处理我生成的四个按键事件.

我怎么能这样做?谢谢!

java testing concurrency multithreading javafx

5
推荐指数
1
解决办法
1149
查看次数

执行前检查异步任务依赖性的设计模式

问题

给定一些异步加载的依赖项,我想在所有依赖项加载完成后才触发一些代码。作为一个简单的例子,考虑以下伪代码:

bool firstLoaded = false, secondLoaded = false, thirdLoaded = false;

function loadResourceOne() {
    // Asynchronously, or in a new thread:
    HTTPDownload("one.txt");
    firstLoaded = true;
    if (secondLoaded && thirdLoaded) {
        allLoaded();
    }
}

function loadResourceTwo() {
    // Asynchronously, or in a new thread:
    HTTPDownload("two.txt");
    secondLoaded = true;
    if (firstLoaded && thirdLoaded) {
        allLoaded();
    }
}

function loadResourceThree() {
    // Asynchronously, or in a new thread:
    HTTPDownload("three.txt");
    thirdLoaded = true;
    if (firstLoaded && secondLoaded) {
        allLoaded();
    }
}

function allLoaded() …
Run Code Online (Sandbox Code Playgroud)

language-agnostic multithreading design-patterns asynchronous

5
推荐指数
1
解决办法
1126
查看次数

Java - ExecutorService 有最大大小

有没有办法通过一个巨大的数据库并并行应用一些工作来获取条目?我尝试使用 ExecutorService,但我们必须关闭()才能知道池大小......

所以我最好的解决方案是:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestCode
{
private static List<String> getIds(int dbOffset, int nbOfArticlesPerRequest) 
{
    return Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29");
}

public static void main(String args[]) throws Exception
{
    int dbOffset = 0;
    int nbOfArticlesPerRequest = 100;
    int MYTHREADS = 10;
    int loopIndex = 0;
    boolean bContinue=true; …
Run Code Online (Sandbox Code Playgroud)

java multithreading

2
推荐指数
1
解决办法
9051
查看次数

Spring Boot:如何在运行时更改内容安全策略?

我正在尝试热重新加载 Spring Boot 应用程序的内容安全策略(CSP)中的更改,即用户应该能够通过管理 UI 更改它,而无需重新启动服务器。

Spring Boot 中常规的做法是:

@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) {
        // ... lots more config here...
        http.headers()
            .addHeaderWriter(
                 StaticHeadersWriter(
                     "Content-Security-Policy", 
                     "<some policy string>"
                 )
            )
    } 
}
Run Code Online (Sandbox Code Playgroud)

...但这不允许在分配后重新配置。

我可以在运行时(重新)配置它吗?重新加载应用程序上下文不是一个选项,我只需要能够适应这个特定的设置。

java spring content-security-policy spring-boot

2
推荐指数
1
解决办法
2909
查看次数

Java线程恼人的行为

我有以下方法,由大约100多个线程运行.

<Method Signature>
{
   System.out.println("Starting Thread with ID :- " + iThreadID);

   <Some piece of code which takes more than 10 seconds to complete>

   System.out.println("Finished Thread with ID :- " + iThreadID);
}
Run Code Online (Sandbox Code Playgroud)

当线程执行时,我得到以下输出.

Starting Thread with ID :- 1
Starting Thread with ID :- 6
Starting Thread with ID :- 14
Starting Thread with ID :- 9
Starting Thread with ID :- 69
Starting Thread with ID :- 21
Starting Thread with ID :- 87
Starting Thread with ID …
Run Code Online (Sandbox Code Playgroud)

java multithreading synchronization

1
推荐指数
1
解决办法
117
查看次数

为什么 Executors.newSingleThreadExecutor() 不能保证顺序?

我在 Windows 11 x64、IntelliJ IDEA 2022 Ultimate 中使用 JDK/Java 19。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ZooInfo {

    public static void main(String[] args) {
        ExecutorService executorService = null;
        Runnable runnable1 = () -> System.out.println("Printing zoo inventory");
        Runnable runnable2 = () -> {
            for (int i = 0; i < 3; i++) {
                System.out.println("Printing record " + i);
            }
        };
        try {
            executorService = Executors.newSingleThreadExecutor();
            System.out.println("Begin");
            executorService.execute(runnable1);
            executorService.execute(runnable2);
            executorService.execute(runnable1);
            System.out.println("End.");
        } finally {
            if (executorService != null) {
                executorService.shutdown();
            }
        } …
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
101
查看次数