- 浏览: 127244 次
- 性别:
- 来自: Singapore
文章分类
- 全部博客 (112)
- Tiger Thread (18)
- Perforce (6)
- Spring (5)
- maven (3)
- log4j (3)
- Quartz Scheduler (4)
- unix and linux (12)
- hibernate (3)
- Enum (1)
- Futures and Options (1)
- Market Making (2)
- Java Basic (11)
- Tibco EMS (3)
- F I X message (5)
- equity derivative (2)
- Sybase (3)
- XML (1)
- JUnit (2)
- J A X B 2.0 (1)
- N I O (1)
- windows batch file (1)
- Cruise Control (1)
- util Class (5)
- ant (1)
- JMS (1)
- profiling (0)
- Sql Server (6)
- GXT (2)
- eclipse (1)
- Generics (1)
- Tibco RV (3)
- Autosys (0)
- Message (1)
最新评论
-
houzhe11:
<property name="proxyTa ...
AOP usage -- BeanNameAutoProxyCreator usage
In Java 5, Thread.interrupt() is not encouraged. Instead, you are encouraged to use
<1st way> aExecutorService.shutdownnow();
<2nd way> aFuture<?>.cancel(true);
For the 1st way, a interrupt message is sent to all Runnables.
For the 2nd way, true means, a interrupt message can be sent to the related Runnables.
There are 2 ways to submit a Runnable to a executorService instance.
<1st way> use execute(aRunnable).
Generally, Runnable object should be sent in this way.
<2nd way> use submit(aRunnable). submit(aCallable<?>)
Generally, all Callable<?> should be sent in this way, since this is the only way that you can get a Future<?> response where you can get the result.
aRunnable can also be sent in this way, the only reason to get a uselss Future<?> is because you want to call cancel<boolean> on the Future<?> object. If you call cancel(true) on it, acutally you are sending a interrupt message to the Runnable.
package test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class Thread5Interrupt
{
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException
{
ExecutorService service = Executors.newCachedThreadPool();
Future<?>[] handleList = new Future<?>[10];
for(int i = 0; i < 10; i++)
{
handleList[i] = service.submit(new RunIt());
}
TimeUnit.SECONDS.sleep(1);//wait for all runnable starting up..
// service.shutdownNow(); //Send interrupt call to all Runnables.
for(Future<?> temp : handleList) //Send interrupt call to Runables one by one
{
boolean mayInterruptIfRunning = false;
temp.cancel(mayInterruptIfRunning);
}
service.shutdown();
}
}
class RunIt implements Runnable
{
public void run()
{
System.out.println(Thread.currentThread().getName() + " start!");
try
{
TimeUnit.SECONDS.sleep(10);
}
catch (InterruptedException e)
{
System.out.println(Thread.currentThread().getName() + " interrupted! Interrupted value: " + Thread.currentThread().isInterrupted());
}
System.out.println(Thread.currentThread().getName() + " finished!");
}
}
发表评论
-
javadoc for Cyclic Barrier
2009-04-24 12:48 907java.util.concurrent.CyclicBarr ... -
Delayed interface and Delay Queue
2009-04-22 17:42 1054/** * A standard implementati ... -
Count Down Latch example code
2009-04-22 10:38 1155Key point : 1) 1 task is co ... -
3 ways to break dead lock
2009-04-21 17:30 7661) supply special resources. ... -
Blocking Queue Usage
2009-04-20 11:21 8343 implementations: LinkedBlocki ... -
The usage of Lock and Condition
2009-04-18 12:31 1070//: concurrency/waxomatic2/WaxO ... -
Re entrantLock usage
2009-04-15 17:15 1321a thread can be really interru ... -
interrupt
2009-04-15 10:57 8231) Each thread has a boolean in ... -
Executor Service Usage
2009-04-14 18:18 894ExecutorService exec = Executor ... -
Thread Local usage
2009-04-14 17:46 817ThreadLocal usage – from Javado ... -
Timer TimerTask usage
2009-04-14 12:03 725Timer typical usage new Tim ... -
wait, notify及线程通讯机制
2009-02-26 22:42 8511) wait(), notify() 方法被调用的时候,只要 ... -
Java Thread programming basical knowledge
2009-02-26 22:40 1090yield() : Give a hint to the th ... -
Count Down Latch explanation
2008-10-02 10:29 959Very important paragraph on how ... -
Scheduled Executor Service
2008-07-22 11:27 1110Executor can return Executor, E ... -
Executor usage
2008-07-22 11:04 909Executor is used to arrange thr ... -
Callable Usage
2008-07-22 10:24 935The important thing need to loo ...
相关推荐
5. **中断和唤醒(Interrupt and Wakeup)**:NIO支持中断IO操作,当操作被中断时,系统会立即停止等待并抛出异常,使得线程可以更快地响应其他事件。 6. **文件系统操作的增强**:NIO还提供了一些高级文件系统功能...
dataIn = new ObjectInputStream(socket.getInputStream()); dataOut = new ObjectOutputStream(socket.getOutputStream()); listener = new Thread(this); listener.start(); } catch (IOException ...
- **高性能**:虽然Java最初被认为是解释型语言,但随着JIT(Just-In-Time)编译器的发展,Java的性能得到了显著提升。 **1.2 运行原理** Java程序的执行过程大致分为以下几个步骤: 1. 编写源代码并保存为`.java`...
5. 通知机制:线程间的通信可以通过`wait()`和`notify()`或`java.util.concurrent`包中的`Condition`来实现。 通过上述的多线程和队列组合,我们可以构建出一个高效且符合并发编程原则的排队叫号系统,这在现实世界...
在Java中,可以使用Thread类的interrupt()方法来请求中断线程。但是,这并不意味着线程会立即停止,而是会在线程检查中断状态时抛出InterruptedException。因此,正确的中断策略是在线程的run()方法中定期检查Thread...
Java提供了Thread.interrupt()方法来中断线程,而Thread.isInterrupted()和Thread.currentThread().isInterrupted()用于检查线程是否被中断。中断机制可以用来优雅地停止线程,避免无限循环或其他长时间运行的操作。...
在计算机科学中,队列是一种先进先出(FIFO,First In First Out)的数据结构,它允许我们在一端添加元素(入队),而在另一端移除元素(出队)。这种数据结构非常适合用于处理并发请求,因为它可以确保任务按照到达...
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); ``` 4. **反射执行**: 反射是Java中的一个重要特性,它允许程序在运行时检查自身,并且可以“反射”地调用方法或构造函数。 - **示例**: 获取...
int[] ar = new int[5]; for(i = 0;i ;i++) System.out.println(ar[i]); } } 上面程序运行结果是:(选择1项) A) 打印5个0 B) 编译出错,数组ar[]必须初始化 C) 编译出错,Mine应声明为abstract D) 出现...
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String message = in.readLine(); System.out.println("Received: " + message); // 客户端 Socket socket ...
13. **Java5新特性对数组的支持** - 改进了数组的处理方式,简化了操作。 #### 面向对象(1) 1. **什么叫面向对象?** - 一种编程范式,强调使用对象来组织和设计程序。 2. **类** - 定义了一组具有相同特征...
5. **健壮性:** 强类型系统、异常处理和垃圾回收等机制确保程序健壮性。 6. **架构中立:** Java不依赖于任何特定硬件或操作系统。 7. **可移植性:** Java代码在不同平台上无需修改即可运行。 8. **高性能:** JIT...
- 标准输入输出流: `System.in`, `System.out`, `System.err`。 **8.5 其它常用的流** - 如`BufferedReader`, `BufferedWriter`用于缓冲读写操作。 #### 十一、多线程 **9.1 线程简介** - 线程是程序执行的最小...
6. ** 示例代码 **:在`producer_consumer_using_multithreading_in_java-master`这个项目中,应该包含了一个完整的示例,演示了如何用Java实现生产者消费者模型。源代码可能包括一个`Producer`类和一个`Consumer`类...
- 线程状态包括NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING和TERMINATED。 8. **ThreadLocal** - 存储线程局部变量,每个线程有自己的副本,避免数据冲突。 9. **原子类** - 面对多线程环境,提供无锁的...
Thread t = new Thread() { public void run() { System.out.println("Reading"); try { System.in.read(); } catch (IOException e) { System.err.println(e); } System.out.println("Thread finished.");...
thread.interrupt(); boolean ran = false; //这里就可以继承ThreadPoolExecutor,并覆盖beforeExecute(...)该方法,来做一些执行任务之前的统计工作或者用来保存正在执行的任务 beforeExecute(thread, task); ...
Pointerscan limit nodes is default on in a new ce install (remembers your choice when you disable it) Autoattach now happens using a thread instead of a gui blocking timer Some colorscheme ...
Scanner scanner = new Scanner(System.in); System.out.println("请输入您的名字:"); String name = scanner.nextLine(); System.out.println("您好, " + name); } } ``` ### switch语句 switch语句用于基于...