简体   繁体   English

单步执行多个线程

[英]Stepping through multiple threads

I have multiple threads that need to wait for each other to do a process. 我有多个线程需要互相等待才能完成一个进程。 It has to be done this way because of requirements. 由于需要,必须以这种方式进行。 If I show code and show expected output it will make sense. 如果我显示代码并显示预期的输出,这将是有意义的。 Here is my main class: 这是我的主要课程:

public static ThreadedJob job1, job2;
public static ArrayList<Thread> threads = new ArrayList<Thread>();
public static void main(String[] rags) throws InterruptedException{
    job1 = new Job1("Thread1:");
    job2 = new Job2("Thread2:");
    job1.run();
    job2.run();
    threads.add(new Thread(job1));
    threads.add(new Thread(job2));
    if (job1.getCounter() > job2.getCounter()) {
        threads.get(0).wait();
        threads.get(1).notify();
    }
    if (job1.getCounter() < job2.getCounter()) {
        threads.get(1).wait();
        System.out.println(true);
        threads.get(0).notify();
    }
}

And here is the threaded job class, (Job1 and Job2 class for now just extend and call super(name) that is all) 这是线程作业类,(现在,Job1和Job2类只是扩展并调用全部的super(name))

public class ThreadedJob implements Runnable{



protected String name;
protected int counter = 0;
public ThreadedJob(String name){
    this.name = name;
}

@Override
public void run() {

    System.out.println(name + " job 1 complete");
    counter++;
    System.out.println(name + " job 2 complete");
    counter++;
    System.out.println(name + " job 3 complete");
    counter++;


}

public int getCounter(){ return counter; }

}

Here is the expected output: 这是预期的输出:

Thread1: job 1 complete Thread2: job 1 complete Thread1: job 2 complete Thread2: job 2 complete Thread1: job 3 complete Thread2: job 3 complete

Here is the output from my code: 这是我的代码的输出:

Thread1: job 1 complete Thread1: job 2 complete Thread1: job 3 complete Thread2: job 1 complete Thread2: job 2 complete Thread2: job 3 complete

Question: How can I make it so the threads pause, allow to let go, then continue. 问题:如何使线程暂停,允许释放然后继续。 Pretty much, how can I get them to step and wait to produce the expected output. 几乎,我该如何让他们迈步并等待产生预期的输出。 Thanks! 谢谢!

Important Note: This needs to work for x amount of threads, I just am testing with 2. Also, the threads operate with different logic most of the time in the real application but aren't allowed to work at the same time. 重要说明:这需要工作于x个线程,我正在用2进行测试。此外,在实际应用程序中,线程大多数时候都使用不同的逻辑进行操作,但不允许同时工作。

Basically you want to read a bit about java multithreading, locking, and the java memory model. 基本上,您想阅读一些有关Java多线程,锁定和Java内存模型的信息。

They way you have written your code (each thread having its own counter, without any sync/locks when accessing it) ... basically means that many more things can happen than what you expect as potential output. 他们用这种方式来编写代码(每个线程都有自己的计数器,访问它时没有任何同步/锁定)...基本上意味着发生的事情可能比您期望的多得多。

Meaning: both threads start running, and will increase their counters completely independent of each other. 含义:两个线程都开始运行,并且将完全彼此独立地增加其计数器。

Well, to be precise: it would be like that if you would be actually using those two other threads (which you aren't as you are calling run on your Runnables , not your threads). 好吧,确切地说: 如果您实际上正在使用其他两个线程(您调用的不是在Runnables运行 ,而不是在线程上运行) ,那将是这样。

So: when you would be actually going with three threads, then what your third thread sees when querying those counters is completely unpredictable. 因此:当您实际上要使用三个线程时,查询这些计数器时您的第三个线程所看到的完全是不可预测的。 Your code is simply one huge race condition. 您的代码只是一个巨大的竞争条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM