2017-09-13 180 views
0

我創建了一個食品訂購系統,當按下按鈕時,它將調用一個方法,然後查詢數據庫以找出當前數量,然後查詢另一個表以找出數量每道菜需要,然後用新數量更新數據庫。這需要一兩秒鐘的時間,所以我想在用戶每次訂購一個項目時在新的線程上運行它,以便在每次按下按鈕後都不會掛起。我試圖用executorService,但我仍然得到一個InvocationTargetException錯誤。JavaFX多次運行線程

下面是代碼:

public ExecutorService executorService = Executors.newSingleThreadExecutor(); 

Task<Boolean> buyDish = new Task<Boolean>() { 
    @Override 
    protected Boolean call() throws Exception { 
     return Datasource.getInstance().insertDishSale(dishId, cost); 
    } 
}; 

public void orderFood(ActionEvent event){ 
    if (event.getSource() == salmon) { 
     dishId = 1; 
     cost = 4.99; 
     executorService.execute(buyDish); 
    } 
} 

我的問題是;

  1. 如何在數據庫更新完成時運行此方法並且不會導致應用程序掛起?

  2. 在另一個線程上這樣做是一個好主意,因爲我只是在想,如果另一個線程試圖更新數據庫,它將處理的數據不是最新的,直到所有其他線程都有也更新了數據庫。

編輯 -

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774) 
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Node.fireEvent(Node.java:8413) 
at javafx.scene.control.Button.fire(Button.java:185) 
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) 
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) 
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) 
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) 
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417) 
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416) 
at com.sun.glass.ui.View.handleMouseEvent(View.java:555) 
at com.sun.glass.ui.View.notifyMouse(View.java:937) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
at java.lang.Thread.run(Thread.java:745) 

Caused by: java.lang.reflect.InvocationTargetException 
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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) 
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) 
... 48 more 

Caused by: java.util.concurrent.RejectedExecutionException: Task [email protected] rejected from [email protected][Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1] 
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047) 
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823) 
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369) 
at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:668) 
at com.lukedavies.controller.TestController.orderFood(TestController.java:44) 
+1

堆棧跟蹤看起來像您試圖在執行程序服務關閉後執行任務。 –

+0

就是這樣,在那種方法中還有其他不相干的東西,我遺漏了(這是重複的),我錯過了方法結束時的關閉。我應該什麼時候關閉執行器服務? –

+1

關閉應用程序時(例如,從Application.stop()方法)關閉執行程序服務。 –

回答

2

堆棧跟蹤表明您要執行任務,你已經關閉了執行售後服務。您通常只應在退出應用程序時關閉執行程序服務,例如

public class MyApplication extends Application { 

    private ExecutorService executorService = Executors.newSingleThreadExecutor(); 

    @Override 
    public void start(Stage primaryStage) { 
     // ... 
    } 

    @Override 
    public void stop() { 
     executorService.shutdown(); 
    } 
} 

或者,如果你正在使用FXML和控制器:

public class MainController { 

    private ExecutorService executorService = Executors.newSingleThreadExecutor(); 

    public void shutdown() { 
     executorService.shutdown(); 
    } 
} 

public class MyApplication extends Application { 

    private MainController mainController ; 

    @Override 
    public void start(Stage primaryStage) { 
     FXMLLoader loader = new FXMLLoader(...); 
     Parent root = loader.load(); 
     mainController = loader.getController(); 

     // ... 

    } 

    @Override 
    public void stop() { 
     mainController.shutdown(); 
    } 
} 

此外,作爲documentation

與FutureTask指出,一個任務是一次性課程,不能是r eused。

(順便說一句,請注意,它也是一個非常糟糕的主意引用在不同的線程可變數據。你dishIdcost似乎是同時從FX應用程序線程和後臺線程引用的實例字段。 )

因此,您不能多次使用相同的Task。每次只需創建一個新的文件:

private ExecutorService executorService = Executors.newSingleThreadExecutor(); 

private Task<Boolean> createBuyDishTask(int dishId, double cost) { 
    return new Task<Boolean>() { 
     @Override 
     protected Boolean call() throws Exception { 
      return Datasource.getInstance().insertDishSale(dishId, cost); 
     } 
    }; 
} 

public void orderFood(ActionEvent event){ 
    if (event.getSource() == salmon) { 
     executorService.execute(createBuyDishTask(1, 4.99)); 
    } 
} 
+0

複製了確切的代碼並仍然得到相同的錯誤 - 線程「JavaFX Application Thread」中的異常java.lang.RuntimeException:java.lang.reflect.InvocationTargetException –

+0

@JohnnyB。在你的問題中發佈完整的堆棧跟蹤(不只是錯誤消息)。 –