package phonedemo.demo.test;
/**
* @author 李晨亮
* @date 2020-06-16 9:28
**/
public class threadTest extends Thread implements Thread.UncaughtExceptionHandler {
// @Override
// public void run(){
// Thread thread=new Thread(){
// @Override
// public void run(){
// try {
// Thread.sleep(10000);
// } catch (InterruptedException e) {
// //返回当线程突然终止时调用的默认处理程序
// Thread.getDefaultUncaughtExceptionHandler();
// }
// }
// };
// thread.start();
// thread.start();
// thread.getName();
// //优先级的设置
// thread.setPriority();
// //判断线是否还活着
// thread.isAlive();
// //唤醒当前线程
// thread.notify();
// thread.run();
// //等多时间运行
// thread.join();
// //获取运行id
// thread.getId();
// //等几秒
// thread.wait();
// //返回真在执行的线程
// Thread.currentThread();
// //休眠多久
// Thread.sleep();
// //表示愿意放弃已经获取的服务器(调度程序可以忽略这个暗示)
// Thread.yield();
// //统计存活的线程数
// Thread.activeCount();
// //将当前线程的堆栈跟踪打印到标准错误流。(*此方法仅用于调试)
// Thread.dumpStack();
// //将当前的每个活动线程复制到指定的数组中
// Thread.enumerate();
// //将当前的每个活动线程复制到指定的数组中
// Thread.getAllStackTraces();
// //时候有锁
// Thread.holdsLock();
// }
public static void main(String[] args) {
Thread thread = new Thread() {
@Override
public void run() {
try {
Thread.sleep(1000);
Thread.sleep(10000);
} catch (InterruptedException e) {
//返回当线程突然终止时调用的默认处理程序
}
}
};
thread.start();
Thread.setDefaultUncaughtExceptionHandler(new threadTest());
UncaughtExceptionHandler defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
thread.destroy();
//直接杀死线程
thread.stop();
//关闭
thread.interrupt();
System.out.println(defaultUncaughtExceptionHandler);
}
@Override
public void uncaughtException(Thread t, Throwable e) {
String name = t.getName();
System.out.println(name + "线程异常退出");
}
}
原因是:
stop会直接杀死进程导致县城中的某些代码没有执行到,可能导致的情况是数据丢失或是数据不准确。
源码:
@Deprecated
public final void stop() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
//安全管理器
checkAccess();
if (this != Thread.currentThread()) {
security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
}
}
// A zero status value corresponds to "NEW", it can't change to
// not-NEW because we hold the lock.
if (threadStatus != 0) {
//如果线程被挂起,则唤醒线程;否则不执行操作
resume(); // Wake up thread if it was suspended; no-op otherwise
}
//VM可以处理所有线程状态
// The VM can handle all thread states
stop0(new ThreadDeath());
}
interrupt源码:
public void interrupt() {
if (this != Thread.currentThread())
checkAccess();
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt(this);
return;
}
}
interrupt0();
}
protected final void begin() {
if (this.interruptor == null) {
this.interruptor = new Interruptible() {
public void interrupt(Thread var1) {
//获取资源
synchronized(AbstractInterruptibleChannel.this.closeLock) {
if (AbstractInterruptibleChannel.this.open) {
AbstractInterruptibleChannel.this.open = false;
AbstractInterruptibleChannel.this.interrupted = var1;
try {
AbstractInterruptibleChannel.this.implCloseChannel();
} catch (IOException var5) {
}
}
}
}
};
}
protected final void implCloseChannel() throws IOException {
implCloseSelectableChannel();
//最后把锁一个个释放掉
synchronized (keyLock) {
int count = (keys == null) ? 0 : keys.length;
for (int i = 0; i < count; i++) {
SelectionKey k = keys[i];
if (k != null)
k.cancel();
}
}
}