Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将主线程作为实时线程运行_Java_Real Time_Real Time Java - Fatal编程技术网

Java 如何将主线程作为实时线程运行

Java 如何将主线程作为实时线程运行,java,real-time,real-time-java,Java,Real Time,Real Time Java,在实时Java中,可以创建实时线程并通过以下方法运行它: RealtimeThread rt = new RealtimeThread(){ public void run(){ /*do work*/ } }; rt.start(); RealtimeThread rt2 = new RealtimeThread(); rt2.start(); RealtimeThread rt3 = new RTThread(); rt3.start(); 其中RTTh

在实时Java中,可以创建实时线程并通过以下方法运行它:

RealtimeThread rt = new RealtimeThread(){
    public void run(){
        /*do work*/
    }
};
rt.start();

RealtimeThread rt2 = new RealtimeThread();
rt2.start();

RealtimeThread rt3 = new RTThread();
rt3.start();
其中RTThread是一个扩展RealtimeThread的类。但显然,上述方法在涉及到主要问题时并不奏效。那么有没有办法做到这一点呢?我这样做的动机是,我只希望运行两个实时线程。如果我在main中启动两个实时线程,那么总共不会有3个线程吗

如果我在main中启动两个实时线程,那么总共不会有3个线程吗?


否。如果启动两个线程,然后返回main方法的/“fall off the edge”,则将有两个线程在运行。

如果
RealtimeThread
s不是deamon线程,则可以让主线程完成,并保持所有线程在
RealtimeThread
s内运行

public class BootStrap extends Runnable{

    public static void main(String[] args){
        new RealtimeThread(new BootStrap()).start();
        //main finishes running and stops
    }

    public void run(){
    //...
    }
}

不是所有线程都是RTJ中的RealTimeThreads吗?包括主线?

那太棒了,但我不记得在哪里读过。还使用以下代码对此进行了测试:
Thread currT=Thread.currentThread();if(RealtimeThread的currT instanceof)System.out.println(“耶!”);else if(线程的当前实例)System.out.println(“Meh”);else System.out.println(“WTF O.O”)它打印“Meh”。经过测试,它工作得非常漂亮。我还添加了一个
sleep()
,然后在
public void run()
中的实际代码之前添加了一个
CyclicBarrier
,以确保main真正消亡,并且其他两个线程同时开始工作。