Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Java will run your code sequentially unless u tell it otherwise (by creating threads.)</p> <p>If you jave an infinite loop in function doSomthingElse() then doYetAnotherThing() will never execute and doSomething will never terminate.</p> <pre><code>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 { } } </code></pre> <p>This will print to output:</p> <pre><code> Hi </code></pre> <p>But not: Hi Agn.</p> <p>For making both functions run you need to remove the infinite loop in doSomethingElse().</p> <p>UPDATE: </p> <p>However if you cant do that and still want to run the code above, you can use threads:</p> <p>Main Class: public class javaworking {<br> static MyThread t1, t2; Thread tc;</p> <pre><code>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(); } } </code></pre> <p>Thread class that contains all your functions: public class MyThread implements Runnable {</p> <pre><code>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++); } } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload