java - Thread does not start in a Swing application -


I am working on a relatively simple DB manager, which takes particular information in many files, purses and catalogs Fashion I wrote a simple GUI in swing for this purpose, to speed up the process, I want to implement multithreading in parallel parts of execution to speed up the program.

The code below sits in a class named FDBCreatePanel , a custom JPNEL , which sits in FDBManagerFrame , Which adjusts the main method.

  Private Zero dbCreateActionButtonActionPerformed (java.awt.event. ActionEvent evt) {// GEN-FIRST: event_dbCreateActionButtonActionPerformed jfc = New JFileChooser (); Jfc.setVisible (true); Jfc.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY); Int returnVal = jfc.showSaveDialog (empty); ((FDBManagerFrame) SwingUtilities.getRoot (this)). StartProcessAnimation (); If (returnVal == JFileChooser.APPROVE_OPTION) {new swingworker & lt; Zero, zero> () {@Override Safe Voice Dow Inbound ()) throws an exception {file dir = jfc.getSelectedFile (); DBManager DBM = new DBMenegger (DIR, DBNAFieldGettext ()); Try {dbm.doTimeConsumingStuff (); } Grip (eclipse e) {// error hanging} @ Override safe wired () ((FDB managerframe) Swing UTGE.GetRoot (FDBCRPNell.Is)). Interaction animation (); }}. Execute (); }  

The time consuming method in class ParserType1 in the class leads to the following bits of code (among other) :

  Private Zero Init () {try.reader = new LineNumberReader (New FileReader (this.datfile)); Thread T = New Thread (New Runnabel) {@ Override Public Video Run () {Entry E; While ((e = perspectiveenter)! = Null) queue.fofer (e);}}, "t1-parser-thread"); T.run (); } Hold (FileNotFoundException e) {// Error handling}}  

When I monitor, I do not see any t1-parser-thread in JVisualVM The execution of my program It appears that my code completely executes on a thread, ignoring the beginning of the new thread. Am I missing something about threading and swing?

You call run () to the newly created The thread is in the object ParserType1.init () . It does not start a new thread - it executes the run () method of the thread in the current thread. Instead, you should call start () .

Originally I think it was a mistake on the Runnable to implement this thread all - "this is the code that executed Should go "(the runnable ) and the difference between" this is the way I am going to execute it "( threads / code>) is unfortunately blurred due to the fact that threads can also write runnab and make worse.

  Runnable runnable = new Runnable () {...}; Thread thread = new thread (runner) {// override goes here ...};  

As long as your override runs method calls super.run () , runner passed in the manufacturer Gaya has been ignored. crazy stuff. Do not apply threads should be , and do not apply Runnabel , and force you have a runnable To provide. Unfortunately, it's too late to change: (

Actually, you should never run on thread At least, I can not remember the last time that I have no bugs.


Comments