源:http://samttsch.iteye.com/blog/1118998
评:
1 interruped
java中当一个线程sleep时,调用其中断方法是无效的,因此在异常的处理中需要将线程interrupt(),否则线程依然是非interrupt状态。
下面的例子中,线程将进入死循环,不会退出,因为主线程调用了interrupt()方法时,线程s正在睡眠。
正确的方法应该在异常中调用Thread.currentThread().interrupt();
public static void main(String[] args) throws InterruptedException
{
Thread s = new Thread(){
public void run(){
while(!interrupted()){
System.out.println("------");
try{
sleep(5000);
} catch (InterruptedException e) {
//Thread.currentThread().interrupt();
System.out.println("异常");
}
System.out.println(".......");
}
}
};
s.start();
Thread.currentThread().sleep(2000);
s.interrupt();
}
2.synchronized与wait区别
synchronized不会让线程进入对象的等待队列,线程不会停止,而wait表示将线程放入到对象的等待队列中,只有当其他的线程调用了notify,notifyAll时,jvm才会调度该线程
3.静态synchronized
synchronized表示同步的对象为当前对象,静态synchronized表示锁定的是class对象
4.stop方法用来停止线程不安全,当一个线程被stop时,它会立即释放它锁定的所有对象的锁,这会使得各个对象处于不一致的状态中,要停止一个线程,使用一个变量来标示
分享到:
相关推荐
java线程中的interrupt,isInterrupt,interrupted方法
主要介绍了JAVA多线程之中断机制stop()、interrupted()、isInterrupted()的相关资料,需要的朋友可以参考下
在java的api中有stop、suspend等方法可以达到目的,但由于这些方法在使用上存在不安全性,会带来不好的副作用,不建议被使用。具体原因可以参考Why is Thread.stop deprecated。 在本文中,将讨论中断在java中的...
oracle连接失败 解决方案 超详细 挺有用的
New chapters cover thread performance, using threads with Swing, threads and Collection classes, thread pools, and threads and I/O (traditional, new, and interrupted). Developers who cannot yet ...
interrupt,interrupted,isInterrupted方法区别 join方法 yield方法 多线程 进程和线程 进程与线程最主要的区别是它们是操作系统管理资源的不同方式的体现。 准确来说进程与线程属于衍生关系。 进程是操作系统执行...
Java™ Puzzlers: Traps, Pitfalls, and Corner Cases.chm,英文版本,chm 格式,大小 1 Mb,作者:Joshua Bloch、Neal Gafter。 内容预览: Chapter 1. Introduction Chapter 2. Expressive Puzzlers Puzzle 1: ...
Puzzle 84: Rudely Interrupted Puzzle 85: Lazy Initialization Chapter 10. Advanced Puzzlers Puzzle 86: Poison-Paren Litter Puzzle 87: Strained Relations Puzzle 88: Raw Deal Puzzle 89: ...
主要介绍了Java中断一个线程操作,结合实例形式分析了java中断线程相关的interrupt()、isInterrupted()及interrupted()函数使用技巧,需要的朋友可以参考下
java8 源码 Java SE 的代码与笔记 [ReadMe部分没有代码,代码都在项目文件中] [Java多线程核心编程技术] 书籍笔记 第一章:Java多线程技能 线程是进程中的子任务 interrupted与isInterrupted的区别: interrupted是...
(2)使用FutureTask(实现Runnable接口)来指定任务,可以通过get方法来获得执行结果,如果当前还获取不到结果就会阻塞,直到结果返回。 (3)调用start方法只是让线程进入就绪状态,是否可以真正运行run()是由任务调度器...
Java应用程序设计之多线程和泛型 Thread、Runnable、Callable 线程操作:isAlive()、isInterrupted()、join()、sleep()、stop()、interrupted()、setDaemon()、setPriority()、yield() 同步与死锁、Object类对线程的...
Thread.interrupted() && runState >= STOP) thread.interrupt(); boolean ran = false; //这里就可以继承ThreadPoolExecutor,并覆盖beforeExecute(...)该方法,来做一些执行任务之前的统计工作或者用来保存...
SVN使用过程中有时候,人为中断update操作,造成SVN命令未执行完毕,进而无法进行cleanup操作。 执行cleanup一直提示“Previous operation has not finished; run 'cleanup' if it was interrupted
run 'cleanup' if it was interrupted.的解决工具。sqlite3.exe绿色版 下载 SQLite是一款轻型的数据库,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,...
这个东西是用来解决SVN的bug的,svn报"Error: Previous operation has not finished; run 'cleanup' if it was interrupted"这个错时解决
Java并发示例 no01_threads-JustMain-一个主线程,使用调试器检查并暂停线程。 最有可能的是,它将在睡眠时停止在。 no02_threads_basics_deadlock_interrupt InterruptedThread:这是一个示例,说明如何可以中断...
6427312: (fc) FileChannel.transferTo() throws IOException "system call interrupted" 5103988: (fc) FileChannel.transferTo should return -1 for EAGAIN instead throws IOException 6253145: (fc) ...
downloading, download can be interrupted and continuous, download task management. It’s simple and easy to use. Only support HTTP. Feel free to use. 这是一个Android下载器。支持多线程断点下载,任务...
import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.GestureDetector; import android.view.KeyEvent; import android....