How can I simulate or write a code that would indicate that Java blocks a function till it has finished its execution. This way I will be able to show that Java has non-blocking I/O.
What I thought as my initial solution was to make an infinite loop but that didn't work as it will never finish its execution.
my other solution was to make a REST API and in that get request would delay and return something and think this might work but is there a native way to do it?
Here is the Java code below I want to delay the method fun2() without creating a new thread.
public class SetTimeOut {
public static void fun1(String str){
System.out.println(str);
}
public static void fun2(String str){
//how to make this function wait for 3 sec?
System.out.println(str);
}
public static void fun3(String str){
System.out.println(str);
}
public static void main(String[] args) {
fun1("Hello from fun1 is being called");
fun2("Hello from fun2 is being called");
fun3("Hello from fun3 is being called");
}
}
Here is an equivalent JavaScript code to show that JavaScript has a non-blocking I/O. Want to simulate a similar kind of behavior in Java.
console.log("Hey");
setTimeout(() => {
console.log("there!")
},3000);
console.log("please help");