- 浏览: 121396 次
- 性别:
- 来自: 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 847java.util.concurrent.CyclicBarr ... -
Delayed interface and Delay Queue
2009-04-22 17:42 1000/** * A standard implementati ... -
Count Down Latch example code
2009-04-22 10:38 1089Key point : 1) 1 task is co ... -
3 ways to break dead lock
2009-04-21 17:30 7031) supply special resources. ... -
Blocking Queue Usage
2009-04-20 11:21 7943 implementations: LinkedBlocki ... -
The usage of Lock and Condition
2009-04-18 12:31 1038//: concurrency/waxomatic2/WaxO ... -
Re entrantLock usage
2009-04-15 17:15 1289a thread can be really interru ... -
interrupt
2009-04-15 10:57 7631) Each thread has a boolean in ... -
Executor Service Usage
2009-04-14 18:18 861ExecutorService exec = Executor ... -
Thread Local usage
2009-04-14 17:46 760ThreadLocal usage – from Javado ... -
Timer TimerTask usage
2009-04-14 12:03 688Timer typical usage new Tim ... -
wait, notify及线程通讯机制
2009-02-26 22:42 8031) wait(), notify() 方法被调用的时候,只要 ... -
Java Thread programming basical knowledge
2009-02-26 22:40 924yield() : Give a hint to the th ... -
Count Down Latch explanation
2008-10-02 10:29 896Very important paragraph on how ... -
Scheduled Executor Service
2008-07-22 11:27 1060Executor can return Executor, E ... -
Executor usage
2008-07-22 11:04 842Executor is used to arrange thr ... -
Callable Usage
2008-07-22 10:24 884The important thing need to loo ...
相关推荐
dataIn = new ObjectInputStream(socket.getInputStream()); dataOut = new ObjectOutputStream(socket.getOutputStream()); listener = new Thread(this); listener.start(); } catch (IOException ...
Java SE5 and SE6 .................. 2 Java SE6 ......................................... 2 The 4th edition........................ 2 Changes .......................................... 3 Note on the ...
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) 出现...
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 ...
[ 3.157154] usbcore: registered new interface driver btusb [ 3.163282] Bluetooth: Generic Bluetooth SDIO driver ver 0.1 [ 3.169599] Bluetooth: BlueSleep Mode Driver Ver 1.1 [ 3.175402] Bluetooth: get ...
[ 3.157154] usbcore: registered new interface driver btusb [ 3.163282] Bluetooth: Generic Bluetooth SDIO driver ver 0.1 [ 3.169599] Bluetooth: BlueSleep Mode Driver Ver 1.1 [ 3.175402] Bluetooth: get ...
[ 3.157154] usbcore: registered new interface driver btusb [ 3.163282] Bluetooth: Generic Bluetooth SDIO driver ver 0.1 [ 3.169599] Bluetooth: BlueSleep Mode Driver Ver 1.1 [ 3.175402] Bluetooth: get ...
[ 3.157154] usbcore: registered new interface driver btusb [ 3.163282] Bluetooth: Generic Bluetooth SDIO driver ver 0.1 [ 3.169599] Bluetooth: BlueSleep Mode Driver Ver 1.1 [ 3.175402] Bluetooth: get ...
R:\wyb\rtl8188eu_sina33m_sc3817\android\packages\apps\Camera2\src\com\android\camera\CameraActivity.java 6、(可选修改:) R:\wyb\rtl8188eu_sina33m_sc3817\lichee\linux-3.4\arch\arm\mach-sunxi\rf\...
5、可选: R:\wyb\ap6181_sina33m_sc3817r\android\device\softwinner\astar-d7\ueventd.sun8i.rc #/dev/video1 0777 media media /dev/snd/pcmC0D0c 0777 media media /dev/snd/pcmC0D0p 0777 media media #/dev/...