可以在java中为一组线程定义执行顺序吗

可以在java中为一组线程定义执行顺序吗,java,multithreading,Java,Multithreading,我的理解是,理论上线程是并行执行的。决定;当资源可用时,从等待的线程队列中选择哪个线程(基于某些算法) 因此,我们不能为线程提供/强制执行序列 假设我的java应用程序有3个线程,t1、t2和t3 由于某种特定的原因;我希望线程按以下顺序执行: t3、t1和t2 有可能这样做吗?java提供了这样做的方法吗?您无法告诉线程调度程序执行线程的顺序。如果需要确保在线程a上运行的某段代码必须先于在线程B上运行的另一段代码运行,则必须使用锁或wait()/notify()强制执行该顺序 例如,您可以使用

我的理解是,理论上线程是并行执行的。决定;当资源可用时,从等待的线程队列中选择哪个线程(基于某些算法)

因此,我们不能为线程提供/强制执行序列

假设我的java应用程序有3个线程,t1、t2和t3

由于某种特定的原因;我希望线程按以下顺序执行: t3、t1和t2


有可能这样做吗?java提供了这样做的方法吗?

您无法告诉线程调度程序执行线程的顺序。如果需要确保在线程a上运行的某段代码必须先于在线程B上运行的另一段代码运行,则必须使用锁或
wait()
/
notify()
强制执行该顺序

例如,您可以使用两个线程都可以访问的变量作为“标志”,以指示线程B是否可以安全地继续执行。线程B可以在循环中
wait()
,检查该变量的值。然后,当线程B安全运行时,线程A可以设置变量并使用
notify()
唤醒线程B


所以,是的,在不同线程上发生的事情之间执行所需的顺序是可能的。不过,通常情况下,您希望避免编写此类低级、详细的代码。它太容易出错,导致微妙的、难以发现的错误。处理多线程代码时,如果可以,请始终尝试使用高级构建块。

您无法告诉线程调度程序执行线程的顺序。如果需要确保在线程a上运行的某段代码必须先于在线程B上运行的另一段代码运行,则必须使用锁或
wait()
/
notify()
强制执行该顺序

例如,您可以使用两个线程都可以访问的变量作为“标志”,以指示线程B是否可以安全地继续执行。线程B可以在循环中
wait()
,检查该变量的值。然后,当线程B安全运行时,线程A可以设置变量并使用
notify()
唤醒线程B

所以,是的,在不同线程上发生的事情之间执行所需的顺序是可能的。不过,通常情况下,您希望避免编写此类低级、详细的代码。它太容易出错,导致微妙的、难以发现的错误。处理多线程代码时,如果可能,请始终尝试使用高级构建块。

使用执行器:

executor.execute(runnable1);
wait();
executor.execute(runnable2);
wait();
executor.execute(runnable3);
wait();
当然,每个Runnable都必须以
notify()
语句结束。

使用执行器:

executor.execute(runnable1);
wait();
executor.execute(runnable2);
wait();
executor.execute(runnable3);
wait();

当然,每个Runnable都必须以一个
notify()
语句结束。

不要使用线程是一个简单的答案

如果您不想让代码无序运行,那么为什么要使用线程呢?像平常一样一步一步地执行


如果希望线程的某些部分按顺序运行,则使用标准的并发机制,如锁、等待/通知和信号量,但如果只希望整个操作按特定顺序运行,则…按顺序运行它们。没有线程。

不要使用线程,这是一个简单的答案

如果您不想让代码无序运行,那么为什么要使用线程呢?像平常一样一步一步地执行


如果希望线程的某些部分按顺序运行,则使用标准的并发机制,如锁、等待/通知和信号量,但如果只希望整个操作按特定顺序运行,则…按顺序运行它们。没有线程。

您可以在另一个线程上加入一个线程,以便在另一个线程完成时运行。

您可以在另一个线程上加入一个线程,以便在另一个线程完成时运行。

