Running 2 threads and Displaying Best Result

I'm working on a Travelling Salesman Solver, I'm trying to make it execute in two threads and return the path drawing(Jframe) and the path distance/output of the best search.

My Code is below:

  Executer myRunnable = new Executer();
    Thread t = new Thread(myRunnable);
    int tdist=(Executer.returnPathValue());
    int []comparet = returnSearchedPath();

    Thread t1 = new Thread(myRunnable);

    int t1dist=(Executer.returnPathValue());
    int []comparet2 = returnSearchedPath();


The output from this is

32 10 51 13 12 26 24 23 47 45 15 49 19 29 28 46 25 27 11 50 3 43 22 20 41 1 6 16 2 17 30 21 0 48 31 44 18 40 7 8 9 14 5 4 37 36 33 34 35 38 39 42 32->dist 8904

My only problem NOW is accomplishing the same result without using .join() and let the threads run at the same time. Any ideas?

2 answers

  • answered 2018-03-13 20:39 Dax Loy

    Basically, you are needing an ArrayList of your threads and a variable to hold the current best path. In the thread class, you need two Synchronized methods. Synchronized methods are ones that are thread-safe. This means that if you have multiple threads running you can change data in another thread safely. If you want to read more about how that works you can go to this link here. You will need one method to update the current best route if the route the thread found is less than the current best and you will need one function to remove itself from the ArrayList in the main thread when it is finished. This means you would have a blocking while loop in the main thread that looks something like this:

    //Where threads is the ArrayList of all of your threads
        //Wait here

    Then after that while ends you can check the current best path variable and that should be the best one. Now you do not have to wait for it to break out of the while loop to update the JFrame. You can change the JFrame when the current best does not match what the current JFrame is showing inside the while loop but once you get out you should double check that the correct one is being shown. Exiting means all threads are done so the final answer would be whatever is left in the current best path.

  • answered 2018-03-13 20:39 markspace

    Do I HAVE to wait for both of them to complete basically I want to accomplish the same result I have without using .join

    I think I understand. There are techniques where you can get output from a thread without waiting on it specifically. The basic technique is a producer-consumer pattern.

    Here's a simple way to do this in Java. First, make a global thread-safe queue.

    public class Global {
      public static final ConcurrentLinkedQueue outputQueue = new ConcurrentLinkedQueue();

    Then pass this queue in to any thread you want. Use this queue to save output from that thread.

    public class MyTask implements Runnable {
      private final Queue output;
      /** 'q' must be thread safe */
      public MyTask( Queue q ) {
        output = q;
      public void run() {
        // do stuff
        int [] path = ...
        output.add( path );

    Now you can just read the queue to get output, or decide not to read it. You can also dedicate one thread to output and let the main thread do other things. (This is what is typically done in a producer-consumer problem.) You can also produce output but not finish, and continue producing results and add them to the queue as you are able to calculate them. This works well for progressive type problems.

    This code wasn't tested, and sorry for using raw types, it's just a quick example.