⑴ android 定時器暫停 可以用canel()方法 但我又想再次喚起 用什麼方法
android 定時器是一個線程,線程canel 以後,只能重新new 並調用 start啟動。
具體實現代碼:
創建線程定時器同理
Thread thread = new Thread();
thread.start(); 啟動線程
當線程或定時器停止後,需要重新new
thread = new Thread();
thread.start(); 重新啟動線程或定時器
⑵ android中新聲明了一個線程,如何對這個線程進行開始,暫停,再開始呢
class RunnableDemo implements Runnable {
public Thread t;
private String threadName;
boolean suspended = false;
RunnableDemo( String name){
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 10; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(300);
synchronized(this) {
while(suspended) {
wait();
}
}
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start ()
{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
void suspend() {
suspended = true;
}
synchronized void resume() {
suspended = false;
notify();
}
}
start為開始,suspend為暫停,resume為恢復。
⑶ android如何終止一個正在運行的子線程
線程像這樣:
Thread{
boolean flag = fase;
run(){
while(!flag){
}
}
}
Thread t = new Thread();
t.start();
-----------------------------------------------------
要終止循環,只需要這樣
t.flag=true;
================================================
還有一種方式 線程像這樣:
Thread{
run(){
while(true){
Thread.sleep(xxxx);
}
}
}
Thread t = new Thread();
t.start();
--------------------------------------------
要終止循環,只需要這樣
t.interrupte();
但是這里要注意調用的時機,要在子線程執行了run方法裡面的sleep(xxxx)後xxxx時間之內調用。也就是子線程會睡一會,醒一會,睡一會,醒一會,要在子線程睡著的時候調用。
⑷ android thread join和stop的區別
stop() 方法是立即停止當前線程, 這樣停止的後果是導致stop後的語句無法執行, 有可能資源未釋放或者在同步塊中調用此方法會導致同步數據會不完整. 所以這樣的方法並不安全. 強列建議不要使用此函數來中斷線程。
interrupt()方法沒有stop那麼的粗暴,因為可以用catch捕捉到InterruptedException這個異常。一個線程處於了阻塞狀態(如線程調用了thread.sleep、thread.join、thread.wait以及可中斷的通道上的
I/O
操作方法後可進入阻塞狀態),方法調用處拋出InterruptedException異常,拋出異常是為了線程從阻塞狀態醒過來,並在結束線程前讓程序員有足夠的時間來處理中斷請求。
join() 方法作用是:「等待該線程終止」,這里需要理解的就是該線程是指的主線程等待子線程的終止。也就是在主線程調用了join()方法後面的代碼,只有等到子線程結束了才能執行
⑸ 能主動讓android主線程等待1s嗎
當然可以啊,你在主線程中
try {
Thread.sleep(1000); //就是當前線程暫停1000毫秒
} catch (Exception e) {
// TODO: handle exception
}
⑹ Java(android) 程序暫停, Thread.sleep
我覺得吧
你暫停的時候應該吧TIMER.CANCEL()掉,也就是退出TIMER,等恢復的時候再重新調用TIMER
如果時間是變動的,那麼你就不要用TIMER,自己NEW 一個THREAD然後在THREAD里設置一個標志變數,可以再外面訪問的,用它來控制線程的退出,裡面再調用this.sleep(),
不能再主線程里直接SLEEP,那樣程序會全停掉
⑺ Android 的Thread編程,我在Thread的run()方法中用Toast輸出信息時出錯!
子線程中不能彈Toast,不能更新UI,你可以在子線程中使用Handler回調,在主線程中彈Toast,可以這樣寫:
主線程在 onCreate方法之外:
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Toast.makeText(ThreadActivity.this, "toast", Toast.LENGTH_SHORT).show();
break;
}
super.handleMessage(msg);
}
};
子線程:
try {
Thread.sleep(5000);
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
⑻ Java android while 想暫停幾秒 如何實現
試試加上這樣一段代碼:
try {
//單位是毫秒
Thread.sleep(1000);
} catch(Exception ex) {
}
⑼ 如何實現Android 中斷線程的處理
可以用interrupt()方法中斷線程,而線程是否已經中斷則用Thread.currentThread().isInterrupted()方法返回true/false判斷:
1、線程start()後馬上調用interrupt(),在進入run()時中斷標志已經被set on;
2、在處理sleep()可能拋出的InterruptedException時,再次中斷線程即可成功中斷;
3、注意interrupt()方法是set on 中斷標志的,interrupted()方法是判斷後並清除中斷標志的。
public class ThreadDemo extends Thread{
public static void main(String[] args){
try{
ThreadDemo thread = new ThreadDemo();
thread.start();
thread.sleep(100);
thread.interrupt(); //中斷線程
thread.sleep(100);
thread.printStr();
thread.interrupt(); //第三次中斷線程
thread.printStr();
thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
}
private void printStr(){
System.out.println("thread obj");
}
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()) {
System.out.println("thread running");
try {
Thread.sleep(100);
}catch(InterruptedException e)
{
System.out.println("InterruptedException");
Thread.currentThread().interrupt(); //再次中斷線程
}
}
System.out.println("thread interrupted");
}
}
運行結果:
thread running
InterruptedException
thread interrupted
thread obj
thread obj
⑽ android thread 後台線程 怎麼設置隨著主線程的結束而結束
關於線程的結束有以下幾點:
1.不要手動調用stop方法強行終止一個線程,這種方式不安全。
通過幫助文檔,我們可以知道,Android的線程類本身就提供了一些公共方法去結束線程。
final void stop()
This method is deprecated. because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state
但是,通過說明我們可以看到,這些方法Android本身都是不推薦使用的,通過這種方式結束線程是不安全的。
2.線程里run函數短,執行完後線程會自行銷毀,不用手動去終止。
3.手動停止,通過在run里設置標志先停止運行,再調用Thread.interrupt();注意,在run沒有停止時調用.interrupt()沒有效果。