自java 8以来,这变得非常容易使用:


自java 8以来,这变得非常容易使用:

您可以设置线程的顺序。 我试图模拟你的情况:

public class ThreadJoinExample {
    private static final Thread FIRST = new Thread( new RunnableImpl(), "first" );
    private static final Thread SECOND = new Thread( new RunnableImpl(), "second" );
    public static void main(String[] args) {
        //here have started current thread or "main" thread that will control above threads
        FIRST.start();
        //waiting 2 seconds, "stop" your current thread and after current thread will start this "t3" thread until it will dead
        try {
            FIRST.join(2000);
        } catch (InterruptedException e) {
            System.out.println();
            e.printStackTrace();
        }
        SECOND.start();
        //"stop" your current thread immediately and run "t1" thread until it will dead.
        try {
            SECOND.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //Or we can wait for all threads and in the end - finish current main thread
        try {
            FIRST.join();
            SECOND.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Current thread is going die");
    }
}
class RunnableImpl implements Runnable{

    @Override
    public void run() {
        System.out.println("Started thread: "+Thread.currentThread().getName());
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread is going die: "+Thread.currentThread().getName());
    }
}

输出:

Started thread: first
Started thread: second
Thread is going die: first
Thread is going die: second
Current thread is going die
摘要:使用.join()方法,我们可以将当前线程移动到可运行状态,直到“已连接的线程”停止为止

您可以为线程设置顺序。 我试图模拟你的情况:

public class ThreadJoinExample {
    private static final Thread FIRST = new Thread( new RunnableImpl(), "first" );
    private static final Thread SECOND = new Thread( new RunnableImpl(), "second" );
    public static void main(String[] args) {
        //here have started current thread or "main" thread that will control above threads
        FIRST.start();
        //waiting 2 seconds, "stop" your current thread and after current thread will start this "t3" thread until it will dead
        try {
            FIRST.join(2000);
        } catch (InterruptedException e) {
            System.out.println();
            e.printStackTrace();
        }
        SECOND.start();
        //"stop" your current thread immediately and run "t1" thread until it will dead.
        try {
            SECOND.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //Or we can wait for all threads and in the end - finish current main thread
        try {
            FIRST.join();
            SECOND.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Current thread is going die");
    }
}
class RunnableImpl implements Runnable{

    @Override
    public void run() {
        System.out.println("Started thread: "+Thread.currentThread().getName());
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread is going die: "+Thread.currentThread().getName());
    }
}

输出:

Started thread: first
Started thread: second
Thread is going die: first
Thread is going die: second
Current thread is going die

摘要:使用.join()方法,我们可以将当前线程移动到可运行状态,直到“已连接线程”将停止为止

您可以编辑您的帖子来解释为什么要这样做吗?如果您想要顺序执行,那你为什么要使用线程呢?是的-最好的方法是把三个线程中的代码放到一个线程中。@Gary我现在所做的任何事情都没有实际的背景。我正在刷新我的理解,这个问题出现在我的脑海中。@Ayusman,不。没有办法扩展最后一个类,但是有了正确的同步和锁存,您可以按顺序执行线程。它看起来像:
ThreadA:work();锁b.倒计时()+
ThreadB:latchB.await();moreWork();挂锁c.倒计时()等等。但是使用多线程来模拟单线程行为有点。。。奇怪的所以这不是一个技术上的矛盾修饰法,只是太复杂了。你能编辑你的帖子来解释你为什么要这么做吗?如果你想要顺序执行,那你为什么要使用线程呢?是的-最好的方法是把三个线程中的代码放到一个线程中。@Gary我现在所做的任何事情都没有实际的背景。我正在刷新我的理解,这个问题出现在我的脑海中。@Ayusman,不。没有办法扩展最后一个类,但是有了正确的同步和锁存,您可以按顺序执行线程。它看起来像:
ThreadA:work();门闩。