Results 1 to 5 of 5
- 09-02-2010, 02:12 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
How to invoke thread2 after the thread1
I am trying to create a Java Desktop Application that validates GML file as my thesis.
Right now, I am phasing to log all the errors found in the thread1 (extends SwingWorker)
thread1: (it may call simultaneously)
1) validate gml file.
2) log the errors to a Vector.
3) when it is finished, call the done() method. Then would like to call the thread2 (SwingWorker or implement Runnable Interface - not yet decided) to write all the Vector contents to a log file.
in the number 3), I am thinking of creating a synchronized method to write a file instead of the creating thread2. So that, I may able to retain the arranged errors(Vector's contents).
- 09-02-2010, 02:16 AM #2
Do you have any specific questions or problems?
- 09-02-2010, 05:13 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
- 09-02-2010, 01:07 PM #4
By 'call' I assume you mean to create a thread and call its run method. Is that right?how to call another thread after SwingWorker.done() method.
I don't understand your problem.
Do you want to place the code to create and call run for the new thread in the done() method?
Without a working program that shows your problem, its hard to say.
- 09-02-2010, 04:31 PM #5
perhaps the join() method is what you are looking for. when you invoke the join() method the execution of the program waits till the called thread has finished. look at this example:
Java Code:public class ExecuteThreads { public static void main(String[] args) { System.out.println("main started"); ThreadExample thread1 = new ThreadExample( "validate gml file and log errors"); ThreadExample thread2 = new ThreadExample("SwingWorker"); thread1.start(); try { thread1.join(); } catch (InterruptedException ex) { } thread2.start(); try { thread2.join(); } catch (InterruptedException ex) { } System.out.println("main finished"); } }
inside the main() method two threads are instantiated, one for simulating the "gml file and log errors" and the other for simulating the "SwingWorker". after starting the thread1 the join() method is called, so that the following statement will wait, until thread1 has finished (after 3000 millis) and then thread2 is started and the output "main finished" is also waiting the thread2 to finished, since the join() method is invoked on thread2. after that also "main finished" is printed on the console. to complete here is the code for the thread:
Java Code:public class ThreadExample extends Thread { private String work; public ThreadExample(String s) { work = s; } public void run() { System.out.println(work + " started"); try { Thread.sleep(3000); } catch (InterruptedException e) { System.out.println("interrupt received"); } System.out.println(work + " ended"); } }
note the order of the following output, which correspond to the order the code is executed:
main started
validate gml file and log errors started
validate gml file and log errors ended
SwingWorker started
SwingWorker ended
main finished
Similar Threads
-
Loop through an Array and invoke SQL command
By Robert_85 in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-25-2010, 12:14 PM -
How invoke open dialog?
By artemff in forum CLDC and MIDPReplies: 4Last Post: 12-27-2009, 03:18 PM -
How to invoke a C API using Java program
By mgopi in forum New To JavaReplies: 6Last Post: 12-27-2008, 12:17 PM -
Invoke Applet
By chankokchern in forum Java AppletsReplies: 1Last Post: 07-29-2008, 04:35 PM -
Re address to jsp to that I invoke it
By Marcus in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-09-2007, 04:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks