0

I have a set of roughly 20 threads and i want to schedule them so they run in a set order. Is there a way to do this. I have tried using priority and setting the priority 1-10 but the scheduler still seems to execute threads at its own order. Btw im working in Java

Is there a way to run threads in a set order ?

Thanks regards Mike

Mike Howard
  • 53
  • 1
  • 8
  • do you mean you want to run them sequentially (one after the other)? – assylias Apr 27 '12 at 14:06
  • yeah thats it. sorry about confusion – Mike Howard Apr 27 '12 at 14:07
  • 6
    In that case, why do you want separate threads at all? It sounds like you want one thread doing 20 things, one at a time... – Jon Skeet Apr 27 '12 at 14:08
  • 2
    Not sure why you'd want to spawn 20 threads for that. You can simply use an `ExecutorService` with single thread and submit jobs to it. EDIT: Doh, beaten by Jon. – Sanjay T. Sharma Apr 27 '12 at 14:09
  • i realise i cannot achieve true real time with standard java (and my operating system) but i am trying to achieve a structure that would accommodate real-time with requires concurrency for time based executions. Is my way of thinking wrong? Basically i am trying to achieve hard real time therefore a deterministic approach is required which to me says sequential fixed execution? – Mike Howard Apr 27 '12 at 14:21
  • I have no clue what you mean by "true real time", "time based executions", "hard real time" or "deterministic approach". – toto2 Apr 27 '12 at 14:37
  • Your OS can download video at high speed, stream MTV to your screen, run a browser with 30 tabs, have 20 documents and 10 spreadsheets open, run a VM with a couple Windows XP and Ubuntu running. It can do all that at the same time. It cannot, however, do much deterministic anything re. scheduling threads. Threads waiting on IO become ready in a near-chaotic order. In general, you will not know what your OS is running at any particular time, or what it will running in 50ms. – Martin James Apr 27 '12 at 15:10
  • maybe you could give an example of what it is you would like to do? – Martin James Apr 27 '12 at 15:11
  • See also: http://stackoverflow.com/questions/10362953/forcing-a-context-switch-from-the-userland-on-linux – andersoj Apr 28 '12 at 15:10

4 Answers4

2

What you need is an ExecutorService that will run your threads one at a time, namely : newSingleThreadExecutor.

 ExecutorService pool = Executors.newSingleThreadExecutor(); 
 pool.submit(job1);
 pool.submit(job2);
 pool.submit(job3);
Snicolas
  • 37,840
  • 15
  • 114
  • 173
1

Why do you have multiple threads if you want synchronous behaviour in the first place?

If you've acquired multiple Thread objects from "something else" then you can use thread.run() to execute them in the current thread, which will, of course allow you to control the order.

Clive Evans
  • 658
  • 8
  • 12
1

You don't have to run a single threaded version if the jobs can be executed in parallel. Below is an example where you can use eight threads to run your 20 jobs:

public static void main(String[] args) {
    final ExecutorService executorService = Executors.newFixedThreadPool(8);
    final Queue<Integer> workItems = new ConcurrentLinkedQueue<Integer>();
    for (int i = 0; i < 20; i++) {
        workItems.add(i);
    }
    for (int i = 0; i < 20; i++) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                final Integer workIem = workItems.poll();
                // process work item
            }
        });
    }
    // await termination of the exec service using shutdown() and awaitTermination()

}

The idea is that you use an auxiliary queue to maintain the items to be processed and rely on the FIFO ordering of the queue to process the items in order and in parallel.

shams
  • 3,460
  • 24
  • 24
  • This will not work as expected, please refer to : http://stackoverflow.com/questions/42153013/why-tasks-in-threadpool-are-not-executed-following-fifo-java?noredirect=1#comment71472912_42153013 – JaskeyLam Feb 10 '17 at 07:32
0

If the threads depend on each other, then one option would be to schedule only the first thread and have it spawn its dependent threads, which can then turn their dependent threads, etc...

You need to understand, however, that even though you may be launching threads in a particular order, as soon as they start they are off of your hands and they will be fighting for resources and the OS will time-slice their executions, which means some may get "ahead" of threads that were launched before. So if you truly need to keep the order, then I would suggest you use only one thread and let it orchestrate the tasks in a synchronized manner.

mprivat
  • 21,582
  • 4
  • 54
  • 64
  • i realise i cannot achieve true real time with standard java (and my operating system) but i am trying to achieve a structure that would accommodate real-time with requires concurrency for time based executions. Is my way of thinking wrong? Basically i am trying to achieve hard real time therefore a deterministic approach is required which to me says sequential fixed execution? – Mike Howard Apr 27 '12 at 14:40