Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop control for few seconds in java?
    primarykey
    data
    text
    <p>first if condition executes and then one method called and then i stop the control for 3 seconds ,then next if condition executes again one method called and stop control for 3 seconds likewise i executed 8 if conditions.But i get wrong output. What happened in my output is, sometimes method called before completion of control stop method.</p> <p>Here my coding and output. Give one solution</p> <pre><code> package com.example; import java.util.Timer; import java.util.TimerTask; public class TimeBetween { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SecondClass obj = new SecondClass(); int a = 5; int b = 3; int c; System.out.println("Timer Starts"); if (a == 5 &amp;&amp; b == 3) { c = a + b; System.out.println("-----------Addition:" + c); obj.secondClassMethod(); timeControlMethod(); } if (a == 5 &amp;&amp; b == 3) { c = a - b; System.out.println("-----------Subtraction:" + c); obj.secondClassMethod(); timeControlMethod(); } if (a == 5 &amp;&amp; b == 3) { c = a * b; System.out.println("------------Multipication:" + c); obj.secondClassMethod(); timeControlMethod(); } if (a == 5 &amp;&amp; b == 3) { c = a % b; System.out.println("-------------Modulo:" + c); obj.secondClassMethod(); timeControlMethod(); } if (a == 5 &amp;&amp; b == 3) { c = a + b; System.out.println("************Addition:" + c); obj.secondClassMethod(); timeControlMethod(); } if (a == 5 &amp;&amp; b == 3) { c = a - b; System.out.println("************Subtraction:" + c); obj.secondClassMethod(); timeControlMethod(); } if (a == 5 &amp;&amp; b == 3) { c = a * b; System.out.println("***************Multipication:" + c); obj.secondClassMethod(); timeControlMethod(); } if (a == 5 &amp;&amp; b == 3) { c = a % b; System.out.println("***************Modulo:" + c); obj.secondClassMethod(); timeControlMethod(); } System.out.println("Timer Ends"); } private static void timeControlMethod() { long getCurrentTimeInMilSec = System.currentTimeMillis(); long setEndTime = getCurrentTimeInMilSec + 3000l; System.out.println("Time method waiting for 3 sec........"); while (setEndTime &gt; System.currentTimeMillis()) { } } } class SecondClass { public void secondClassMethod() { System.err.println("Inside of SecondClass method"); } } </code></pre> <p>OUTPUT:</p> <pre><code> Timer Starts Inside of SecondClass method -----------Addition:8 Time method waiting for 3 sec........ -----------Subtraction:2 Time method waiting for 3 sec........ Inside of SecondClass method ------------Multipication:15 Inside of SecondClass method Time method waiting for 3 sec........ -------------Modulo:2 Time method waiting for 3 sec........ Inside of SecondClass method ************Addition:8 Time method waiting for 3 sec........ Inside of SecondClass method Inside of SecondClass method************Subtraction:2 Time method waiting for 3 sec........ ***************Multipication:15 Time method waiting for 3 sec........ Inside of SecondClass method ***************Modulo:2 Time method waiting for 3 sec........ Inside of SecondClass method Timer Ends </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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