简体   繁体   中英

From single thread to multi thread android studio

I am making space invaders game. This is main acitivity:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // Get a Display object to access screen details
    Display display = getWindowManager().getDefaultDisplay();
    // Load the resolution into a Point object
    Point size = new Point();
    display.getSize(size);
    spaceInvadersEngine = new SpaceInvadersEngine(this, size.x, size.y);
    setContentView(spaceInvadersEngine);

}

    // This method executes when the player starts the game
    @Override
    protected void onResume() {
        super.onResume();

        // Tell the gameView resume method to execute
        spaceInvadersEngine.resume();
    }

    // This method executes when the player quits the game
    @Override
    protected void onPause() {
        super.onPause();

        // Tell the gameView pause method to execute
        spaceInvadersEngine.pause();
    }

Class SpaceInvadersEngine has two main functions update() where all calculation is done and draw() where I draw all elements. After adding more elements game works but it is slow, so I decided to separate it in more threads. This is my code in SpaceInvadersEngine

  public void resume() {
    playing = true;
    gameThread = new Thread(runnable);
    gameThread.start();

    drawThread = new Thread(this);
    drawThread.start();
}

In gameThread runnable I have

while(playing){update();}

and for drawThread in run() i have only draw();

While game load and prepare for new level (create new invader and upgrade objects) it takes up to 5 seconds and game freeze. How to remove that waiting time? When I try drawThread with runnable too, it does not draw anything.

Also, for some reason, when I do with two threads, my ship sometimes random blink as a big image in one frame, and then returns to normal, it was not blinking in single thread?

I would suggest you using Kotlin language with coroutines for asynchronous code. It's not hard to start using and it can really improve overall performance and code readability.

fun exampleMethod() {
    // Starts a new coroutine on Dispatchers.Main as it's the scope's default
    val job1 = scope.launch {
        // New coroutine with CoroutineName = "coroutine" (default)
    }

    // Starts a new coroutine on Dispatchers.Default
    val job2 = scope.launch(Dispatchers.Default + "BackgroundCoroutine") {
        // New coroutine with CoroutineName = "BackgroundCoroutine" (overridden)
    }
}

If you are open to change technology a bit, look at this and try coroutines. It's a fresh and great way how to deal with long-running tasks on Android. Also, you can find many articles and examples for them. Improve app performance with Kotlin coroutines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM