Android 多线程编程

Android 多线程编程

 //1.多线程
进程:操作系统的多道程序
线程:同一个程序的多条路径 //2.创建多线程程序
创建一个类extends Thread
重写run方法
在main方法中创建对象
对象.start() 开启线程 //3.多线程的特殊语法
在main方法中直接开启多线程
方法1
new Thread(){
public void run() {
for(int i=;i<;i++){
System.out.println("运行了"+i);
} }; }.start(); 方法2
在属性上创建多线程对象
private static Thread t1 = new Thread(){ //属性
public void run() {
for(int i=;i<;i++){
System.out.println("线程t1");
}
};
} ; //4.线程的睡眠
Thread.sleep(毫秒) //5.实现runnable接口处理多线程
写一个类实现Runnable,并重写run方法
在main方法中创建Thread对象开启多线程 匿名内部类的写法
new Thread(new Runnable() {
public void run() {
for(int i=;i<;i++){
System.out.println("====");
try {
Thread.sleep();
} catch (Exception e) { }
}
}
}).start(); //6.Service组件
Activity Ui界面
Intent
Service没有界面 后台的操作 创建一个类extends Service
重写Service 的 oncreate方法
ondestory方法
注册Service
<service android:name="com.example.andservice01.MyService"></service> 点击按钮 通过Intent启动service
//启动service
Intent intent = new Intent();
intent.setClass(MainActivity.this, MyService.class);
startService(intent); 停止Service
//销毁service
Intent intent2 = new Intent();
intent2.setClass(MainActivity.this, MyService.class);
stopService(intent2);
break;
上一篇:oracle drop table(表)数据恢复方法


下一篇:【Spring源码解读】bean标签中的属性