代码:charapter6
1.开关的方式,逻辑关闭,不关闭就在thread一直执行。
public class ThreadCloseGraceful {
private static class Worker extends Thread {
private volatile boolean start = true;
@Override
public void run() {
while (start) {
//
}
System.out.println("停止");
}
public void shutdown() {
this.start = false;
}
}
public static void main(String[] args) {
Worker worker = new Worker();
worker.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
worker.shutdown();
}
}
2.打断的方式
package one.chapter6.stopThread;
/***************************************
* @author:Alex Wang
* @Date:2017/2/19 QQ:532500648
* QQ交流群:286081824
***************************************/
public class ThreadCloseGraceful2 {
private static class Worker extends Thread {
@Override
public void run() {
while (true) {
System.out.println(123);
if (Thread.interrupted()){
System.out.println("打断");
break;
}
}
//-------------可以执行下main的逻辑
//-------------
//-------------
}
}
public static void main(String[] args) {
Worker worker = new Worker();
worker.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
worker.interrupt();
}
}
这样也可以
或者:
---
有一个问题,假设我们没有判断interrupted的机会呢?也不能判断flag,此时以上的两个方法都不可以了。
线程的状态:cnblogs.com/soulmatesjc/p/11213884.html
代码:这个真的是太重要了。
package chapter6;
public class ThreadService {
private Thread executeThread;
private boolean finished = false;
public void execute(Runnable task) {
executeThread = new Thread() {// 调用我我就启动一个线程
@Override
public void run() {
Thread runner = new Thread(task);// run方法创建一个守护线程
runner.setDaemon(true);
runner.start();
try {
runner.join();
//这里是为了什么?executeThread起来了执行到start()就结束了,可能守护线程还没来得及执行呢。join下,知直到runner执行死掉为止。 守护线程执行完了 才会finish。
finished = true;
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("all over");
}
}
};
executeThread.start();
}
public void shutdown(long mills) {
long currentTime = System.currentTimeMillis();
while (!finished) {
if ((System.currentTimeMillis() - currentTime) >= mills) {
System.out.println("任务超时,需要结束他!");
executeThread.interrupt();
break;
}
try {//没有执行结束也没有超时
executeThread.sleep(1);
} catch (InterruptedException e) {
System.out.println("执行线程被打断!");
break;
}
}
finished = false;
}
}
package chapter6;
public class ThreadCloseForce {
public static void main(String[] args) {
ThreadService service = new ThreadService();
long start = System.currentTimeMillis();
service.execute(() -> {
//load a very heavy resource.
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(123);
}
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }//假设5秒搞定了
});
service.shutdown(10000);
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
线程中断:https://blog.csdn.net/u014543872/article/details/90549240
https://blog.csdn.net/u014543872/article/details/90549240
---------------------16-----17----------优雅的停止线程------------------
代码:chapter7
index是499
三个线程都是进入这个位置:
进入了箭头位置。
解决:
synchronized (MONITOR) {
if (index > MAX)
break;
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + " 的号码是:" + (index++));
}
------------------------------18------------------------线程安全----------------
synchronized到底是什么东西呢?
public class SynchronizedTest {
private final static Object LOCK = new Object();
public static void main(String[] args) {
Runnable runnable = () -> {
synchronized (LOCK) {
try {
Thread.sleep(200_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t1 = new Thread(runnable);
Thread t2 = new Thread(runnable);
Thread t3 = new Thread(runnable);
t1.start();
t2.start();
t3.start();
}
}
共享数据去串行化去运行。
-----------------------------19----20------------synchronized--------------------