首页 技术 正文
技术 2022年11月6日
0 收藏 537 点赞 855 浏览 1006 个字

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;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919