android---多线程(启动,暂停,终止)

来源:互联网 发布:大班美工多彩的剪纸 编辑:程序博客网 时间:2024/05/01 21:45
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();
}
}
}
0 0
原创粉丝点击