Thread doesn't come back from wait() when notify is called

Problems with Canvas, OpenGL, etc...

Thread doesn't come back from wait() when notify is called

Postby jumpoutofatree » Thu Jun 16, 2011 7:43 pm

So I have this code, I'm not the best at threads, but I thought I understood this much. For some reason this thread never leaves the wait() and resumes notifyAll() does nothing and doesn't bring back the view after the activity pauses/closes. I'm also kind of confused on what needs to be synchronized I see that people sometimes make generic Objects and sometimes people use other things. I've tried everything from making a generic object to using mSurfaceHolder and right now am using a Boolean object that runs the while loop. Any help greatly appreciated. **EDIT** To clarify the application doesn't crash when opened again after pausing/closing, but the view doesn't restore and is black. I've found a work around which includes running the thread constantly by making one while(true) loop and adding if(_run) and else if(!_run) but I know this isn't the most elegant solution and seems like the thread never closes. **EDIT**

Code: Select all
    class DrawThread extends  Thread{
       private SurfaceHolder mSurfaceHolder;
        public DrawThread(SurfaceHolder surfaceHolder){
            mSurfaceHolder = surfaceHolder;
        }
        public void onResume(){
           synchronized(_run){
              _run.notifyAll();
              _run = true;
           }
        }
        public void onPause(){
           synchronized(_run){
              _run = false;
           }
        }
        public void setRunning(boolean run) {
           synchronized(_run){
              _run = run;
           }
        }
        @Override
        public void run() {
          Canvas canvas = null;
           while (_run){
              System.out.println("RUNNING");
               if(isDrawing == true){
                  System.out.println("IS DRAWING");
                   try{
                       canvas = mSurfaceHolder.lockCanvas(null);
                       if(mBitmap == null){
                           mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                       }
                
                       final Canvas c = new Canvas (mBitmap);
                       c.drawColor(Color.WHITE);
                       canvas.drawColor(Color.WHITE);
                       c.drawBitmap(bBitmap, 0, 0,null);
                       //canvas.drawBitmap(bBitmap, 0, 0,null);
                       commandManager.executeAll(c,previewDoneHandler);
                       previewPath.draw(c);
                       canvas.drawBitmap (mBitmap, 0,  0,null);
                   } finally {
                       mSurfaceHolder.unlockCanvasAndPost(canvas);
                   }
               }
           }
           synchronized(_run){
              while(!_run){
                 try {
                    System.out.println("PAUSE");
                  _run.wait();
               } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
              }
           }   
       }
    }
jumpoutofatree
Junior Developer
Junior Developer
 
Posts: 17
Joined: Tue Mar 23, 2010 4:48 pm
Location: Gainesville, Florida

Top

Re: Thread doesn't come back from wait() when notify is call

Postby jumico » Sat Aug 20, 2011 10:53 am

I think it should be mSurfaceHolder.wait();

I know this is an old post.
jumico
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 05, 2011 9:15 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest