Tomas Bisciak
Tomas Bisciak

Reputation: 2841

Platform.RunLater like behaviour thread imeplementation based on JFXAT PlatformImpl to throw work at

So I decided to create my own Platform,PlatformImpl like implementation to basically avoid intensive synchronization and synchronization problems at places where it should not be needed, my requirement is that I have a thread that I can throw work at from JFXAT or other threads that I happen to be in. What I do now its creating thread from JFXAT when user has to for instance delete something from database on user input and then reflect this change in model etc since I dont want my communication to happen on JFXAT.

Am I going little overboard with this? I like how JavaFX and swing has dedicated thread that you just throw work at and you dont have to expect any problems, things I create threads for really would need just some one thread where queue of jobs would be constantly executed as needed. Preferably FIFO structure emebeded within.

Is there already elegant solution for this ? Some API in standard java that I don't know about? I don't really have place to reffer to when I want to execute my application logic thats my problem , I create a lot of threads or execute on JFXAT (light weight tasks nothing heavy/bud might change) and I'm in constant worry about synchronization and forced to use thread safe collections, I know this is pretty dumb question bud i tend to overcomplicate things in my design.

Upvotes: 0

Views: 248

Answers (1)

fabian
fabian

Reputation: 82461

Executors seem to fit your needs.

More precisely you describe the kind of ExecutorService that Executors.newSingleThreadExecutor() returns.

Example:

private final ExecutorService executor = Executors.newSingleThreadExecutor();

public void runLater(Runnable runnable) {
    executor.execute(runnable);
}

Upvotes: 2

Related Questions