1

Edit, found a work around, code is posted at the bottom.

Is it possible to pause a thread that's in the middle of using thread.sleep(sleeptime)? And then when it's resumed, sleep for the remaining time?

I tried implementing the wait/notify set up here to pause a thread How to pause/resume thread in Android?

However, it looks like it waits until thread.sleep is finished with it's sleeping time.

The reason why I am asking this because I am using a while loop inside a thread to play music from a play list, and I need the thread to sleep for the duration of the music track so that the music track finishes before the while loop plays the next song on the playlist.

For example, the while loop will play a song that has a duration of 50 seconds, so the thread.sleep method will pause the thread that contains the while loop. But, the user wants to the pause the song, and the whole play list 25 seconds into the song. So the user will press the pause button 25 seconds into thread.sleep's 50 second sleep. After the user unpauses, I would like the thread.sleep to continue the rest of 50 second wait (25 seconds left at this point) to complete playing the rest of the song. Then the while loop will play the next track on the playlist.

Runnable whilePlay2 = new Runnable() {
                    @Override
                    public void run(){
                while (k <= myIntArray2.size()-1 ) {
                    System.gc();
                    if( myIntArray2.get(k)>count){
                        video_column_index = videocursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                        videocursor.moveToPosition(myIntArray2.get(k)-count);
                        filename2 = videocursor.getString(video_column_index);
                    }
                    else{
                    music_column_index = musiccursor
                            .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                    musiccursor.moveToPosition(myIntArray2.get(k));
                    filename2 = musiccursor.getString(music_column_index);
                    }

                                                try {
                                if (m2MediaPlayer.isPlaying()) {
                                    m2MediaPlayer.reset();
                                }
                                m2MediaPlayer.setDataSource(filename2); 
                                m2MediaPlayer.prepare();
                                m2MediaPlayer.start();      
                                Thread.sleep(m2MediaPlayer.getDuration());
                                synchronized(Lock){
                                    while(onS){
                                        Log.i("should be", "waiting");
                                        Lock.wait();
                                    }
                                    }
                                Log.i("what is on", "on is"+onS);
                                Log.i("k Check", "k is" +k);
                            } catch (Exception e) {
                            }
                    m2MediaPlayer.stop();
                    m2MediaPlayer.reset();
                    k++;
                }
            } //closes public void run(){
                        // TODO Auto-generated method stub
                    };

                Thread whilePlay2T = new Thread(whilePlay2);
                whilePlay2T.start();

And here is my pause button

public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {
        mMediaPlayer.pause();
        length=mMediaPlayer.getCurrentPosition();
        m2MediaPlayer.pause();
        length2=m2MediaPlayer.getCurrentPosition();
        synchronized (Lock){
            onS=true;
        }
    } else {
        mMediaPlayer.seekTo(length);
        mMediaPlayer.start();
        m2MediaPlayer.seekTo(length2);
        m2MediaPlayer.start();
        synchronized (Lock){
            onS=false;
            Lock.notifyAll();
        }
    }



}

EDIT

Looks like I can't use it. So now I am using a recursive function instead of a while loop to set up my play list. Here's what one of them looks like.

public void hiplaylist2(int t2) {
    k=t2;
    if(k <= myIntArray2.size()-1 ){
    System.gc();
    if( myIntArray2.get(k)>count){
        video_column_index = videocursor
                .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        videocursor.moveToPosition(myIntArray2.get(k)-count);
        filename2 = videocursor.getString(video_column_index);
    }
    else{
    music_column_index = musiccursor
            .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
    musiccursor.moveToPosition(myIntArray2.get(k));
    filename2 = musiccursor.getString(music_column_index);
    }

                                try {
                if (m2MediaPlayer.isPlaying()) {
                    m2MediaPlayer.reset();
                }
                m2MediaPlayer.setDataSource(filename2); 
                m2MediaPlayer.prepare();
                m2MediaPlayer.start();
                //Thread.sleep(mMediaPlayer.getDuration());
                //SystemClock.sleep(mMediaPlayer.getDuration());
                Log.i("k Check", "k is" + k);
            } catch (Exception e) {
            }
    }
    m2MediaPlayer.setOnCompletionListener(new OnCompletionListener()
    {
        public void onCompletion(MediaPlayer m2MediaPlayer){                            

    m2MediaPlayer.stop();
    m2MediaPlayer.reset();
    if(k < myIntArray2.size()-1 ){
    k++;
    hiplaylist2(k);
    }
    }
    });
};

And then I ues a thread to have more than one of these run at the same time.

Community
  • 1
  • 1
SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116
  • pausing or sleeping both are same.after pause thread will become alive and after sleep also thread will become alive. – Divya Mar 22 '14 at 19:13

2 Answers2

2

I don't really get it. I'm not sure about what you want to do, but i guess that there are at least two problems into your algorithm.

Firstly, when you work on concurrent tasks take care of your race conditions, your m2MediaPlayer seems to be one of them. Concerning your Thread.sleep(m2MediaPlayer.getDuration()); I believe that you shouldn't do it this way, if you want to wait until your song is finished you just have to :

  1. Launch your song from Thread 1
  2. Pause your Thread 1 (wait)
  3. The song is playing in Thread 2
  4. When the song is finished wake up (notify) your Thread 1 from Thread 2 (and let T2 die)

Note that you have to keep your code as simple as you can, because concurrency is hell.

Hosni
  • 668
  • 8
  • 29
Batkovic
  • 91
  • 3
0

Is it possible to pause a thread that's in the middle of using thread.sleep(sleeptime)? And then when it's resumed, sleep for the remaining time?

Assuming that you are asking about the behaviour of the Java runtime libraries, then the answer is No.

  • There is no safe way to pause and resume a thread. The Thread suspend / resume methods for doing this are unsafe and were deprecated in Java 1.1. (Read this if you want an explanation.)

  • Even if you use the unsafe methods, they do not magically pause a sleep timer. In fact, it is not clear what would happen to a sleeping thread: it is not specified.

    Indeed, I would not totally be surprised if you found that a sleeping thread whose sleep time expired while suspended never woke up ...


I'll leave it to others to deal with the rest of your question.

(I can't figure out from your description what it is you are trying to do here ... and why you even need to pause a sleeping thread. On the face of it, it sounds like you should be using wait / notify or a higher level synchronization class ... rather than sleeping. The fact that you apparently tried and failed does not make that the wrong solution.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216