Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run two threads at the same time in java

I am new to java and I am trying to learn about threads.

I am expecting an output of alternate hello this is thread one and hello this is thread two. but the output I get is as follows:

hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two

Below is my code. Can anyone please help me out to why I am getting this output as opposed to expected. And what is it that I can do to run the two threads in parallel.

public class ThreadDemo {
    public static void main(String args[]) {

        // This is the first block of code
        Thread thread = new Thread() {
            public void run() {
                for (int i = 0; i < 10; i += 2) {
                    System.out.println("hello this is thread one");
                }
            }
        };

        // This is the second block of code
        Thread threadTwo = new Thread() {
            public void run() {
                for (int i = 0; i < 10; i += 2) {
                    System.out.println("hello this is thread two");
                }
            }
        };

        // These two statements are in the main method and begin the two
        // threads.
        // This is the third block of code
        thread.start();

        // This is the fourth block of code
        threadTwo.start();
    }
}
like image 443
Prakhar Agrawal Avatar asked Dec 23 '15 11:12

Prakhar Agrawal


People also ask

Can we run two threads simultaneously in Java?

Multithreading in Java is a process of executing two or more threads simultaneously to maximum utilization of CPU. Multithreaded applications execute two or more threads run concurrently. Hence, it is also known as Concurrency in Java. Each thread runs parallel to each other.

How do you run multiple threads in Java?

In order to create a Thread, first we need to create an Instance of RunnableWorker which implements the Runnable Interface. The above code creates a Runnable Instance r. Then it create 3 threads t1, t2 and t3 and passes r as the argument to the 3 threads. Then the start() function is used to start all the 3 threads.

Can two threads run at the same time?

Within a process or program, we can run multiple threads concurrently to improve the performance. Threads, unlike heavyweight process, are lightweight and run inside a single process – they share the same address space, the resources allocated and the environment of that process.

How many threads can run simultaneously in Java?

On Windows machines, there's no limit specified for threads. Thus, we can create as many threads as we want, until our system runs out of available system memory.


1 Answers

Just because threads may interlace does not mean that they will. Your threads simply run too fast. Try adding Thread.sleep() to make them run longer.

like image 193
cadrian Avatar answered Oct 03 '22 05:10

cadrian