线程内容:
概述:
创建、实现方式
线程管理
生命周期
线程状态
线程管理
线程通信
同步线程(多个同步、同步的粒度、特征)
异步线程(核心、机制)
子线程
线程池
注意事项
死锁
并发
android中关联的类AsyncTask
Android消息处理系统——Looper、Handler、Thread
Android 线程同步 ConditionVariable的用法
概述:
创建、实现方式
线程管理
生命周期
线程状态
线程管理
线程通信
同步线程(多个同步、同步的粒度、特征)
异步线程(核心、机制)
子线程
线程池
注意事项
死锁
并发
android中关联的类AsyncTask
Android消息处理系统——Looper、Handler、Thread
Android 线程同步 ConditionVariable的用法
同步线程例子
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestThread testThread = new TestThread();
//确保多个线程操作的是同一个实例
new Thread(testThread, "testThreadName1").start();
new Thread(testThread, "testThreadName2").start();
new Thread(testThread, "testThreadName3").start();
new Thread(testThread, "testThreadName4").start();
new Thread(testThread, "testThreadName5").start();
new Thread(testThread, "testThreadName6").start();
}
/*
线程状态(待续)
线程休眠:sleep (待续)
线程优先级:priority (待续)
线程让步(调度):yield (待续)
后台线程:daemon (待续)
使用执行器:Executer (待续)
定义任务:Runnable (待续) todo
*/
class TestThread extends Thread {
private int tickets = 1000;
public void run() {
try {
for (int i = 0; i < 1000; i++) {
buyed();
Thread.sleep(10);//sleep thread 暂停同时不抢锁
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 锁住 同步
*/
private synchronized void buyed() {
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + " = " + tickets--);
}
}