1.概念:
Java是一种多线程编程语言,它包含两个或多个可以同时运行的部分,每个部分可以同时处理不同的任务,
特别是当您的计算机有多个CPU时,可以充分利用可用资源。
2.线程的生命周期:
创建、启动、运行、死亡
new - 新线程在新状态下开始其生命周期。它一直处于这种状态,直到程序启动线程。它也被称为天生的线程。
Runnable - 启动新生成的线程后,线程变为可运行。处于此状态的线程被视为正在执行其任务。
waiting - 有时,线程在等待另一个线程执行任务时转换到等待状态。仅当另一个线程通知等待线程继续执行时,线程才会转换回可运行状态。
Timer Waiting - 可运行的线程可以在指定的时间间隔内进入定时等待状态。当该时间间隔到期或者等待的事件发生时,处于此状态的线程将转换回可运行状态。
Terminated Dead: - 可运行线程在完成任务或以其他方式终止时进入终止状态
3.线程的两种启动方式:
new Thread 和 implement Runnable
3.1 implement 启动线程:
public class OneActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cabinet_type_1);
RunnableDemo r1 = new RunnableDemo("Thread-1");
r1.start();
RunnableDemo r2 = new RunnableDemo("Thread-2");
r2.start();
}
}
线程类:
package com.example.admin.ztest.thread;
import android.util.Log;
public class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
RunnableDemo(String name){
threadName = name;
Log.e("TAG","Creating =="+threadName);
}
@Override
public void run() {
Log.e("TAG","Running =="+threadName);
try {
for (int i=4;i>0;i--){
Log.e("TAG","Thread=="+threadName+","+i);
Thread.sleep(50);
}
} catch (InterruptedException e) {
Log.e("TAG","Thread " + threadName + " interrupted.");
}
Log.e("TAG","Thread " + threadName + " exiting.");
}
public void start(){
Log.e("TAG","Starting " + threadName);
if (t== null){
t = new Thread(this,threadName);
t.start();
}
}
}
运行结果:
05-16 17:20:52.159 9069-9069/com.example.admin.ztest E/TAG: Creating ==Thread-1
Starting Thread-1
Creating ==Thread-2
Starting Thread-2
05-16 17:20:52.160 9069-9100/com.example.admin.ztest E/TAG: Running ==Thread-1
Thread==Thread-1,4
05-16 17:20:52.160 9069-9101/com.example.admin.ztest E/TAG: Running ==Thread-2
Thread==Thread-2,4
05-16 17:20:52.210 9069-9100/com.example.admin.ztest E/TAG: Thread==Thread-1,3
05-16 17:20:52.210 9069-9101/com.example.admin.ztest E/TAG: Thread==Thread-2,3
05-16 17:20:52.260 9069-9100/com.example.admin.ztest E/TAG: Thread==Thread-1,2
05-16 17:20:52.260 9069-9101/com.example.admin.ztest E/TAG: Thread==Thread-2,2
05-16 17:20:52.311 9069-9100/com.example.admin.ztest E/TAG: Thread==Thread-1,1
05-16 17:20:52.311 9069-9101/com.example.admin.ztest E/TAG: Thread==Thread-2,1
05-16 17:20:52.361 9069-9100/com.example.admin.ztest E/TAG: Thread Thread-1 exiting.
05-16 17:20:52.362 9069-9101/com.example.admin.ztest E/TAG: Thread Thread-2 exiting.
第二种方式:extend Thread
public class OneActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cabinet_type_1);
ThreadDemo r1 = new ThreadDemo("Thread-1");
r1.start();
ThreadDemo r2 = new ThreadDemo("Thread-2");
r2.start();
}
}
public class ThreadDemo extends Thread {
private Thread t;
private String threadName;
ThreadDemo(String name) {
threadName = name;
Log.e("TAG", "Creating ==" + threadName);
}
@Override
public void run() {
Log.e("TAG", "Running ==" + threadName);
try {
for (int i = 4; i > 0; i--) {
Log.e("TAG", "Thread==" + threadName + "," + i);
Thread.sleep(50);
}
} catch (InterruptedException e) {
Log.e("TAG", "Thread " + threadName + " interrupted.");
}
Log.e("TAG", "Thread " + threadName + " exiting.");
}
public void start() {
Log.e("TAG", "Starting " + threadName);
if (t == null) {
t = new Thread(this, threadName);
t.start();
}
}
}
05-16 17:36:10.926 10528-10528/com.example.admin.ztest E/TAG: Creating ==Thread-1
Starting Thread-1
Creating ==Thread-2
Starting Thread-2
05-16 17:36:10.926 10528-10547/com.example.admin.ztest E/TAG: Running ==Thread-1
Thread==Thread-1,4
05-16 17:36:10.926 10528-10548/com.example.admin.ztest E/TAG: Running ==Thread-2
05-16 17:36:10.927 10528-10548/com.example.admin.ztest E/TAG: Thread==Thread-2,4
05-16 17:36:10.976 10528-10547/com.example.admin.ztest E/TAG: Thread==Thread-1,3
05-16 17:36:10.977 10528-10548/com.example.admin.ztest E/TAG: Thread==Thread-2,3
05-16 17:36:11.027 10528-10547/com.example.admin.ztest E/TAG: Thread==Thread-1,2
05-16 17:36:11.027 10528-10548/com.example.admin.ztest E/TAG: Thread==Thread-2,2
05-16 17:36:11.077 10528-10547/com.example.admin.ztest E/TAG: Thread==Thread-1,1
05-16 17:36:11.077 10528-10548/com.example.admin.ztest E/TAG: Thread==Thread-2,1
05-16 17:36:11.127 10528-10548/com.example.admin.ztest E/TAG: Thread Thread-2 exiting.
05-16 17:36:11.127 10528-10547/com.example.admin.ztest E/TAG: Thread Thread-1 exiting.
4.线程的多种方法的使用
public void suspend()此方法将线程置于挂起状态,并可以使用resume()方法恢复。
public void stop()此方法完全停止线程。
public void resume()此方法恢复使用suspend()方法挂起的线程。
public void wait()导致当前线程等待,直到另一个线程调用notify()。
public void notify()唤醒正在此对象监视器上等待的单个线程。
public class OneActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cabinet_type_1);
RunnableDemo r1 = new RunnableDemo("Thread-1");
r1.start();
RunnableDemo r2 = new RunnableDemo("Thread-2");
r2.start();
try {
Thread.sleep(1000);
r1.suspend();
Log.e("TAG", "Suspending First Thread");
Thread.sleep(1000);
r1.resume();
Log.e("TAG", "Resuming First Thread");
r2.suspend();
Log.e("TAG", "Suspending First Two");
Thread.sleep(1000);
r2.resume();
Log.e("TAG", "Resuming First Two");
} catch (InterruptedException e) {
Log.e("TAG", "Main thread Interrupted");
}
try {
Log.e("TAG", "Waiting for threads to finish");
r1.t.join();
r2.t.join();
} catch (InterruptedException e) {
Log.e("TAG", "Main thread Interrupted");
}
Log.e("TAG", "Main thread exiting.");
}
}
线程的方法:
package com.example.admin.ztest.thread;
import android.util.Log;
public class RunnableDemo implements Runnable {
public Thread t;
private String threadName;
private boolean suspended = false;
RunnableDemo(String name) {
threadName = name;
Log.e("TAG", "Creating ==" + threadName);
}
@Override
public void run() {
Log.e("TAG", "Running ==" + threadName);
try {
for (int i = 4; i > 0; i--) {
Log.e("TAG", "Thread==" + threadName + "," + i);
Thread.sleep(300);
synchronized (this) {
while (suspended) {
wait();
}
}
}
} catch (InterruptedException e) {
Log.e("TAG", "Thread " + threadName + " interrupted.");
}
Log.e("TAG", "Thread " + threadName + " exiting.");
}
public void start() {
Log.e("TAG", "Starting " + threadName);
if (t == null) {
t = new Thread(this, threadName);
t.start();
}
}
void suspend() {
suspended = true;
}
synchronized void resume() {
suspended = false;
notify();
}
}
日志记录:
05-16 17:59:19.623 26624-26624/? E/TAG: Creating ==Thread-1
Starting Thread-1
05-16 17:59:19.624 26624-26624/? E/TAG: Creating ==Thread-2
Starting Thread-2
05-16 17:59:19.624 26624-26653/? E/TAG: Running ==Thread-1
Thread==Thread-1,4
05-16 17:59:19.624 26624-26654/? E/TAG: Running ==Thread-2
Thread==Thread-2,4
05-16 17:59:19.924 26624-26653/? E/TAG: Thread==Thread-1,3
05-16 17:59:19.924 26624-26654/? E/TAG: Thread==Thread-2,3
05-16 17:59:20.224 26624-26653/? E/TAG: Thread==Thread-1,2
05-16 17:59:20.225 26624-26654/? E/TAG: Thread==Thread-2,2
05-16 17:59:20.525 26624-26653/com.example.admin.ztest E/TAG: Thread==Thread-1,1
05-16 17:59:20.525 26624-26654/com.example.admin.ztest E/TAG: Thread==Thread-2,1
05-16 17:59:20.624 26624-26624/com.example.admin.ztest E/TAG: Suspending First Thread
05-16 17:59:20.826 26624-26654/com.example.admin.ztest E/TAG: Thread Thread-2 exiting.
05-16 17:59:21.624 26624-26624/com.example.admin.ztest E/TAG: Resuming First Thread
Suspending First Two
05-16 17:59:21.625 26624-26653/com.example.admin.ztest E/TAG: Thread Thread-1 exiting.
05-16 17:59:22.625 26624-26624/com.example.admin.ztest E/TAG: Resuming First Two
Waiting for threads to finish
Main thread exiting.