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 8846595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:01:41+00:00 2026-06-14T12:01:41+00:00

I have GraphicView which extends SurfaceView. I need it for drawing a graphics. I

  • 0

I have GraphicView which extends SurfaceView. I need it for drawing a graphics. I need also onTouchEvent. But problem is… I don’t know how to describe it 🙂 here’s my code:

public class GraphicView extends SurfaceView implements Runnable {

    private boolean mutexOpened = true;

    public boolean onTouchEvent( MotionEvent me ) {

        if ( mutexOpened ) {

            mutexOpened = false;

            Log.d( "mutex", "ACTION 1" );

            switch ( action ) {

                case MotionEvent.ACTION_DOWN: {

                    int rnd = new Random().nextInt( 40000 ) + 1000;
                    for ( int i = 0; i < rnd; i++ ) {} // it's some long action :)

                    Log.d( "mutex", "ACTION 2: down" );
                    break;

                }

            }

            Log.d( "mutex", "ACTION 2: end" );
            mutexOpened = true;

        }

    }

    public void run() {

        while ( true ) {

            if ( mutexOpened ) {        
                Log.d( "mutex", "!!! RUN !!!!" );
            }

        }

    }

}

I use the mutex technique which (I suppose) has to control my threads. But in the Log I see following:

!!! RUN !!!!
ACTION 1
!!! RUN !!!!
ACTION 2: down
ACTION 2: end

But why?? Why the second “!!! RUN !!!!” runs between “ACTION 1” and “ACTION 2” when the mutex is closed? It’s impossible! :)))

I tried to do next:

public void run() {
    while ( true ) {
        if ( mutexOpened ) {
            mutexOpened = false; // close mutex
            Log.d( "mutex", "!!! RUN !!!!" );
            mutexOpened = true; // open mutex
        }
    }
}

but… FAIL :)) onTouchEvent never runs at all :D))) Does anybody know how to solve this problem?

  • 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-14T12:01:43+00:00Added an answer on June 14, 2026 at 12:01 pm

    You have to synchronize the access to mutexOpened: run() may be reading mutexOpened immediately before you set it to false and may be printing RUN!!! immediately after you print “ACTION 1”.

    Use the Java keyword synchronized to synchronize the access to mutexOpened. On run() you can use wait(), which releases the lock during the sleep phase.

    And if you use the synchronized keyword, you don’t need the variable mutexOpened at all.

    public class GraphicView extends SurfaceView implements Runnable {
    
        synchronized public boolean onTouchEvent( MotionEvent me ) {
    
    
            Log.d( "mutex", "ACTION 1" );
    
            switch ( action ) {
    
                case MotionEvent.ACTION_DOWN: {
    
                    int rnd = new Random().nextInt( 40000 ) + 1000;
                    for ( int i = 0; i < rnd; i++ ) {} // it's some long action :)
    
                    Log.d( "mutex", "ACTION 2: down" );
                    break;
    
                }
    
            }
    
            Log.d( "mutex", "ACTION 2: end" );
    
        }
    
    
        synchronized public void run() {
    
            while ( true ) {
                wait(100); // Wait 100ms and release the lock
                Log.d( "mutex", "!!! RUN !!!!" );
    
            }
    
        }
    
    }
    

    Also, try to don’t execute long operations in the GUI thread (that comment “it’s some long action”): that should be executed in a separate thread

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

Sidebar

Related Questions

I have a problem with following simple code. I create GraphicView class which extends
I have a problem making an inner class that extends from JPanel to draw
I have a graphicalview element which updates in realtime. I know from the beginning
I know a lot of people have had similar issues to this but I
I know, I am not the first onbe with this problem, but I tried
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
have a nice day. I got problem when trying to create an image from

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.