0

In real time Java one can create a real time thread and run it via the following methods:

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

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

RealtimeThread rt3 = new RTThread();
rt3.start();

where RTThread is a class which extends RealtimeThread. But clearly the above approaches does not work when it comes to main. So is there a way to do it? My motivation for this is that I want only 2 real time threads to run. If I start two real time threads within main, won't there be a total of 3 threads?

wmjdgla
  • 101
  • 1
  • 8

3 Answers3

0

If I start two real time threads within main, won't there be a total of 3 threads?

No. If you start two threads, then return / "fall off the edge" of the main method, you'll have two threads running.

aioobe
  • 413,195
  • 112
  • 811
  • 826
0

if the RealtimeThreads are not deamon threads you can let the main thread finish and keep everything running inside the RealtimeThreads

public class BootStrap extends Runnable{

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

    public void run(){
    //...
    }
}
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
  • Tested, it works beautifully. I've also added a `sleep()`, then a `CyclicBarrier` before the actual code in `public void run()` to make sure main really dies and the other two threads start working at the same time. – wmjdgla Aug 03 '11 at 11:12
0

Aren't all threads RealTimeThreads in RTJ? including the main thread?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • That would be awesome, but I don't remember reading it anywhere. Also tested this with the following code: `Thread currT = Thread.currentThread(); if(currT instanceof RealtimeThread) System.out.println("Yeah!"); else if(currT instanceof Thread) System.out.println("Meh."); else System.out.println("WTF O.o");` It printed "Meh." – wmjdgla Aug 03 '11 at 10:34