简体   繁体   中英

Threads Execution

I have two threads t1 and t2. each having the code

getWindow().getDecorView().setBackgroundColor(Color.GREEN);

The problem is that the line doesn't works for Thread t2 and the main thread.

Here is the code:

package arj.developer.jaadu;

import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    MediaPlayer mp1,mp2;
    View v;
    Thread t1 = new Thread(){
        public void run(){
            getWindow().getDecorView().setBackgroundColor(Color.CYAN);          
            mp1=MediaPlayer.create(MainActivity.this,R.raw.jaad);
            mp1.start();
        }
    };
    Thread t2= new Thread(){
        public void run(){
            getWindow().getDecorView().setBackgroundColor(Color.MAGENTA);
            mp2=MediaPlayer.create(MainActivity.this,R.raw.jaad);
            mp1.stop();
            mp2.start();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setBackgroundColor(Color.GREEN);
        try {
            t1.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t1.start();
        try {
            t2.sleep(30000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t2.start();
    }
}

sleep() causes the CURRENT thread to sleep, so your t1.sleep(10000) and t2.sleep(10000) both caused the main thread to sleep! Move the sleep method to t1 & t2's run() method, and you probably should run getWindow().getDecorView().setBackgroundColor(_color_); in main thread as well.

package arj.developer.jaadu;

import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    MediaPlayer mp1,mp2;
    View v;
    Thread t1 = new Thread(){
        public void run(){
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            runOnUiThread(new Runnable() {

            @Override
            public void run() {
                                getWindow().getDecorView().setBackgroundColor(Color.CYAN);   
            }
        });       
            mp1=MediaPlayer.create(MainActivity.this,R.raw.jaad);
            mp1.start();
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            runOnUiThread(new Runnable() {

            @Override
            public void run() {
                                getWindow().getDecorView().setBackgroundColor(Color.MAGENTA);
            }
        });   
            mp2=MediaPlayer.create(MainActivity.this,R.raw.jaad);
            mp1.stop();
            mp2.start();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setBackgroundColor(Color.GREEN);
        t1.start();
    }
}

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