Search code examples
javamultithreadingrunnablegreen-threads

Combining two Runnable objects


Say for example that I have a Runnable called RunnableA that does something. I also have a Runnable called RunnableB that does something else. Is there a way that I can combine these two Runnables someway so that they will run in the same thread?

The second part of the question is if this is possible, can I then specify the order that they will run in?

EDIT!: The reason why I wanted to do this was because I need to run code on the EDT but some of the other code needs to be run on another thread. Please take a look at the code below.

Something like this


public final class CompoundRunnable implements Runnable
{
    private final Iterable runnables;

    public CompoundRunnable(Iterable runnables)
    {
        // From Guava. Easy enough to do by hand if necessary
        this.runnables = Lists.newArrayList(runnables);

    }

    public CompoundRunnable(Runnable... runnables)
    {
        this(Arrays.asList(runnables));
    }

    @Override
    public void run()
    {
        for (Runnable runnable : runnables)
        {
             runnable.run();
        }
    }
}


public void setStatusAndProgress(final String status,Runnable runnable)
    {
        Runnable startUpRunner = new Runnable()
        {
            public void run()
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        setStatus(status);
                        selfReference.getProgressBar().setIndeterminate(true);
                    }

                });
            }
        };
        Runnable cleanUpRunner = new Runnable()
        {
            public void run()
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        setStatus("");
                        getProgressBar().setIndeterminate(false);
                    }
                });
            }
        };

        Runnable theRunner = new CompoundRunnable(startUpRunner,runnable,cleanUpRunner);
        new Thread(theRunner).start();
    }

Sorry if this isnt explained well, post comments if you need clarification.

Thanks!


Solution

  • Well you can certainly create a Runnable which just runs one runnable then the other:

    public final class CompoundRunnable implements Runnable
    {
        private final Runnable first;
        private final Runnable second;
    
        public CompoundRunnable(Runnable first, Runnable second)
        {
            this.first = first;
            this.second = second;
        }
    
        @Override
        public void run()
        {
            first.run();
            second.run();
        }
    }
    

    More generally, you could make it take an Iterable<Runnable>, copy all the Runnable references, and then run them in order. For example:

    public final class CompoundRunnable implements Runnable
    {
        private final Iterable<Runnable> runnables;
    
        public CompoundRunnable(Iterable<Runnable> runnables)
        {
            // From Guava. Easy enough to do by hand if necessary
            this.runnables = Lists.newArrayList(runnables);
        }
    
        public CompoundRunnable(Runnable... runnables)
        {
            this(Arrays.asList(runnables));
        }
    
        @Override
        public void run()
        {
            for (Runnable runnable : runnables)
            {
                 runnable.run();
            }
        }
    }