3

this code i make.

public long detecFace(int threshold, int dir, JLabel... lbl) throws InterruptedException {

    long timeStart = Calendar.getInstance().getTimeInMillis();         
    BufferedImage[] finalImg = new BufferedImage[10];
    BufferedImage[] edgeImage = new BufferedImage[10];

    long t = System.currentTimeMillis();
    long end = t-300000;
    while (t < end){
        for (int i = 0; i < 5; i++) {
            ip.setFileImage(df.getFile()[i]);
            ip.procesImage(threshold, dir);
            edgeImage[i] = ip.getEdgeImage();
            //ImageDrawer.drawOriginalImage(edgeImage[i]); //BENER HASIL YANG KELUAR
            finalImg[i] = showSelectedFace(RHT(edgeImage[i]), ResearchData.getOriginalImage()[i]);
            //ImageDrawer.drawOriginalImage(finalImg[i]); //SALAH HASIL YANG KELUAR
            int count = 1;
            int total = count + i;
            System.out.println(total);

            if (finalImg[i] != null)
            {
                ui.drawerImage(finalImg[i], lbl[i]);
            } else {
                System.out.println("Failed!");
            }
        }
    Thread.sleep(end);
    }    
    ResearchData.setEdgeImage(edgeImage);
    ResearchData.setFinalImage(finalImg);

    long timeEnd = Calendar.getInstance().getTimeInMillis();
    long tt = timeEnd - timeStart;
    long second = tt/1000;
    long minute = second/60;
    long seconds = second%60;
    System.out.println("minute =" +minute+ "; second =" +seconds);     
    return tt;   
}`

I try to make loop using timeout but I stack with the result. there is no error but false for logical. I wanna make my program running for 3 or 5 minutes and as long as program running there is a looping again in there. help me to find best solution with my problem. thank you

3
  • welcome to SO. plz paste your code in the question Commented Oct 24, 2018 at 4:21
  • what is the purpose of the sleep ? Commented Oct 24, 2018 at 5:11
  • my aim is program will stop when the time is out, whether "for loop" done or not..but I'm not really sure for code I made. Commented Oct 24, 2018 at 5:24

2 Answers 2

7

try like this.

  long start = System.currentTimeMillis();
    long end = start + 300000;
    while(true) {
        //do your code
        //
        if(System.currentTimeMillis() > end) {
            break;
        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

try with less time, 300000 ms is equivalent to 5 minutes.
it's not working :'( the program stil running until looping for 5 times (loop for)
do you want to run inner for loop only once? then there is no need for while loop. remove while loop and after for loop just put Thread.sleep(300000)
yes. and program still running until for loop done. :'(
my aim is program will stop when time is out. whether for loop done or not.
1
long timeoutInMn = 3;
LocalDateTime startTime = LocalDateTime.now();
while (true && ChronoUnit.MINUTES.between(startTime, LocalDateTime.now()) <= timeoutInMn ) {

}

2 Comments

You can change MINUTES by the ChronoUnit you want of course
Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.