Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default 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).

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Do you have any specific questions or problems?

  3. #3
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Do you have any specific questions or problems?
    my problem is on number 3): how to call another thread after SwingWorker.done() method.

    or if there is other better approach, please share it.

    thanks.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    how to call another thread after SwingWorker.done() method.
    By 'call' I assume you mean to create a thread and call its run method. Is that right?
    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.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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

  1. Loop through an Array and invoke SQL command
    By Robert_85 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-25-2010, 12:14 PM
  2. How invoke open dialog?
    By artemff in forum CLDC and MIDP
    Replies: 4
    Last Post: 12-27-2009, 03:18 PM
  3. How to invoke a C API using Java program
    By mgopi in forum New To Java
    Replies: 6
    Last Post: 12-27-2008, 12:17 PM
  4. Invoke Applet
    By chankokchern in forum Java Applets
    Replies: 1
    Last Post: 07-29-2008, 04:35 PM
  5. Re address to jsp to that I invoke it
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-09-2007, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •