Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:10:06+00:00 2026-06-10T05:10:06+00:00

I’m developing simple video recording app for Android 2.2+ and having trouble with getting

  • 0

I’m developing simple video recording app for Android 2.2+ and having trouble with getting Timer thread to work as expected. Code is below and so steps are:

  1. when user pres start recording button, recording starts and fightTimer.start() method is called.
  2. it calls timer.start() method starting to run the thread. timer is the thread object inside of fightTimer
  3. when user clicks stop button fightTimer method stop() is called. There I set a flag stopTimer=true and so that stops the thread method from running and calls timer.wait() method so the thread waits
  4. when user clicks start recording button then fightTimer.start() method is called again. It calls threads timer.start() as the timer has already started however it throws exception.
  5. I catch exception and call fightTimer.restart() method. This method set flag stopTimer I set in step 3 to true so now we have stopTimer=true. timer thread is still waiting inside of run() method
  6. then it calls timer.notify() to let timer know that it doesn’t need to wait anymore and can continue running
  7. Now I was expecting the timer thread start running again but for some reason at this point execution jumps to beginning of the same method that called notify() (restart()) and sets the flag stopTimer=false, and then tries to notify timer thread again. It throws runtime exception and that’s where it ends.

I assume that my understanding of whole synchronisation of threads is not correct so if anyone could point out where I screw up that would be great. The code for FightTimeris below and I don’t even get any output information in logCat. Like I said any help will be much appreciated as I don’t understand why is this happening and how to fix it.

    public class FightTimer extends SurfaceView implements Runnable, SurfaceHolder.Callback {

        public Thread timer;
        public SurfaceHolder surfaceHolder;
        public Canvas canvas;
        public int counter=0;
        public volatile boolean stopTimer=false;
        public volatile boolean pauseTimer=false;

        public FightTimer(Context context)
        {
            super(context);

            surfaceHolder=getHolder();

            // this is necessary to make surfaceview transparent
            surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
            setZOrderOnTop(true);

            surfaceHolder.addCallback(this);

            timer=new Thread(this);
        }


        public void start()
        {
            try
            {
                timer.start();
            }
            catch(IllegalThreadStateException ex)
            {
                reStart();
            }
        }

        public synchronized void reStart()
        {
// here the method is executed twice as I described in step 7
// after notify() it actually jumps back to stopTimer=false again and then exits the function. Then outside of this object I catch RuntimeException         
            stopTimer=false;
                    timer.notify(); 
        }

        public synchronized void pause()
        {
            pauseTimer=true;
        }

        public synchronized void resume()
        {
            pauseTimer=false;
            timer.notify(); 
        }

        public void stop()
        {
            stopTimer=true;
        }

        public void run() {

            TimeWatch timeWatch=TimeWatch.start();

            Paint paint=new Paint();

            paint.setColor(Color.RED);

            paint.setTypeface(Typeface.create("Arial",Typeface.NORMAL));
            paint.setTextSize(20);

            while(true)
            {
                // this is to pause timer

                try
                {
                    if(pauseTimer)
                    {
                        synchronized(timer) {
                            while(pauseTimer)
                                timer.wait();
                        }

                        // TODO heres the code should be executed when timer is resumed eg.
                        // maybe calculate how timer should count now as it wasn't counting for a while etc

                    }
                } catch(InterruptedException ex)
                {

                }

                // this is to pause timer

                try
                {
                    if(stopTimer)
                    {
                        synchronized(timer) {
                            while(stopTimer)
                                timer.wait();
                        }

                        // TODO heres the code should be executed when timer is restarted
                        // maybe reset timer completely etc

                        timeWatch.reset();
                    }
                } catch(InterruptedException ex)
                {

                }

                canvas=surfaceHolder.lockCanvas();

                // canvas might not exists at this point as we might be in activitis onStop() callback and stopping preview
                // etc. so we need to check if so then we exit run function

                if(canvas==null) return;

                //canvas.drawARGB(0,255,255,255);
                canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR); 

                long minutes=timeWatch.time(TimeUnit.SECONDS)/60;

                canvas.drawText(counter+"      "+minutes+":"+timeWatch.time(TimeUnit.SECONDS)%60,0,counter%60, paint);

                counter++;

                surfaceHolder.unlockCanvasAndPost(canvas);
            }

        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub

            //Toast.makeText(getContext(), "Surface Changed", Toast.LENGTH_LONG).show();

        }

        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub

            //timer.start();
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            // when surface is destroyed it means it cannot be displayed anymore and there is no canvas to draw
            // meaning the run() method cannot draw anything and calls to surfaceHolder will throw exception
            // so we need to stop thread here
            // this will happen when activity is in onStop() callback and when is already invisible and we are going to 
            // remove the object anyway so we don't care what will happenn later and make it wait. All we need is stop
            // run() from calling any other methods on canvas from surfaceHolder 


            Toast.makeText(getContext(), "Surface Destroyed", Toast.LENGTH_LONG).show();
        }

        public void setSurfaceHolder(SurfaceHolder surfaceHolder2) {
            // TODO Auto-generated method stub
            surfaceHolder=surfaceHolder2;
        }

    }

Please also see edited comment in restart() method. Below is call stack, when restart() method exited. Please let me know if any more information is needed.

DalvikVM[localhost:8754]    
    Thread [<1> main] (Suspended)   
        <VM does not provide monitor information>   
        MyFirstAppActivity.startRecording(View) line: 271   
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 521  
        View$1.onClick(View) line: 2077 
        Button(View).performClick() line: 2461  
        View$PerformClick.run() line: 8888  
        ViewRoot(Handler).handleCallback(Message) line: 587 
        ViewRoot(Handler).dispatchMessage(Message) line: 92 
        Looper.loop() line: 123 
        ActivityThread.main(String[]) line: 4627    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 521  
        ZygoteInit$MethodAndArgsCaller.run() line: 858  
        ZygoteInit.main(String[]) line: 616 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<7> Binder Thread #2] (Running) 
    Thread [<6> Binder Thread #1] (Running) 
    Thread [<8> Binder Thread #3] (Running) 
    Thread [<9> Thread-9] (Running) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:10:08+00:00Added an answer on June 10, 2026 at 5:10 am

    Methods reStart() and resume() must acquire monitor on the object timer before calling notify() on it. (Similar to how timer.wait() is being done in code above().)

    public synchronized void reStart()
    {
    // here the method is executed twice as I described in step 7
    // after notify() it actually jumps back to stopTimer=false again and then exits the function. Then outside of this object I catch RuntimeException 
        stopTimer=false;
        synchronized(timer) {
            timer.notify();
        }
    }
    
    public synchronized void resume()
    {
        pauseTimer=false;
        synchronized(timer) {
            timer.notify();
        }
    }
    

    Failure to do so results IllegalMonitorStateException on a JRE. And stacktrace posted points towards similar issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.