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?
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.
Comments