Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Java Threads sequentially

How will you execute Three threads sequentially? For eg. Thread1, Thread2, Thread3. It is not possible to pass the reference of one Thread to the other and invoke from the run() method.

So code should be like this:

 Thread1.start();
 Thread2.start();
 Thread3.start();

and out put should be

 Printing Thread1
 Printing Thread2
 Printing Thread3

This can be possible by using ThreadPoolExecutor and using a blocking queue but even that is not an acceptable answer.

like image 577
Geek Avatar asked Mar 28 '11 15:03

Geek


People also ask

Do threads execute sequentially?

A thread is a single sequential flow of control within a program. Programs start, execute a series of instructions, and end. The execution sequence is a thread. Definition: A thread is a single sequential flow of control within a program.

How do you get threads to run in order?

You can run them all at once, but the important thing is to get their results in order when the threads finish their computation. Either Thread#join() them in the order in which you want to get their results, or just Thread#join() them all and then iterate through them to get their results. Save this answer.

How can we make threads to run one after another in sequence?

By using join you can ensure running of a thread one after another.

Do Java threads run in parallel?

Java Thread allows us to create a lightweight process that executes some tasks. We can create multiple threads in our program and start them. Java runtime will take care of creating machine-level instructions and work with OS to execute them in parallel.


2 Answers

Since this is an interview question, they're looking for specific knowledge, not a "well it's obviously better to do it this way" answer. It also seems that they'll likely strike out solution after solution until they get the answer they want.

Odds are they want to see if you can implement inter-thread communications yourself. But they don't want you to do it the easy way (thread references available). Otherwise, you could just do thread.join().

So have all three threads grab some bit of shared memory (synchronized static class). Have each thread check a public static int nextThread(). Upon successful comparison that they are the next thread, they should do their work and update public static setNextThread(int value) with the value of the next thread to be processed.

The key is to do this in a thread-safe manner; however, if you can guarantee unique thread identifiers and ensure that no two threads have the same identifier, you can (with careful coding) even manage to do this without synchronization.

like image 195
Edwin Buck Avatar answered Oct 05 '22 22:10

Edwin Buck


The simplest answer is

Thread1.run();
Thread2.run();
Thread3.run();

The problem with unrealistic questions is they often have an uninformative answer. ;)

The whole point of having threads is to run them concurrently. If you are not doing that at all, don't use threads.

You might say that; you cannot call the run() method, in which case you cannot use ThreadPoolExecutor because it calls the run() method for you. i.e. thats what submit() eventually does.

EDIT: The results are completely deterministic, becaus ethe fact that there is a Thread involved is irrelivent.

static class PrintThread extends Thread {
    public PrintThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++)
            System.out.println(getName() + ": " + i);
    }
}

public static void main(String args[]) {
    Thread thread1 = new PrintThread("A");
    Thread thread2 = new PrintThread("B");
    Thread thread3 = new PrintThread("C");

    thread1.run();
    thread2.run();
    thread3.run();
}

Prints

A: 0
A: 1
.. deleted ..
C: 98
C: 99

as expected.

like image 33
Peter Lawrey Avatar answered Oct 05 '22 22:10

Peter Lawrey