android---多线程(启动,暂停,终止)
2016-04-27 19:48
423 查看
package com.example.main;
import android.util.Log;
public class TaskInfo extends Thread{
//任务的进度
private int TaskStep = 0;
//判断线程是否暂停
private boolean isPause;
//判断线程是否终止
private boolean isClose;
public TaskInfo()
{
initThread();
}
//初始化线程
public void initThread()
{
}
@Override
public void run() {
//线程没有关闭
while(!isClose)
{
//线程没有中断
if(!isPause)
{
sleep(5000);
}catch(Exception e)
{
e.printStackTrace();
}
}
else
{
onThreadWait();//线程暂停
}
}
}
//线程唤醒
public synchronized void onResume(){
this.isPause = false;
this.notify();
}
//线程暂停
public synchronized void onPause()
{
this.isPause = true;
}
//关闭线程
public synchronized void onClose(){
try{
this.notify();
isClose = true;
interrupt();//中断
}catch(Exception e)
{
e.printStackTrace();
}
}
//wait方法执行等待、
private synchronized void onThreadWait()
{
try{
synchronized (this) {
this.wait();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
import android.util.Log;
public class TaskInfo extends Thread{
//任务的进度
private int TaskStep = 0;
//判断线程是否暂停
private boolean isPause;
//判断线程是否终止
private boolean isClose;
public TaskInfo()
{
initThread();
}
//初始化线程
public void initThread()
{
}
@Override
public void run() {
//线程没有关闭
while(!isClose)
{
//线程没有中断
if(!isPause)
{
sleep(5000);
}catch(Exception e)
{
e.printStackTrace();
}
}
else
{
onThreadWait();//线程暂停
}
}
}
//线程唤醒
public synchronized void onResume(){
this.isPause = false;
this.notify();
}
//线程暂停
public synchronized void onPause()
{
this.isPause = true;
}
//关闭线程
public synchronized void onClose(){
try{
this.notify();
isClose = true;
interrupt();//中断
}catch(Exception e)
{
e.printStackTrace();
}
}
//wait方法执行等待、
private synchronized void onThreadWait()
{
try{
synchronized (this) {
this.wait();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
相关文章推荐
- android---控件隐藏与显示动画
- android---图片获得与裁剪
- android---仿淘宝下单PopWindow
- Android 图片滤镜工具——高斯模糊
- android---下拉ListView(顺畅版)
- android---gesture手势识别类
- Android Wallpaper分析
- Android RadioButton图片和图片尺寸
- android---朋友圈评论功能自定义对话框
- android---Activity仿Dialog
- Android中EditText的setError文字不显示的问题
- Android之TelephonyManager&GsmCellLocation类的方法详解
- android meta-data
- Android获取剪贴板内容
- Android调用系统分享
- Android Studio下载插件 修改背景图片
- android蓝牙测试
- Android集成Fresco框架导致在64位机器上(三星S6,华为P8等)找不到其他so库
- 保存在线图片到本地时如何能及时在媒体库中看到
- android 控件 下拉刷新 JellyRefreshLayout