android thread

线程内容:
概述:
创建、实现方式
线程管理
生命周期
线程状态

线程管理
线程通信
同步线程(多个同步、同步的粒度、特征)
异步线程(核心、机制)
子线程
线程池

注意事项
死锁
并发

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--);
			}
		}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值