Main thread is blocked when calling method from another thread

harbun :

I am trying to create a new thread with an custom object, and then call this custom objects method from the main thread. The idea is that the main thread can go on doing other stuff, while the custom object goes on working in the second thread:

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

        Multithreading mt = new Multithreading();
        mt.multiTest();
    }

    public void multiTest() {
        SomeObject someObject = new SomeObject();
        Thread thread = new Thread(someObject);
        thread.setDaemon(true);
        thread.start();
        someObject.startit();
        int i = 0;
        while (i < 3) { 
            try {

                System.out.println("this is the main thread being busy");

                Thread.sleep(3000);
                i += 1;
            } catch (InterruptedException e) {
            }

        }
    }

    class SomeObject implements Runnable {

        public void sayHello() {
            System.out.println("this is the second thread being busy");
        }

        public void startit() {
            int i = 0;
            while (i < 3) {
                try {
                    sayHello();

                    Thread.sleep(3000);
                    i += 1;
                } catch (InterruptedException e) {
                }
            }

        }

        @Override
        public void run() {
            // TODO Auto-generated method stub

        }
    }
}

The output is:

this is the second thread being busy
this is the second thread being busy
this is the second thread being busy
this is the main thread being busy
this is the main thread being busy
this is the main thread being busy

It should be more like this:

this is the second thread being busy
this is the main thread being busy
this is the second thread being busy
this is the main thread being busy
this is the second thread being busy
this is the main thread being busy

So the main Thread is blocked until the method is completed. Is the main thread waiting for completion of someObject.startit() in the second thread(Being void as return type, I would think that this would not be the case)? Or is it executed in the first thread, therefore blocking it?

I know that with the following code I could execute someObject.startit() in another thread, but it would be created from scratch every time, and I cant afford the thread creation overhead:

new Thread(() -> {someObject.startit();}).start(); 

How can one thread call methods from an object in another thread without blocking?

xingbin :

Is the main thread waiting for completion of someObject.startit() in the second thread(Being void as return type, I would think that this would not be the case)? Or is it executed in the first thread, therefore blocking it?

When you call someObject.startit() directly in multiTest, it is executed in the first calling thread.

It is very important to understant that Runnbale is not Thread, Runnbale is just normal object, except its run method will be executed when you create and start a new Thread with it, like this:

new Thread(new Runnable()).start;

So, actually, there is nothing to do with thread blocking in this case. You can move startit() to the run method, so it will be executed by the second thread:

@Override
public void run() {
    startit();
}

And, to avoid thread creation overhead, you can use a thread pool to execute it:

ExecutorService executorService = Executors.newCachedThreadPool(); // It should a singlton
// ExecutorService executorService = Executors.newFixedThreadPool(threadSize);
executorService.execute(() -> {
    someObject.startit();
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Activity starts with delay when calling startActivity from background thread and the main thread being blocked

From Dev

Calling a method in thread from another thread, python

From Dev

Why is GUI thread blocked from worker thread when calling waitForReadyRead?

From Dev

Calling a method on the main thread?

From Dev

Calling a method in another thread after a thread is finished

From Java

calling main thread from Runnable thread in java

From Dev

main thread blocked on SCNetworkReachabilityGetFlags

From Dev

Laravel Main Thread Blocked

From Dev

Exception in thread "main" when calling methods from switch in menu class

From Java

Calling a method from the class that created a thread, in the thread

From Dev

Calling a method in a running thread, from outside the thread

From Java

Running code in main thread from another thread

From Dev

Call function in main thread from another thread?

From Dev

Calling a method from inside a thread to another class in python pyqt

From Dev

What is the scope of "this" when calling a method from an individual thread?

From Dev

In debug, when the app is frozen, how to know where the main thread is blocked?

From Dev

QProgressDialog freezes when the main thread is apparently non blocked

From Dev

Passing UI Thread method to another thread for calling in C#

From Dev

What happens to main queue/main thread, when a dispatch_sync is issued from main thread targeting another dispatch queue?

From Java

calling another method from the main method in java

From Java

Calling @Transactional methods from another thread (Runnable)

From Dev

Calling an FnMut callback from another thread

From Dev

Signal is not calling slot from another thread

From Dev

Why Thread.Start method is blocked when CPU load is high?

From Java

Thread safe calling a TimerTask from within another Thread

From Dev

How to start a thread from one object's method by calling another method?

From Dev

Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

From Dev

calling method from a new thread C

From Dev

Calling java method from different thread in ndk

Related Related

  1. 1

    Activity starts with delay when calling startActivity from background thread and the main thread being blocked

  2. 2

    Calling a method in thread from another thread, python

  3. 3

    Why is GUI thread blocked from worker thread when calling waitForReadyRead?

  4. 4

    Calling a method on the main thread?

  5. 5

    Calling a method in another thread after a thread is finished

  6. 6

    calling main thread from Runnable thread in java

  7. 7

    main thread blocked on SCNetworkReachabilityGetFlags

  8. 8

    Laravel Main Thread Blocked

  9. 9

    Exception in thread "main" when calling methods from switch in menu class

  10. 10

    Calling a method from the class that created a thread, in the thread

  11. 11

    Calling a method in a running thread, from outside the thread

  12. 12

    Running code in main thread from another thread

  13. 13

    Call function in main thread from another thread?

  14. 14

    Calling a method from inside a thread to another class in python pyqt

  15. 15

    What is the scope of "this" when calling a method from an individual thread?

  16. 16

    In debug, when the app is frozen, how to know where the main thread is blocked?

  17. 17

    QProgressDialog freezes when the main thread is apparently non blocked

  18. 18

    Passing UI Thread method to another thread for calling in C#

  19. 19

    What happens to main queue/main thread, when a dispatch_sync is issued from main thread targeting another dispatch queue?

  20. 20

    calling another method from the main method in java

  21. 21

    Calling @Transactional methods from another thread (Runnable)

  22. 22

    Calling an FnMut callback from another thread

  23. 23

    Signal is not calling slot from another thread

  24. 24

    Why Thread.Start method is blocked when CPU load is high?

  25. 25

    Thread safe calling a TimerTask from within another Thread

  26. 26

    How to start a thread from one object's method by calling another method?

  27. 27

    Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

  28. 28

    calling method from a new thread C

  29. 29

    Calling java method from different thread in ndk

HotTag

Archive