-6

Well, I was wondering how java handles code reading and running, for example if I wrote:

static void doSomething(){
    doSomethingElse();
    doYetAnotherThing();
}

Will it wait for doSomethingElse() to complete before it runs doYetAnotherThing()? Or will it just run both?

I guess if it sets a variable, variable = setAVariable(); it will retrieve the variable before continuing, but if the method contains an infinite loop it would get stuck.

alvitawa
  • 394
  • 1
  • 4
  • 12
  • 7
    Yes it will wait for `doSomethingElse()` to complete before it runs `doYetAnotherThing()`. – Christian Tapia Oct 28 '13 at 20:05
  • Why don't you try it out for yourself, with a long-running `doSomethingElse` and a very fast `doYetAnotherThing` and see what happens? – Dawood ibn Kareem Oct 28 '13 at 20:09
  • Just out of interest, is there any asynchronous programming language that would run such statements concurrently / wait for future returns? – Thomas Jungblut Oct 28 '13 at 20:09
  • 2
    @ThomasJungblut [Scala](http://www.scala-lang.org/) has the concept of futures and promises built in to the language, and is based on the JVM. C# also now has the `async` and `await` keywords for asynchronous programming. I could see how some purely functional languages might have asynchronicity built even deeper into the language, but for imperative/procedural programming, you really need to point out what can be run asynchronously and what to wait on for the code to make any sense. – Tim S. Oct 28 '13 at 20:22
  • @TimS. Thanks, I wasn't aware that Scala allows it explicitly. The `async` keyword goes into the right direction. @UpAndAdam obviously you can construct something like that in every language, but I doubt its efficiency and readability. A better solution would need to formulate a graph of executions and synchronizations. Scala makes it easier, because the actor model is such a nice fit for async/messaging computations. – Thomas Jungblut Oct 28 '13 at 20:28
  • @ThomasJungblut Never said it would be pretty :-p Tends to be slightly nicer at higher level languages. I've actually had to synthesize my own actor/ action model to do just this on occasion. Had the 'users' write code in XML and I interpreted that into the target language.. – UpAndAdam Oct 28 '13 at 20:58

4 Answers4

1

Java will run your code sequentially unless u tell it otherwise (by creating threads.)

If you jave an infinite loop in function doSomthingElse() then doYetAnotherThing() will never execute and doSomething will never terminate.

public static void main(String[] args)
{
    doSomethingElse();
    doYetAnotherThing();
}

private static void doYetAnotherThing() {
    System.out.println("Hi Agn");

}

private static void doSomethingElse() {
    System.out.println("Hi");
    while(true) // Infinite Loop
    {

    }
}

This will print to output:

    Hi

But not: Hi Agn.

For making both functions run you need to remove the infinite loop in doSomethingElse().

UPDATE:

However if you cant do that and still want to run the code above, you can use threads:

Main Class: public class javaworking {
static MyThread t1, t2; Thread tc;

public static void main(String[] args)
{
    t1 = new MyThread(1);       
    Thread tc = new Thread(t1);
    tc.start();

    t2 = new MyThread(2);
    tc = new Thread(t2);
    tc.start();
}
}

Thread class that contains all your functions: public class MyThread implements Runnable {

int ch;

public MyThread(int choice)
{
    ch = choice;
}

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

    switch(ch)
    {
    case 1:
        doSomethingElse();          
        break;

    case 2:
        doYetAnotherThing();
        break;

    default:
        System.out.println("Illegal Choice");
        break;
    }

}

private static void doYetAnotherThing() {
    // TODO Auto-generated method stub
    System.out.println("Hi Agn");

}

private static void doSomethingElse() {
    // TODO Auto-generated method stub
    System.out.println("Hi");
    int i = 1;
    while(true)
    {
        System.out.println(i++);
    }
}
}

Please note: The code I provided is merely an example. I didn't do any error handling or follow the recommended standards. The code works and that's it.

Rahul Dabas
  • 726
  • 2
  • 15
  • 27
0

These are synchronous calls executing in one thread so they are executed one after the other, ie. first doSomethingElse(); then doYetAnotherThing();. If you wanted them to be executed concurrently you could put each in different threads, then the order would not be guaranteed.

Bizmarck
  • 2,663
  • 2
  • 33
  • 48
0

Logically the program will read top to bottom. And as a programmer that's all you really need to know. However, behind the scenes this may not necessarily be the case. But you're guaranteed the results as if they ran sequentially.

Sometimes your processor will run lines of code that should never even have been executed! This is because of something called branch prediction(which has a nice explanation on this answer, though not java the idea is demonstrated at a lower level).

Again, you can work under the assumption that everything in the same Thread, will execute in written order.

Community
  • 1
  • 1
Cruncher
  • 7,641
  • 1
  • 31
  • 65
0

One listing from the spec is here: http://docs.oracle.com/javase/specs/jls/se5.0/html/execution.html

The gist is that one function must return before the next one is called. I can't say what that means in your case without knowing what your functions are doing. They could return because they finished or because they forked/spawned off another process/thread/async action. There are more subtleties to this but I'm not getting into anything further than this since they over complicate and obfuscate the answer.

Based on the terminology you use, I would suggest starting with a tutorial. Java doesn't read your code. Java is a language. The compiler will 'read' and parse your code, and generate bytecode that will be executed by the JVM.

And yes, if you cause an infinite loop it's a problem and your program won't exit.

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46