Sunday, February 23, 2014

How Handler thread ends


removeCallbacks () method can not be created by the end of the thread Handler<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Let yourself quit shortly after the line it, or to put an end mark.
Reply:
The reply was deleted at 2011-03-25 17:28:25 moderator

Reply:
handler.removeCallbacks ();
May end
Reply:
Think, is not necessary. Originally used in the life cycle. Those activities are not completely free memory. Wait for the system to manage knowledge, this is also the man has no control.
Handler relatively thread problem is not great. If, from this scheduling considerations. I think, or allow the system to manage their own good.
If you want deliberately to close it, then find LS to say, give it a try.

Reply:
I
 
/ / HandleMessage method
public void handleMessage (Message msg) {
ProgressBar.setProgress (msg.arg1);
Handler.post (updateThread);
}
/ / Run method
public void run () {
System.out.println ("Begin Thread");
i = i + 10;
/ / Get a message object, Message class is the Android operating system
Message msg = updateBarHandler.obtainMessage ();
/ / Set the value of the object arg1 msg parameter is i, passing messages with arg1 and arg2 members of these two variables, the advantage of the system performance consumes less
msg.arg1 = i;
try {
/ / Set the current display sleep one second
Thread.sleep (1000);
} Catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
/ / Msg object will be added to the message queue which
updateBarHandler.sendMessage (msg);
if (i == 100) {
updateBarHandler.removeCallbacks (updateThread);
}
}

But when i add more than 100 have not stopped there thread running
I think handler is not it will continue to create threads loaded into the queue it?
Reply:
Do you mean to say, if your program is running, and not stop until it runs right in

I have an idea, you try, if you can, put your impress this program, let me try.

Set a loop LOOP. Give if, or while doing judgment.
Oh, and if the line, give me see ah.

Reply:
miCount = 0,
miCount + +;
miCount> 10;
Not move. Or simply finish ();
You give it a try.
Reply:
reference to the third floor zhangnianxiang reply:
handler.removeCallbacks ();
You can end
I wrote another thread in Hander an endless loop in the program is running have used hander.removeCallbacks () method can be an infinite loop does not end, continues,
Solving! ! !
Reply:
Do not have an infinite loop, ah, you can also use sendMessage come inside in another callback ah
Reply:
references, 5th Floor rocky225 reply:
but when i add more than 100 have not stopped, there is a thread running
I think handler is not it will continue to create threads loaded into the queue it?


According to my observation, the thread should be removed from the message queue removeCallbacks
But the thread is not over yet continues to run
In
f (i == 100) {
updateBarHandler.removeCallbacks (updateThread);
}
Lane plus return; should be on the line, so:

f (i == 100) {
updateBarHandler.removeCallbacks (updateThread);
return;
}
Reply:
My problem is to add a listener
After clicking stopped prematurely run this thread does not know how to achieve
Reply:
reference to the 8th floor smilezkangel reply:
reference to the third floor zhangnianxiang reply:
handler.removeCallbacks ();

Can endI wrote an infinite loop in Hander's another thread in the program is running have used hander.removeCallbacks () method can be an infinite loop does not end continues,
Solving! ! !


My idea is this: Set a if () statement to the end of the thread as a condition, such as if (msg.arg1 == 200)
Then the end of the cycle when needed, set the value of a 200 msg sent to the thread, this is not the end of the thread can be up?

My own test results fail, I took a class member variable msg set, then the value of the end of the thread condition msg sent thread
Results of the virtual machine error-___________________-

I do not know my thinking wrong, or thought I executed the wrong way?
Reply:
To learn under
Reply:
Oh, stop the thread, although there early stop function, but later abandoned to so you are only using the approach end mark.


An end flag is set
 
boolean flag = true;

.... Thread ... {
public void run ()
{
while (flag) {
/ / Do some thing
}
}

Reply:
reference to the 14th floor taiji1985 reply:
Oh, stop the thread, although there early stop function, but later abandoned it to the way you are now only by the end. mark.


An end flag is set
Java code

boolean flag = true;

.... Thread ... {
public void run ()
{
while (flag) {
/ / Do some thing
}
}


Well useful, I solved the problem
Thank you ~
Reply:
Mark is the positive solution set
Reply:
thread turned bad stopped, tags can be allowed to stop the internal settings, but you still encounter the thread to try to return to the deadlock in the code layer, such as processing state machine rotation mode
Reply:
Looks like you say is the problem of threads, not much relationship with HANDLER
Reply:
Operating system PV operation ...
Reply:
Looks like the end of the flag should be set
Reply:
reference to the 14th floor taiji1985 reply:
Oh, stop the thread, although there early stop function, but later abandoned it to the way you are now only by the end. mark.


An end flag is set
Java code

boolean flag = true;


.... Thread ... {
public void run ()
{
while (flag) {
/ / Do some thing
}
}

Indeed, seemingly, to open up a thread in android framework, there is no better way to say it seems to go well off the end of the process.
Reply:
Directly reference this thread variable is set to NULL, go to the GC can be recycled.
In the case of stop () method of waste, which is one of the more secure method.
Reply:
reference to the 8th floor smilezkangel reply:
reference to the third floor zhangnianxiang reply:
handler.removeCallbacks ();

Can endI wrote an infinite loop in Hander's another thread in the program is running have used hander.removeCallbacks () method can be an infinite loop does not end continues,
Solving! ! !

Directly with a logo to let the dead end of the cycle is not on the line. . . .
hander.removeCallbacks () method is not the end of the thread loop
Reply:
The
if (i == 100) {
updateBarHandler.removeCallbacks (updateThread);
}
Statement put
public void handleMessage (Message msg) {
ProgressBar.setProgress (msg.arg1);
Handler.post (updateThread);
}
Like, because although the judge after the end of the thread sendmessage, but handlemessage method to re-enable the thread.
Reply:
reference to the 24th floor firepopo reply:
will
if (i == 100) {
updateBarHandler.removeCallbacks (updateThread);
}
Statement put
public void handleMessage (Message msg) {
ProgressBar.setProgress (msg.arg1);
......
handsome
Reply:
reference to the 24th floor of the reply:
will
if (i == 100) {
updateBarHandler.removeCallbacks (updateThread);
}
Statement put
public void handleMessage (Message msg) {
ProgressBar.setProgress (msg.arg1);
Handler.post (updateThread);
}
Like ......

This can be ~
Reply:
 
private myRunnable runnable = new myRunnable ();
private Handler myHandler = new Handler () {

@ Override
public void handleMessage (Message msg) {
/ / TODO Auto-generated method stub
super.handleMessage (msg);
if (msg.arg1> 100) {
myHandler.removeCallbacks (runnable);
}
else {
progressBar.setProgress (msg.arg1);
myHandler.post (runnable);
System.out.println (msg.arg1);
}
}
};

class myRunnable implements Runnable {
int i = 0;
public void run () {
/ / TODO Auto-generated method stub
i = i + 10;
Message message = myHandler.obtainMessage ();
message.arg1 = i;
try {
Thread.sleep (1000);
}
catch (InterruptedException e) {
e.printStackTrace ();
}
myHandler.sendMessage (message);
}
}

Reply:
Learn
Reply:
HandlerThread thread = new HandlerThread ("netthread");
thread.start ();
Handler r = new Handler (thread.getLooper ());
r.postDelayed (new java.lang.Runnable () {
public void run () {
......
}}, 1000);
This thread is executed only once ......

No comments:

Post a Comment