I have a delay happening where my pushGreentText does not show up in my EditText until after the last brace of firstrollSP leaves scope. pushGreenText is a custom function on the UI thread I wrote that adds a text to my EditText in XML that overlays a SurfaceView. I would like to see the green text show up right away instead of waiting for the entire moveWholeAITurn sequence. Is there a way to accomplish that? I tried threading firstrollSP but there is still a delay and I thought about maybe threading pushGreenText but I'm not sure that is the answer yet.
Thank you...
static synchronized public void firstRollSP( ) throws InterruptedException
{
//Roll for Player and announce
mGame.mDice.setDie1( mGame.mDiceFirstRoll.getDie1( ) );
mGame.mDice.setRolled( true );
MainActivity.activity.pushTextGreen( Strings.get_First_roll_X_Die1( ) );
//Roll for two and announce
Thread one = new Thread( )
{
public void run( )
{
try
{
Thread.sleep( 2000 );
mGame.mDice.setDie2( mGame.mDiceFirstRoll.getDie2( ) );
while( mGame.mDice.getDie1( ) == mGame.mDice.getDie2( ) )
mGame.mDice.setDie2( (mRng.nextInt( 6 ) + 1) );
}
catch( InterruptedException e )
{
Log.d( "ACtionUP", "Interupted e" );
}
}
};
one.start( );
one.join( );
MainActivity.activity.pushTextGreen( "Android first roll is " + Integer.toString( mGame.mDice.getDie2() ) );
if( H.initWonFirstRoll( ) )
{
MainActivity.activity.pushTextGreen("Player won first roll.");
Thread tInitWon = new Thread( )
{
public void run( )
{
try
{
Thread.sleep( 2000 );
mGame.isFirstRoll = false;
mGame.isTurn = true;
mGameAI.isFirstRoll = false;
mGameAI.isTurn = false;
mGame.mDice.sort( );
mGame.mDice.setRolled( true );
mGame.mDice.setDiceAnimationComplete( true );
mGame.mOppDice.init( );
mGame.mPossibleIndexes.calcPossibleTriangles( );
}
catch( Exception e )
{
Log.d( "ACtionUP", "Interupted e" );
}
}
};
tInitWon.start( );
tInitWon.join( );
}
else
{
MainActivity.activity.pushTextGreen("Android won first roll.");
Thread tDroidWon = new Thread( )
{
public void run( )
{
try
{
Thread.sleep( 2000 );
mGame.isFirstRoll = false;
mGame.isTurn = false;
mGameAI.isFirstRoll = false;
mGameAI.isTurn = true;
mGameAI.mDice.init( );
mGame.mOppDice.init( );
mGame.mDice.sort( );
mGameAI.mDice.setDie1( mGame.mDice.getDie1() );
mGameAI.mDice.setDie2( mGame.mDice.getDie2() );
mGame.mOppDice.setDie1( mGame.mDice.getDie1() );
mGame.mOppDice.setDie2( mGame.mDice.getDie2() );
Thread.sleep( 2000 );
mGameAI.mPossibleIndexes.calcPossibleTrianglesAI( );
}
catch( InterruptedException e )
{
Log.d( "ACtionUP", "Interupted e" );
}
}
};
tDroidWon.start( );
tDroidWon.join( );
if( mGameAI.mPossibleIndexes.anyPossibles( ) )
{
moveWholeTurnAI( );
}
else
{
H.endTurnAI( );
}
}
}
Because the UI thread is an event loop. It will not update the screen until it returns to the main looper and handles the draw event- until whatever function of yours it calls exits.
You should never call thread.join on the main thread. Your app will freeze if you do, it may crash if it trips a watchdog timer. You need to refactor it to not require joins on the UI thread.
In addition, I see a lot of MainActivity.activity. That's a huge code smell- if you're holding the activity in a static variable you will have memory leaks and you are almost certain to have problems if your app is relaunched. Its something you should never do.