notify(): 是很温和的唤醒线程的方法,它不可以指定清除哪一个异常 interrupt(): 粗暴的方式,强制清除线程的等待状态,被清除的线程会接收到一个InterruptedException异常。(例:d.interrupt()方法是无法直接阻止的,要加循环) 它可以指定清除,某一个异常。 ...
分类:
编程语言 时间:
2016-08-19 00:54:14
阅读次数:
157
二Java多线程
(4)线程的状态
线程状态转换示意图:
线程控制的基本方法:
1)sleep()方法
可以调用Thread的静态方法:
public static void sleep(long millis) throws interruptedException
使...
分类:
编程语言 时间:
2016-06-21 07:23:02
阅读次数:
244
1. FutureTask的get方法靠什么机制来阻塞 看其get方法源码: /** * @throws CancellationException {@inheritDoc} */ public V get() throws InterruptedException, ExecutionExcep... ...
分类:
其他好文 时间:
2016-06-20 14:03:00
阅读次数:
425
之前介绍过,停机线程可以使用interrupte,可以用标记,让run执行结束。现在我们来看几个方法。方法1、使用interrupte方法来执行,通过抛出InterruptedException来结束run运行。packagesecondThreadStudy;
importjava.lang.InterruptedException;
publicclassMyThreadextendsT..
分类:
编程语言 时间:
2016-05-29 16:55:04
阅读次数:
461
class Computer{ int num=0; boolean flag=false; public synchronized void set(){ if(flag){ try{ super.wait(); }catch(InterruptedException e){ e.printSta ...
分类:
编程语言 时间:
2016-05-17 13:32:45
阅读次数:
153
class Computer{ int num=0; boolean flag=false; public synchronized void set(){ if(flag){ try{ super.wait(); }catch(InterruptedException e){ e.printSta ...
分类:
编程语言 时间:
2016-05-17 13:31:56
阅读次数:
124
Java中没有一种安全的抢占式方法来停止线程,只有一种协作式的机制。
大致分为两种协作式机制:1.设置某个”已请求取消“的标志,线程任务定期查看该标志。如果取消标志设置为true,则结束任务
2.调用线程的interrupt()能中断目标线程,通过Thread.currentThread().isInterrupted()方法来查询,也可以通过大多数可阻塞的库函数(如Thread.sleep和Object.wait)来抛出InterruptedException异常,在异常中退出线程。...
分类:
编程语言 时间:
2016-05-12 21:22:23
阅读次数:
183
对象锁,是 使用一个object 对象,将这个对象供多个线程共享使用,然后再线程中,对这个对象加锁。直接看代码package com.luoy.Thread.wait;public class ObjectWait2{ public static void main(String[] args)
throws InterruptedException{ Object...
分类:
编程语言 时间:
2016-05-06 12:34:40
阅读次数:
133
一,介绍 这篇文章主要记录使用 interrupt() 方法中断线程,以及如何对InterruptedException进行处理。感觉对InterruptedException异常进行处理是一件谨慎且有技巧的活儿。 由于使用stop()方法停止线程非常的暴力,人家线程运行的好好的,突然就把人家杀死了 ...
分类:
编程语言 时间:
2016-05-01 17:49:23
阅读次数:
289
这一章节我们来讨论一下synchronized持有对象锁。1.当所有方法都不使用同步的时候代码清单package com.ray.deepintothread.ch02.topic_2;
public class SynchInstance1 {
public static void main(String[] args) throws InterruptedException {
MyT...
分类:
编程语言 时间:
2016-04-29 18:59:02
阅读次数:
232