thread和runnable

转载:http://blog.csdn.net/wssiqi/article/details/8158370

截取了自己需要的一部分,方便自己以后学习,也贴出了原网址,方便需要的人。

1.创建Android线程

参考官方文档是个好习惯。

http://developer.android.com/reference/java/lang/Thread.html

http://developer.android.com/guide/components/processes-and-threads.html

创建线程有两种方式:一是在创建线程的时候传入一个Runnable对象,另一种是继承Thread类,实现run()方法。这两种方法没啥区别嘛。。。

Game start....

(1)用Runnable对象

源码MainActivity.java

package com.example.siqi;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        /**
         * 线程1
         */
        new Thread(new Runnable(){
            @Override
            public void run() {
                int cnt = 10;
                while(cnt>0) {
                    Log.d("Thread1", "Thread one cnt: " + cnt--);
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        }).start();
        
        /**
         * 线程2
         */
        new Thread(new Runnable(){
            @Override
            public void run() {
                int cnt = 10;
                while(cnt>0) {
                    Log.d("Thread2", "Thread two cnt: " + cnt--);
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        }).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

 

 

从结果我们可以看出,这两个线程的确是并行运行的。
(2)定义一个Thread

 

MainActivity.java

 

package com.example.siqi;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    /**
     *定义一个简单的线程SimpleThread
     */
    public class SimpleThread extends Thread {
        @Override
        public void run() {
            int cnt = 10;
            while(cnt>0) {
                Log.d("Thread", "Thread cnt: " + cnt--);
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
        
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        SimpleThread thread1 = new SimpleThread();
        SimpleThread thread2 = new SimpleThread();
        thread1.start();
        thread2.start();
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

posted @ 2015-10-14 20:59  dreamOwn  阅读(186)  评论(0编辑  收藏  举报