Home > Java > javaTutorial > How to Properly Implement Threading for Database Requests in JavaFX to Avoid UI Blocking?

How to Properly Implement Threading for Database Requests in JavaFX to Avoid UI Blocking?

Susan Sarandon
Release: 2024-12-26 10:20:12
Original
529 people have browsed it

How to Properly Implement Threading for Database Requests in JavaFX to Avoid UI Blocking?

Implementing Threading for Database Requests

You encounter an exception due to accessing the JavaFX UI from a separate thread. Here's how you can correctly implement threading:

General Principles of Threading in JavaFX

  • Rule 1: Any modification or access to the scene graph must be performed on the FX Application Thread.
  • Rule 2: Long-running operations should be executed on non-FX Application Threads.

Implementation Using Threads

Create a thread and start the database request on it:

Thread t = new Thread(new Runnable() {
    public void run() {
        requestCourseName();
    }
}, "Thread A");

t.start();
Copy after login

Using JavaFX Concurrent API

The javafx.concurrent API provides a convenient way to manage background threads and update the UI.

  • Task: Encapsulate the background operation.
  • Background Task Thread: Executes the Task's call() method on a background thread.
  • FX Application Thread: Updates the UI on completion via event handlers registered to Task.

Example Using WidgetDAO and Task

// DAO encapsulates database access
public class WidgetDAO {
    public List<Widget> getWidgetsByType(String type) { ... }
}

// Controller handles UI operations
public class MyController {
    private WidgetDAO widgetAccessor;
    private Executor exec;

    public void searchWidgets() {
        String searchString = widgetTypeSearchField.getText();
        Task<List<Widget>> widgetSearchTask = new Task<>() {
            @Override
            protected List<Widget> call() throws Exception {
                return widgetAccessor.getWidgetsByType(searchString);
            }
        };

        widgetSearchTask.setOnSucceeded(e -> widgetTable.getItems().setAll(widgetSearchTask.getValue()));
        exec.execute(widgetSearchTask);
    }
}
Copy after login

Example Using MyDAO and Task

// DAO encapsulates database access
public class MyDAO {
    public Course getCourseByCode(int code) { ... }
}

// Controller handles UI operations
public class MyController {
    private MyDAO myDAO;
    private Executor exec;

    public void searchCourse() {
        int courseCode = Integer.valueOf(courseId.getText());
        Task<Course> courseTask = new Task<>() {
            @Override
            protected Course call() throws Exception {
                return myDAO.getCourseByCode(courseCode);
            }
        };

        courseTask.setOnSucceeded(e -> {
            Course course = courseTask.getValue();
            courseCodeLbl.setText(course.getName());
        });
        exec.execute(courseTask);
    }
}
Copy after login

By implementing threading correctly, you ensure that long-running database requests do not block the UI thread, resulting in a responsive user interface.

The above is the detailed content of How to Properly Implement Threading for Database Requests in JavaFX to Avoid UI Blocking?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template