不同的线程在执行不同的任务时不能一起工作

时间:2017-11-17 01:11:00

标签: java multithreading

我正在创建两个不同的线程,它们将以不同的方式解决相同的问题,我将计算他们的解决时间。我已经创建了这样的结构;

MainController.java

public static void main(String[] args) throws Exception {
        MainController mc= new MainController();
        mc.setProblem();
        Thread1 thread1 = new Thread1("thread1",problem,approach1);
        Thread2 thread2 = new Thread2("thread2",problem,approach2);
        thread1.start();
        thread2.start();
    }

Thread1.java

class Thread1 implements Runnable{

       public void run() {
          System.out.println("Running " +  threadName );
          try {
             solve(approach);
          } catch (Exception e) {
             System.out.println("Thread " +  threadName + " interrupted.");
          }
          //showTable();
          System.out.println("Thread " +  threadName + " exiting.");
       }

       public void start () {
          System.out.println("Starting " +  threadName );
          if (t == null) {
             t = new Thread (this, threadName);
             t.start ();
          }
       }

       public void solve(approach) throws Exception {

            // Throw an exception to stop the process if the puzzle is solved
            if (didIsolve())  {//true if yes
                    System.out.println("----" + threadName +"----");
                    showResult();
                    System.out.println("--------------------------");
                    throw new Exception("ex");
            }
        }
    }

Thread2.java与上面的代码块相同。

输出如

    Creating thread1
    Creating thread2
    Starting thread1
    Starting thread2
    Running thread2
    Running thread1
    Thread thread1 exiting.
    ----thread2----
   RESULT METHOD OUTPUT
    --------------------------
    Thread thread2 interrupted.
    Thread thread2 exiting.

如上所述,thread1没有解决问题,至少没有显示结果。

到目前为止,我已经检查了这个主题

multiple threads performing different tasks

Java threads to do different tasks?

编辑:

嗯,我想我发现了这个问题。我改变了MainController类;

public static void main(String[] args) throws Exception {
        MainController mc= new MainController();
        mc.setProblem();
        MainController mc2 = new MainContoller();
        mc1.setProblem();
        Thread1 thread1 = new Thread1("thread1",mc.problem,approach1);
        Thread2 thread2 = new Thread2("thread2",mc1.problem,approach2);
        thread1.start();
        thread2.start();
    }

它有效,但我不知道为什么。几天之内我不会接受我的答案作为正确的答案,有人可以解释这种情况吗?

2 个答案:

答案 0 :(得分:0)

如果您的目标是衡量每种方法的解决时间,那么以多线程方式执行此操作并不能为您提供一致且可靠的结果。最好以连续的方式运行每种方法,并且可能多次,以获得每种方法的平均完成时间。在测试期间,请注意避免使用其他进程对CPU征税。

答案 1 :(得分:0)

嗯,我想我发现了这个问题。我改变了MainController类;

public static void main(String[] args) throws Exception {
        MainController mc= new MainController();
        mc.setProblem();
        MainController mc2 = new MainContoller();
        mc1.setProblem();
        Thread1 thread1 = new Thread1("thread1",mc.problem,approach1);
        Thread2 thread2 = new Thread2("thread2",mc1.problem,approach2);
        thread1.start();
        thread2.start();
    }

它有效,但我不知道为什么。几天之内我不会接受我的答案作为正确的答案,有人可以解释这种情况吗?

相关问题