'Java : Are sub threads lock the main thread?

I am new to Java, I have want to starts 02 thread to increase an attribute of an object and I want to print out the value of this attribute until it reach a certain value. I use 02 threads started inside increaseByThread() method.

I use two code snippets as follows but they behave differently. The first one I use while loop in the main thread to check for the value change but it only print out the last value after two sub-threads finish running and return 40.

The second one I use while loop but inside another sub-thread for checking value and it prints out every value, it means that 03 sub-threads are running in parallel (please see the second snippet below)

My question is that why in the first snippet, the while loop block only called after test.increaseByThread() finish execution?

public class ThreadIncrease {
    public volatile int[] count={0};
     public void increaseByThread(){
         Runnable first= () -> {
             for(int i=0;i<20;i++) {
                 count[0] = count[0] + 1;
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
//                 System.out.println(count[0]);
             }

         };

         Runnable second= () -> {
             for(int i=0;i<20;i++) {
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 count[0] = count[0] + 1;
//                 System.out.println(count[0]);
             }

         };
         Thread firstThread=new Thread(first);
         Thread secondThread=new Thread(second);
         firstThread.run();
         secondThread.run();
     }

    public static void main(String[] args) {
        ThreadIncrease test=new ThreadIncrease();
        Runnable check=()->{
            while(true){
                System.out.println(test.count[0]);
                if(test.count[0]<10){
                    System.out.println("count is: "+test.count[0]);
                }
                else{
                    System.out.println("Break");
                    break;
                }
            }
        };
//        Thread checkThread=new Thread(check);
//        checkThread.start();

        test.increaseByThread();
        while(true){
            System.out.println(test.count[0]);
            if(test.count[0]<10){
                System.out.println("count is: "+test.count[0]);
            }
            else{
                System.out.println("Break");
                break;
            }
        }

    }

}

The second one I use while loop but inside another sub-thread for checking value and it prints out every value, it means that 03 sub-threads are running in parallel:

    public class ThreadIncrease {
    public volatile int[] count={0};
     public void increaseByThread(){
         Runnable first= () -> {
             for(int i=0;i<20;i++) {
                 count[0] = count[0] + 1;
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
//                 System.out.println(count[0]);
             }

         };

         Runnable second= () -> {
             for(int i=0;i<20;i++) {
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 count[0] = count[0] + 1;
//                 System.out.println(count[0]);
             }

         };
         Thread firstThread=new Thread(first);
         Thread secondThread=new Thread(second);
         firstThread.run();
         secondThread.run();
     }

    public static void main(String[] args) {
        ThreadIncrease test=new ThreadIncrease();
        Runnable check=()->{
            while(true){
                System.out.println(test.count[0]);
                if(test.count[0]<10){
                    System.out.println("count is: "+test.count[0]);
                }
                else{
                    System.out.println("Break");
                    break;
                }
            }
        };
        Thread checkThread=new Thread(check);
        checkThread.start();

        test.increaseByThread();
//        while(true){
//            System.out.println(test.count[0]);
//            if(test.count[0]<10){
//                System.out.println("count is: "+test.count[0]);
//            }
//            else{
//                System.out.println("Break");
//                break;
//            }
//        }

    }

}


Solution 1:[1]

You have not started any new thread yet.

Each thread needs to run something. That is it's run method. But by invoking thread.run you just execute that code on the calling thread, which is your main thread.

Instead you need to start the new thread using thread.start(). This function will return immediately, and the newly created thread will execute run() in parallel.

Since you were running everything on the main thread the perception is right that the main thread was blocked until all the runs finished.

Solution 2:[2]

Thread.run(), which you are calling in increaseByThread() runs the Thread's Runnable in the current thread. I think you have confused it with Thread.start(), which starts a new thread to run the Runnable.

See What's the difference between Thread start() and Runnable run() and When would you call java's thread.run() instead of thread.start()?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hiran Chaudhuri
Solution 2