不建议用stop 操作, 比较粗鲁, 会导致其可能的一些锁被粗暴的释放,导致一些场景下的数据不一致;
样例1 :interrupt方法
public class ThreadStopGraceful1 implements Runnable {
public void run() {
while(true) {
if (Thread.currentThread().isInterrupted){
System.out.println(Thread.currentThread().getName() + ": " + i);
break; //通过break跳出循环,从而也结束了run()所在线程
}
Thread.yield();
}
}
public static void main(String args[]) throws InterruptedException {
ThreadStopGraceful t1=new ThreadStopGraceful1();
Thread t=new Thread(t1);
t.setName("t1");
t.start();
Thread.sleep(8000);
t.interrupt();
}
}
样例2:使用标志位+ interrupt方法:
public class ThreadStopGraceful2 implements Runnable {
boolean isRun = true;
public boolean isRun() {
return isRun;
}
public void setRun(boolean run) {
isRun = run;
}
public void run() {
for (int i = 0; i < 10; i++) {
if (Thread.currentThread().isInterrupted){
System.out.println(Thread.currentThread().getName() + ": " + i);
break; //通过break跳出循环,从而也结束了run()所在线程
}
if (isRun) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
Thread.sleep(2000);//recive interrupt when thread is sleep
} catch (InterruptedException e) {
System.out.println("interrupted when sleep");
Thread.currentThread().interrupt();//抛出异常后,中断标志会被清空,需要再次设置中断标志,防止后续循环终端标志失效
e.printStackTrace();
}
} else {
return;
}
}
}
public static void main(String args[]) throws InterruptedException {
ThreadStopGraceful t1=new ThreadStopGraceful();
Thread t=new Thread(t1);
t.setName("t1");
t.start();
Thread.sleep(8000);
t1.setRun(false);
}
}
46条评论
This is very interesting, You’re a very skilled blogger. I have joined
your rss feed and look forward to seeking more of your excellent post.
Also, I’ve shared your website in my social networks!
🙂
Wonderful beat ! I wish to apprentice while you amend your web site, how could i subscribe for a blog site?
The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast
provided bright clear concept
🙂
Aw, this was a very nice post. Taking the time and actual effort to produce a top notch article… but what can I say… I hesitate
a whole lot and don’t manage to get nearly anything done.
🙂
Good day! This is my 1st comment here so I just wanted to give a
quick shout out and tell you I truly enjoy reading
through your articles. Can you suggest any other blogs/websites/forums that cover the
same topics? Thank you so much!
🙂
Someone necessarily help to make severely posts I would state.
This is the first time I frequented your web page
and to this point? I amazed with the research you made to make this actual submit
incredible. Wonderful task!
🙂
It’s an amazing paragraph in favor of all the online users;
they will obtain advantage from it I am sure.
:)
you’re really a good webmaster. The site loading velocity is amazing.
It sort of feels that you are doing any distinctive trick.
In addition, The contents are masterpiece. you’ve performed a great task on this topic!
🙂
Someone necessarily lend a hand to make critically articles I would state.
This is the first time I frequented your website page
and thus far? I surprised with the analysis you made to create this particular submit incredible.
Excellent process!
🙂
I used to be suggested this web site by way of my cousin. I’m not certain whether this publish is written via
him as nobody else recognize such targeted approximately my
problem. You are incredible! Thank you!
🙂
Ahaa, its good conversation about this paragraph at this place at this
website, I have read all that, so at this time me also commenting here.
Hi, I do think this is a great site. I stumbledupon it
😉 I’m going to return yet again since I bookmarked it.
Money and freedom is the greatest way to change, may you be rich and continue to help others.
I simply could not leave your web site before suggesting that I
really loved the standard info a person supply in your guests?
Is going to be back incessantly in order to inspect new posts http://foxnews.org
There is certainly a lot to learn about this issue.
I like all of the points you have made.
thank you
hello there and thank you for your info – I’ve definitely picked up anything new from right here.
I did however expertise several technical issues using this site, as I experienced to reload the web site a
lot of times previous to I could get it to load correctly.
I had been wondering if your web host is OK?
Not that I am complaining, but sluggish loading instances times will
very frequently affect your placement in google
and could damage your quality score if ads and marketing with Adwords.
Anyway I am adding this RSS to my email and could look out
for a lot more of your respective exciting content.
Ensure that you update this again soon.
You have made some decent points there. I checked on the internet for additional information about the issue and found most people will go along with your
views on this web site.
Free PC choices to Recycle and Phatmatik Pro:Loopdrive3 VSTABS.
“Because you happen to be here reading this article flash games report now as of this particular point in time; what you have is “The first movers advantage” by yourself to consider a their hands on now. These art reproductions of paintings printed on canvas sheets or paper and then framed and hung on walls.
The great thing concerning this DVD is that you may perform
dance exercises anytime, without a partner. They will shun jobs its keep is not any advantage of the community
involved. These art reproductions of paintings printed on canvas sheets or paper after which framed and hung on walls.
Inspiring story there. What occurred after?
Good luck!
Each beauty obtainable in Fortnite Battle Royale.
Way cool! Some very valid points! I appreciate you writing this post and
the rest of the site is very good.
:)
I like reading a post that can make people think.
Also, thanks for allowing for me to comment!
Hola! I’ve been following your website for a while now and finally got the courage to go ahead and give you
a shout out from Atascocita Texas! Just wanted to tell you keep up the excellent job!
:)
Keep on working, great job! http://organikyumurtaizmir.com
This post offers clear idea designed for the new viewers
of blogging, that really how to do running a blog.
Spot on with this write-up, I absolutely believe this web site needs a great deal more attention. I’ll probably be back again to see more, thanks
for the info!
thank you
Just a smiling visitor here to share the love (:, btw outstanding design and style.
🙂
Great web site you have here.. It’s hard to find good quality writing like yours these days.
I seriously appreciate individuals like you!
Take care!!
🙂
Hello colleagues, how is all, and what you would like to say concerning
this article, in mmy view its truly amazing designed for me.
🙂
I wanted to thank you a lot more for this amazing blog you have
made here. It truly is full of useful tips for those who are
definitely interested in this specific subject, particularly this very
post. You really are all so sweet as well as thoughtful of others and reading your website posts is a
wonderful delight with me. And such a generous surprise!
Ben and I will certainly have excitement making use of
your guidelines in what we should instead do next week.
Our listing is a kilometer long so your tips will certainly be put to
great use.
:)
I know this if off topic but I’m looking into starting my own weblog and was
wondering what all is required to get setup?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very web smart so I’m not 100% positive. Any recommendations
or advice would be greatly appreciated. Thank you
:)