简体   繁体   中英

what is best alternative of C++ Sleep() in java and android?

I am basically a C++ developer, but have moved to Android recently. My question is what is the the best alternative of C++(Windows) Sleep() function in java? I just need a simple delay in execution of program in android (java) and then resume it for each iteration of for loop. But When I use Thread.sleep() in java ,it cause some kind of hang to the android system and does not work as expected. Hope you understand my question and requirement. Please suggest a better way to get C++ Sleep() functionality in java.

 protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(5);
            paint.setStyle(Paint.Style.STROKE);
            Path path = new Path();
            path.moveTo(myPath[0].x, myPath[0].y);
            for (int i = 1; i < myPath.length; i++) {
                path.lineTo(myPath[i].x, myPath[i].y);               
                canvas.drawPath(path, paint);try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }

this is kind of my code. I want to see each Path line draw after 1 sec of delay so I can see the drawing. But what exactly it does is pause the App for 1 sec and then draw the complete at once.

If you want to force a pause in your app use Thread.sleep()

try {
    Thread.sleep(3000);  // 3 seconds (in milliseconds)
} catch(Exception ex) {       
}

That should not cause delay in your app... but yes a pause.
If I understand well, what you want to have possibility of wake up the process, for this use Object.wait() .

Object myObject = // this can be any object (activity) you have
synchronized (myObject) {
    myObject.wait();
} 

But will need more of your code and the situation you want to execute this in your app for giving more exact answer...

Your app hangs because you pause the main thread, which should never be paused. Run your code on a separate thread, for example:

Thread d = new Thread() {
  @Override
  public void run() { /* code, you can use Thread.sleep() here */ }
};
d.start();

The java.lang.Thread.sleep(long millis) method causes the currently executing thread to sleep for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

  try {
       // thread to sleep for 1000 milliseconds
       Thread.sleep(1000);
       } catch (Exception e) {
       System.out.println(e);
       }

If you want some process to run off the UI thread try this..

        int delay = 500;
        Timer t = new Timer();
        t.schedule(new TimerTask() {

            @Override
            public void run() {
                // your code
            }
        }, delay);

I don't know android, but the way to solve your problem in any GUI framework is pretty much the same.

You don't want to make drawing your object take time, because your app will be unresponsive while the drawing takes place. Also, the GUI framework may buffer the result of your drawing calls, so the rate at which new lines appear may not be the same as the rate at which you made the calls.

What you want to do is animate your object: Give your object a state variable that says how many lines have been drawn, and change your onDraw() method to draw that many lines. Then schedule a timer event the bumps the state, and triggers a re-draw at regular intervals.

You can also use a CountDownTimer and implement your own pausing class. It is thread safe and android-exclusive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM