您的位置:首页 > 移动开发 > Android开发

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

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: